assemblyriscv

Loading negative values into RISC-V


I have an assignment where I have to load values into register s7 and everything is working fine until I get to -2999.` xor s7, s7, s7 # Clear s7 addi s7, s7, 29 # Add 29 to s7

# b. -214
xor s7, s7, s7    # Clear s7
addi s7, s7, -214 # Add -214 to s7

# c. -2999
xor s7, s7, s7    # Clear s7
addi s7, s7, -2999 # Add -2999 to s7`

When the code executes to put -2999 into the register it loads 1097. I am not too sure what I am doing wrong. Could someone provide some insight please?


Solution

  • The addi instruction has a sign-extended 12-bit field for the immediate. -2999 doesn't fit into 12 bits. You need to do this in two steps or use a different method entirely.