Skip to content

Exam-like Problems - Base Conversions

Your worked out solutions are due Mon Sep 29th at 11:59pm You must submit problems.pdf in your lab05 GitHub Repo

Put the solutions to the following problem in a file called problems.pdf in your Lab05 GitHub Repo. You can typeset your solutions or you can write your solutions by hand and scan or take a photo of your work. Just be sure to put your solution in a single file called problems.pdf. If you don't use this EXACT name in all lowercase you won't get credit.

I recommend that you handwrite your solutions because this will give you practice with handwriting on the exam.

Note I will not grade you on correctness in your answers; I'm just looking to see that you attemped the problems and that you show your work.

Question 1 - RISC-V Snippet 1

Assume a0 = 1, a1 = 2, and a2 = 3. What is the value of a2 after executing this snippet:

add a0, a0, a0
add a1, a1, a2
mul a2, a1, a0

Question 2 - RISC-V Snippet 2

Assume a0 = 0, a1 = 3. What is the value of a0 after executing this snippet?

    addi a0, a0, 1
    j boo
foo:
    add a0, a0, a1
    j goo
boo:
    addi a0, a0, 2
    beq a0, a1, foo
goo:
    addi a0, a0, 1

Question 3 - RISC-V Snippet 3

Assume a0 = 0, a1 = 2, and a2 = 0. What is the value of a0 after executing this snippet? How many instructions are executing in the snippet below? Note that labels such as loop: and loopend: do not count as instructions. Also assum beq counts as one instruction whether or not the branch is taken.

    li a3, 0
loop:
    beq a1, a2, loopend
    add a3, a3, a1
    addi a1, a1, -1
    j loop
loopend:
    mv a0, a3

Question 4 - RISC-V Assembly

Consider the following RISC-V assembly code, then answer the following questions.

.global swap_s
.global sort_s

/* sort_s sorts an array of 32-bit integers in-place,
   in asceding order

   a0 - int arr[]
   a1 - int len

   t0 - int i;
   t1 - int j;

*/

sort_s:
    addi sp, sp, -64
    sd ra, (sp)
    li t0, 1

floop:
    bge t0, a1, fdone
    mv t1, t0

wloop:
    ble t1, zero, wdone
    li t3, 4
    mul t4, t1, t3
    add t5, a0, t4
    addi t6, t5, -4
    lw t5, (t5)
    lw t6, (t6)
    ble t6, t5, wdone

    sd a0, 8(sp)
    sd a1, 16(sp)
    sd t0, 24(sp)
    sd t1, 32(sp)

    mv a1, t1
    addi a2, t1, -1
    call swap_s

    ld a0, 8(sp)
    ld a1, 16(sp)
    ld t0, 24(sp)
    ld t1, 32(sp)

    addi t1, t1, -1
    j wloop

wdone:
    addi t0, t0, 1
    j floop

fdone:
    ld ra, (sp)
    add sp, sp, 64
    ret

Which caller-saved registers are preserved in this function, if any?

Which callee-saved registers are preserved in this function, if any?

Are there caller-saved registers that are used but not preserved? If so, why is this okay?

How many bytes of the stack are actually used by this function?

Does this function use pointer-based array access or indexed-based array access?