Risc-v instruction that i dont understand
I have this risc v code :
lui S0, 0x1234
ori S1, S0, 0x5678
add S2, S1, S1
and the question asks me, "What does the register S2 hold?"
The question explains that lui and I quote:
"Load the lower half word of the immediate imm into the upper halfword of register rt. The lower bits of the register are set to 0"
I don't know how to 'compile this program' and what does 0x1234 mean? Thanks
instruction-set riscv
add a comment |
I have this risc v code :
lui S0, 0x1234
ori S1, S0, 0x5678
add S2, S1, S1
and the question asks me, "What does the register S2 hold?"
The question explains that lui and I quote:
"Load the lower half word of the immediate imm into the upper halfword of register rt. The lower bits of the register are set to 0"
I don't know how to 'compile this program' and what does 0x1234 mean? Thanks
instruction-set riscv
0x1234is a hexadecimal number equal to4660(decimal).
– Amy
Nov 14 '18 at 19:31
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
Error: illegal operandsori s1,s0,0x5678-- immediate operand is 12 bits only (sign extended)
– Pavel Smirnov
Nov 23 '18 at 21:24
add a comment |
I have this risc v code :
lui S0, 0x1234
ori S1, S0, 0x5678
add S2, S1, S1
and the question asks me, "What does the register S2 hold?"
The question explains that lui and I quote:
"Load the lower half word of the immediate imm into the upper halfword of register rt. The lower bits of the register are set to 0"
I don't know how to 'compile this program' and what does 0x1234 mean? Thanks
instruction-set riscv
I have this risc v code :
lui S0, 0x1234
ori S1, S0, 0x5678
add S2, S1, S1
and the question asks me, "What does the register S2 hold?"
The question explains that lui and I quote:
"Load the lower half word of the immediate imm into the upper halfword of register rt. The lower bits of the register are set to 0"
I don't know how to 'compile this program' and what does 0x1234 mean? Thanks
instruction-set riscv
instruction-set riscv
edited Nov 14 '18 at 19:29
dopamane
85611024
85611024
asked Nov 14 '18 at 17:37
Razi AwadRazi Awad
82
82
0x1234is a hexadecimal number equal to4660(decimal).
– Amy
Nov 14 '18 at 19:31
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
Error: illegal operandsori s1,s0,0x5678-- immediate operand is 12 bits only (sign extended)
– Pavel Smirnov
Nov 23 '18 at 21:24
add a comment |
0x1234is a hexadecimal number equal to4660(decimal).
– Amy
Nov 14 '18 at 19:31
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
Error: illegal operandsori s1,s0,0x5678-- immediate operand is 12 bits only (sign extended)
– Pavel Smirnov
Nov 23 '18 at 21:24
0x1234 is a hexadecimal number equal to 4660 (decimal).– Amy
Nov 14 '18 at 19:31
0x1234 is a hexadecimal number equal to 4660 (decimal).– Amy
Nov 14 '18 at 19:31
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
Error: illegal operands
ori s1,s0,0x5678 -- immediate operand is 12 bits only (sign extended)– Pavel Smirnov
Nov 23 '18 at 21:24
Error: illegal operands
ori s1,s0,0x5678 -- immediate operand is 12 bits only (sign extended)– Pavel Smirnov
Nov 23 '18 at 21:24
add a comment |
2 Answers
2
active
oldest
votes
Take the instructions one at a time. First the load-upper-immediate, take the immediate (0x1234) and "load" it into the upper half of the S0 register and zero out the lower half:
lui S0, 0x1234
S0 = 0x12340000
Next the or-immediate, we OR the value in S0 with the value 0x5678:
ori S1, S0, 0x5678
0x12340000
OR 0x00005678
----------
0x12345678 = S1
Finally the add, we are adding the value in S1 to itself or, equivalently, multiplying the value in S1 by 2:
add S2, S1, S1
0x12345678
+ 0x12345678
----------
0x2468ACF0 = S2
So the value in S2 register is 0x2468ACF0. Note, I am assuming 32-bit words. An immediate is like a constant, so lui is an instruction that places a constant into the upper half of a register. In combination with ori, you can load an entire word-immediate into a register.
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.0x1234(base 16) =0001_0010_0011_0100(base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.
– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
|
show 1 more comment
"LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros." -- riscv-spec-v2.2
so,
lui s0, 0x1234
s0 should be 0x01234000
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305898%2frisc-v-instruction-that-i-dont-understand%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Take the instructions one at a time. First the load-upper-immediate, take the immediate (0x1234) and "load" it into the upper half of the S0 register and zero out the lower half:
lui S0, 0x1234
S0 = 0x12340000
Next the or-immediate, we OR the value in S0 with the value 0x5678:
ori S1, S0, 0x5678
0x12340000
OR 0x00005678
----------
0x12345678 = S1
Finally the add, we are adding the value in S1 to itself or, equivalently, multiplying the value in S1 by 2:
add S2, S1, S1
0x12345678
+ 0x12345678
----------
0x2468ACF0 = S2
So the value in S2 register is 0x2468ACF0. Note, I am assuming 32-bit words. An immediate is like a constant, so lui is an instruction that places a constant into the upper half of a register. In combination with ori, you can load an entire word-immediate into a register.
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.0x1234(base 16) =0001_0010_0011_0100(base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.
– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
|
show 1 more comment
Take the instructions one at a time. First the load-upper-immediate, take the immediate (0x1234) and "load" it into the upper half of the S0 register and zero out the lower half:
lui S0, 0x1234
S0 = 0x12340000
Next the or-immediate, we OR the value in S0 with the value 0x5678:
ori S1, S0, 0x5678
0x12340000
OR 0x00005678
----------
0x12345678 = S1
Finally the add, we are adding the value in S1 to itself or, equivalently, multiplying the value in S1 by 2:
add S2, S1, S1
0x12345678
+ 0x12345678
----------
0x2468ACF0 = S2
So the value in S2 register is 0x2468ACF0. Note, I am assuming 32-bit words. An immediate is like a constant, so lui is an instruction that places a constant into the upper half of a register. In combination with ori, you can load an entire word-immediate into a register.
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.0x1234(base 16) =0001_0010_0011_0100(base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.
– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
|
show 1 more comment
Take the instructions one at a time. First the load-upper-immediate, take the immediate (0x1234) and "load" it into the upper half of the S0 register and zero out the lower half:
lui S0, 0x1234
S0 = 0x12340000
Next the or-immediate, we OR the value in S0 with the value 0x5678:
ori S1, S0, 0x5678
0x12340000
OR 0x00005678
----------
0x12345678 = S1
Finally the add, we are adding the value in S1 to itself or, equivalently, multiplying the value in S1 by 2:
add S2, S1, S1
0x12345678
+ 0x12345678
----------
0x2468ACF0 = S2
So the value in S2 register is 0x2468ACF0. Note, I am assuming 32-bit words. An immediate is like a constant, so lui is an instruction that places a constant into the upper half of a register. In combination with ori, you can load an entire word-immediate into a register.
Take the instructions one at a time. First the load-upper-immediate, take the immediate (0x1234) and "load" it into the upper half of the S0 register and zero out the lower half:
lui S0, 0x1234
S0 = 0x12340000
Next the or-immediate, we OR the value in S0 with the value 0x5678:
ori S1, S0, 0x5678
0x12340000
OR 0x00005678
----------
0x12345678 = S1
Finally the add, we are adding the value in S1 to itself or, equivalently, multiplying the value in S1 by 2:
add S2, S1, S1
0x12345678
+ 0x12345678
----------
0x2468ACF0 = S2
So the value in S2 register is 0x2468ACF0. Note, I am assuming 32-bit words. An immediate is like a constant, so lui is an instruction that places a constant into the upper half of a register. In combination with ori, you can load an entire word-immediate into a register.
edited Nov 15 '18 at 2:56
answered Nov 15 '18 at 2:50
dopamanedopamane
85611024
85611024
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.0x1234(base 16) =0001_0010_0011_0100(base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.
– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
|
show 1 more comment
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.0x1234(base 16) =0001_0010_0011_0100(base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.
– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
thank you so much sir , finally i understand how to compile thanks to you. very useful
– Razi Awad
Nov 15 '18 at 7:39
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
can i ask you one tiny question ? i have read that the lui instruction moves the 16 bits to the higher and the other 16 bits to the lower . how did you determine which side is higher and which is lower . also the 0x1234 does it contain 4 bits ? (1234) i have problem understanding this
– Razi Awad
Nov 15 '18 at 7:42
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
becuase we should have written it like that : 0x1234 0000 0000 0000 0000 0000 0000 0000 which is 32 bits half of it higher whiich is far left and half of it lower far right , Is it like that ?
– Razi Awad
Nov 15 '18 at 7:45
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.
0x1234 (base 16) = 0001_0010_0011_0100 (base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.– dopamane
Nov 15 '18 at 7:52
0x1234 is base 16 (or hexadecimal), so each "digit" is four bits.
0x1234 (base 16) = 0001_0010_0011_0100 (base 2). A register has 32-bits or 8 hex digits. The spec fig2.1 shows the most-significant-bit (XLEN-1) to the left-side. So upper is left, lower is right.– dopamane
Nov 15 '18 at 7:52
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
Also, these instructions are technically "assembled" by an "assembler". Basically the Risc-V assembler takes the instructions (or mnemonics) and encodes them into binary words which are "fed" to the cpu. The instructions that an assembler understands make up an "assembly language". Compiling is a term used for higher level languages like C/C++.
– dopamane
Nov 15 '18 at 8:04
|
show 1 more comment
"LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros." -- riscv-spec-v2.2
so,
lui s0, 0x1234
s0 should be 0x01234000
add a comment |
"LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros." -- riscv-spec-v2.2
so,
lui s0, 0x1234
s0 should be 0x01234000
add a comment |
"LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros." -- riscv-spec-v2.2
so,
lui s0, 0x1234
s0 should be 0x01234000
"LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros." -- riscv-spec-v2.2
so,
lui s0, 0x1234
s0 should be 0x01234000
edited Nov 23 '18 at 2:54
answered Nov 23 '18 at 2:48
jimmy lijimmy li
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305898%2frisc-v-instruction-that-i-dont-understand%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
0x1234is a hexadecimal number equal to4660(decimal).– Amy
Nov 14 '18 at 19:31
ok and what does the lui do
– Razi Awad
Nov 14 '18 at 20:08
You state what it does in the question.
– Amy
Nov 14 '18 at 21:23
stackoverflow.com/q/50742420/995714
– phuclv
Nov 15 '18 at 3:14
Error: illegal operands
ori s1,s0,0x5678-- immediate operand is 12 bits only (sign extended)– Pavel Smirnov
Nov 23 '18 at 21:24