Learning RiscV my lecturer defined a new command called Load Upper Immediate (lui) like this:
lui rt, imm
loads the lower halfword of the immediate imm into the upper halfword of register rt. The lower bits of the register are set to 0.
with the following image:
But there are few things which I can't understand:
"loads the lower halfword of the immediate" what does this mean? I think if we have 32 bits in imm then it loads the first 16 am I right?
Is the image correct at all? shouldn't the first half be all zeroes and the difination mentions? why we have those 0xf, 0, rt and where rt came from?
Given the following command:
lui S0, 0x1234
What will it do? I don't know the value of location 1234 in memory...
Before even attempting to read or write assembly language you need to get the documentation. For the instruction set you are using, is this a MIPS question (the image you posted) or a risc-v question as documented in the text of the title, etc?
Assuming risc-v go to risc-v.org and follow the links to the documentation, they have made it extremely easy to find.
LUI in risc-v is defined as such
Obviously every instruction needs some bits for the processor to decode to know what instruction it is, only one can have the pattern zero so there are non-zero bits in most opcodes. Likewise you need a destination register, where the value get stored, encoded in the instruction as well.
Painfully obvious how this instruction works.
I will admit the risc-v documentation could have been done better, finding the opcode...0b0110111 or 0x37.
Not sure what the confusion is about how humans read numbers
these all describe the same value, which all describe the same bit pattern in the instruction.
so that means they could have just put that in the instruction definition like everyone else.
So knowing that we can for example construct
assemble then disassemble
Okay, so let's try that forward:
assemble and disassemble
So there we go, instruction encoding solved.
So this was quite clear the 32 bit constant is in this case
gets stored in register x2 in this case.
Both the encoding and the operation of all of the instructions are defined, most should be easy to understand. The encoding is very straight forward and easy to understand.
Now if this was a MIPS question and not a risc-v question then in this case it is as equally easy to understand. The 16 bit immediate goes into bits 31:16 of the constant being constructed with bits 15:0 all being zeros and that constant being stored in the register encoded in the instruction. Along with an opcode so the processor can know what instruction this is.