1And in Conclusion¶
🎥 Lecture Video
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
Identify the instruction type (R, I, I*, S, B, U, or J)
Find the corresponding instruction format
Convert the registers and immediate value, if applicable, into binary
Arrange the binary bits according to the instruction format, including the opcode bits (and possibly funct3/funct7 bits)
Binary ⇒ RISC-V
Identify the instruction using the opcode (and possibly funct3/funct7) bits
Divide the binary representation into sections based on the instruction format
Translate the registers + immediate value
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.S | example.bin |
|---|---|
main:addi sp,sp,-4sw ra,0(sp)addi s0,sp,4mv a0,a5call printf... | ...1111111111000001000000010001001100000000000100010010000000100011000000000100000100000100000100110000000000000000000001010001001100000000010001000000000011101111... |
2Textbook Readings¶
P&H 2.5, 2.10
3Exercises¶
Check your knowledge!
3.1Conceptual Review¶
True or False: In RISC-V, the opcode field of an instruction determines its type (R-Type,S-Type, etc.).
Solution
True. The opcode field of an instruction uniquely identifies the instruction type and allows us to identify the instruction format we’re working with. The opcode is located in the lowest 7 bits of the machine instruction (bits 0-6).
True or False: In RISC-V, the instruction li x5 0x44331416 will always be encoded in 32 bits when translated into binary.
Solution
False. This is a bit of a trick question. It is true that every regular instruction in RISC-V will always be encoded in 32-bits. However, li is actually a pseudo-instruction! Recall that pseudo-instructionscan translate into one or more RISC-V instructions. In this case, li will be translated into an addi and lui instruction. Therefore, li x5 0x44331416 will actually be encoded in 64-bits, as it represents two RISC-V instructions.
True or False: We can use a branch instruction to move the PC by one byte.
Solution
False. Branch instruction offsets have an implicit zero as the least significant bit, so we can only move the PC in offsets divisible by 2 (refer back to this section for an explanation why this is!). The full offset for a branch instruction will be the 13-bit offset {imm[12:1], 0}, where we take the immediate bits from our instruction’s binary encoding and add the implicit zero.
3.2Short Exercises¶
Convert the following RISC-V registers into their binary representation:
s0spx9t4
Solution
Note that since we have 32 different registers in RISC-V, we need 5 bits to encode them.
Looking at the 61C reference sheet, we can see that s0 refers to the x8 register. To get the final answer, we convert 8 into binary: 0b01000. Following the same procedure as above, we get the rest of the answers...
s0:x9=0b01000sp:x2=0b00010x9:x9=0b01001t4:x29=0b11101