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?
Note: This question was originally titled/tagged risc-v, but the code can only assemble for MIPS, and the accepted answer is also only correct for MIPS. So lets just make it a MIPS question.
MIPS uses 16-bit immediates for lui
and all other immediate instructions, and zero-extends bitwise boolean immediates (ori/andi/xori). Other MIPS instructions do sign-extend their immediate, like RISC-V does for everything.
RISC-V lui
takes a 20-bit immediate, while other instructions only take a 12-bit sign-extended immediate, so lui
and/or addi
can still materialize any 32-bit constant in 1 or 2 RISC-V instructions, like MIPS and all(?) other 32-bit RISC ISAs.
0x1234
is a hexadecimal number equal to4660
(decimal). – Saltwaterori s1,s0,0x5678
-- immediate operand is 12 bits only (sign extended) – Lamarori
zero-extend their immediates instead of sign-extending like most MIPS instructions (and all RISC-V instructions). @PavelSmirnov is correct, thatori
isn't encodeable for RV32. – Stalder