172k views
1 vote
Write RISC-V assembly code for placing the following immediate constants

in register s7. Use a minimum number of instructions. PLEASE EXPLAIN each and every single step that you used to arrive at the solution! Thank you!
a) 29
b) -214
c) 0xEEEEEFAB
d) 0xEDCBA123

User Kason
by
8.3k points

1 Answer

5 votes

Final answer:

The RISC-V assembly code to load immediate constants into register s7 requires the use of 'addi' for small values and 'lui' followed by 'addi' for larger 32-bit constants.

Step-by-step explanation:

The student's task involves writing RISC-V assembly code to load immediate constants into a specific register, which in this case is s7. Here is how you can do it:

  1. To load the immediate value 29 into s7, a simple addi instruction suffices, because the value is small enough to fit into the immediate field:
  2. addi s7, zero, 29
  3. To load the negative value -214 into s7, use addi with the immediate value directly, as it falls within the range of a 12-bit signed immediate:
  4. addi s7, zero, -214
  5. To load the 32-bit constant 0xEEEEEFAB into s7, you will need to use a pair of instructions since the value is too large for a single immediate. First, use lui to load the upper 20 bits, then use addi to set the lower 12 bits:
  6. lui s7, 0xEEEEF

  7. addi s7, s7, -341
  8. Similarly, to load the constant 0xEDCBA123, use lui and addi in combination:
  9. lui s7, 0xEDCBA

  10. addi s7, s7, 291

For constants c) and d), note that the addi instruction is used to add a signed 12-bit immediate to a register, and thus the lower 12 bits in 0xEEEEEFAB are interpreted as signed while calculating to -341 and the lower 12 bits of 0xEDCBA123 are 291 once signed extension is considered.

User Eloims
by
8.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.