Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1And in Conclusion\dots

1.1Instruction Translation

Recall that every instruction in RISC-V can be represented as a 32-bit binary value, which encodes the type of instruction, as well as any registers/immediates included in the instruction. To convert a RISC-V instruction to binary, and vice-versa, you can use the steps below. The 61C reference sheet will be very useful for conversions!

RISC-V ⇒ Binary

  1. Identify the instruction type (R, I, I*, S, B, U, or J)

  2. Find the corresponding instruction format

  3. Convert the registers and immediate value, if applicable, into binary

  4. Arrange the binary bits according to the instruction format, including the opcode bits (and possibly funct3/funct7 bits)

Binary ⇒ RISC-V

  1. Identify the instruction using the opcode (and possibly funct3/funct7) bits

  2. Divide the binary representation into sections based on the instruction format

  3. Translate the registers + immediate value

  4. Put the final instruction together based on instruction type/format

Below is an example of a series of RISC-V instructions with their corresponding binary translations.

example.Sexample.bin
main:
addi sp,sp,-4
sw ra,0(sp)
addi s0,sp,4
mv a0,a5
call printf
...
...
11111111110000010000000100010011
00000000000100010010000000100011
00000000010000010000010000010011
00000000000000000000010100010011
00000000010001000000000011101111
...

2Textbook Readings

P&H 2.5, 2.10

3Exercises

Check your knowledge!

3.1Conceptual Review

  1. True or False: In RISC-V, the opcode field of an instruction determines its type (R-Type,S-Type, etc.).

  1. True or False: In RISC-V, the instruction li x5 0x44331416 will always be encoded in 32 bits when translated into binary.

  1. True or False: We can use a branch instruction to move the PC by one byte.

3.2Short Exercises

  1. Convert the following RISC-V registers into their binary representation: