-1

I have to store the hex 0x1234ABCD into a register s0 that starts at memory address 0x300?

lui s0, 0x1234B
addi s0, s0, 0xFFFFFBCD
?????

I expect to have stored in s0 0x1234ABCD so then I can manipulate each address as such: address 0 holds 0x12, address 1 holds 0x34, address 2 holds 0xAB, address 3 holds 0xCD

Rylion
  • 5
  • 3
  • The low 12 bits are signed-negative, so you'll need to offset the high 20 bits by 1. Look at how an assembler does it by assembling `li s0, 0x1234ABCD`. (Use an assembler + disassembler, or a simulator with a GUI like RARS that can show you the assembled instructions. Or look at compiler output on https://godbolt.org/ for a function that just returns a constant.) – Peter Cordes Mar 16 '23 at 17:04
  • Bytes in registers don't have addresses. You can right-shift and AND to extract 8-bit fields, but there is no "address" involved unless you stored to memory and used `lb` to access parts of the word. (Which would then involve memory endianness.) This is a totally separate issue from creating the value in a register in the first place. – Peter Cordes Mar 16 '23 at 17:13

1 Answers1

1

You need to addi the 0xBCD part.

addi allows for a 12-bit immediate, and so 0xBCD fits — however, it is a signed 12-bit immediate fields, so the value added will be 0xFFFFFBCD

Hence, the lui should offset the 20-bit value 0x1234A by +1, becoming 0x1234B, so that the effective -1 in the upper 20 bits of the sign extended addi will be cancelled out.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53