RISC-V Assembly

The questions below are due on Sunday October 25, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

RISC-V Assembly

You may find the RISC-V ISA handout useful for the following questions. Please assume that all registers are initialized to 0, and the instructions in the section below are executed one after another.
    addi a1, a2, 9

The command above modifies the value stored in which register?

What value is written into the destination register?

    lui a2, 3
    addi a3, x0, 3

What value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) is written into the register a2? 0x

What value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) is written into the register a3? 0x

    sub a3, a3, a1
    slt a4, a3, zero
    sltu a5, a3, zero

What value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) is written into register a3? Note that all values are stored as 2's complement numbers. 0x

What is the value written into the register a4?

What is the value written into the register a5?

    addi a4, zero, 1
    beq a5, zero, L1
    slli a4, a4, 2
L1:
    slli a4, a4, 5

What final value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) will be inside the register a4 after entire code snippet above is executed? 0x

    lui a0, 2
    lw a1, 8(a0)
    addi a2, a1, 4
    sw a2, 0(a0)
    unimp

    . = 0x2000
    .word 0x12345678
    .word 0xDEADBEEF
    .word 0x50505050

Assume that unimp is a special instruction that tells the processor to stop running. The code piece below the . = 0x2000 line just means that

  • the value 0x12345678 resides in the memory location 0x2000
  • the value 0xDEADBEEF resides in the memory location 0x2004
  • the value 0x50505050 resides in the memory location 0x2008

What value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) will be written to register a1 after the execution of the code snippet above? 0x

Which memory location (answer in 32-bit hexadecimal format like 0x0CDEF1AF) will be written to after the execution of the code snippet above? 0x

What value (answer in 32-bit hexadecimal format like 0x0CDEF1AF) will be written to the memory location you found above? 0x

Assembly 2

Consider the following code segment which is independent from the code in the previous section.
    lui a0, 0x2
    addi a7, x0, 0
    lw a1, 0(a0)
L1 :
    andi a2, a1, 1
    beq a2, zero, L2
    addi a7, a7, 1
L2 :
    srli a1, a1, 1
    bne a1, x0, L1
    unimp

    . = 0x2000
    .word 0x12345678

The program seems to be complicated, but we will try to break it down and understand it piece-by-piece. Once again assume that unimp is a special instruction that tells the processor to stop execution when it is reached.

Consider the following code snippet from the program above:

L1 :
    andi a2, a1, 1
    beq a2, zero, L2
    addi a7, a7, 1
L2 :
    srli a1, a1, 1

Suppose register a1 has a value of 0x84263A01 and the register a7 has a value of 0x00000003 before the execution of this code snippet. What will be written to the register a1 after execution of these four lines of code (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

Given the same initial values of register a1 with a value of 0x84263A01 and register a7 with a value of 0x00000003 before the execution of the code snippet, what will be written to register a7 after execution of these four lines of code (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

Now suppose that register a1 has an initial value of 0x84263A00 and while register a7 once again has a value of 0x00000003 before execution of the code snippet. What will be written to the register a1 after execution of these four lines of code (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

What is the value of register a7 after executing this code snippet if register a1 begins with a value of 0x84263A00 and the register a7 begins with a value of 0x00000003 (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

Now let's look back at the main code

    lui a0, 0x2
    addi a7, x0, 0
    lw a1, 0(a0)
    // start of the code piece
L1 :
    andi a2, a1, 1
    beq a2, zero, L2
    addi a7, a7, 1
L2 :
    srli a1, a1, 1
    // end of the code piece
    bne a1, x0, L1
    unimp

    . = 0x2000
    .word 0x12345678

Execution begins with the first instruction lui a0, 0x2 which initializes register a0 to 0x00002000. The next instruction sets register a7 to 0.

What does the lw a1, 0(a0) instruction write into register a1 (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

Now assume that the code runs until it reaches the bne a1, x0, L1 instruction for the first time.

What is the value in the register a1 when the bne a1, x0, L1 instruction is reached for the first time (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

What is the value in the register a7 when the bne a1, x0, L1 instruction is reached for the first time (answer in 32-bit hexadecimal format like 0x0CDEF1AF)? 0x

Now consider the execution of bne a1, x0, L1 for the first time.

What will the execution branch to after executing this instruction?

In which of the following situation will the branching not occur?

Now consider running the code to completion when it reaches the unimp instruction.

How many times will the loop in this code (the loop consists of the andi a2, a1, 1 instruction through the bne a1, x0, L1 instruction) be executed? (answer in positive integer format)

What will be the value inside the register a7 when the execution reaches the unimp instruction? (answer in 32-bit hexadecimal format like 0x0CDEF1AF) 0x

Back to Exercises