logo

UTK Notes


Midterm 1

Section I. Numbers and Bases

Question 1

Convert 0o21 into decimal.

2 * 8 + 1 * 1 = 16 + 1 = 17

Question 2

Convert 0x3f24 into binary

0x3f24 = 0011_1111_0010_0100

Question 3

Convert 182 into binary

182 = 128 + 32 + 16 + 4 + 2 = 0b1011_0110

Section II. RISC-V Instructions

Write the assembly instruction for the given C++ code.

Question 1

t0 = sp - 4;

addi t0, sp, -4

Question 2

a0 = *(t0 + 4); [Assume t0 is unsigned short]

lhu a0, 8(t0)

Question 3

s0[4] = gp; [Assume s0 is a signed, 64-bit integer]

sd gp, 32(s0)

Question 4

myfunc(1, 2); [Assume the parameters to my func are both int64_t]

li a0, 1
li a1, 2
call myfunc

Section III. RISC-V ABI

Question 1

The stack pointer must always be a multiple of what value?

16 bytes

Question 2

Write the instruction(s) to allocate and store the RA register on the stack.

somefunc:
# Write the instructions below
   addi    sp, sp, -16
   sd      ra, 0(sp)

Section IV. N.O.S.

Question 1

Write a NOS table for the following structure. Make sure you indicate the anme, offset, size, and total size of the structure.

1
2
3
4
5
struct MyStruct {
    uint8_t op;
    int32_t val1;
    int16_t val2;
};
Name Offset Size
op 0 1
val1 4 4
val2 8 2

Total size: 12 bytes

Section V. Assembly

Question 1

Convert the following C++ into assembly using the MyStruct structure above.

1
2
3
4
long dofunc(const MyStruct &ms) {
    if (ms.op) return ms.val1 / ms.val2;
    else return ms.val1 & ms.val2;
}
.section .text
.global dofunc
dofunc:
    # a0 - const MyStruct &ms
    # t0 - ms.op
    # t1 - ms.val1
    # t2 - ms.val2
    lbu    t0, 0(a0)
    lw     t1, 4(a0)
    lh     t2, 8(a0)

    beqz   t0, 2f

    # If we get here, ms.op != 0, we need /
    div    a0, t1, t2
    ret
2:
    # If we get here, ms.op == 0, we need %
    rem    a0, t1, t2
    ret

COSC230 - Midterm 1 - KEY.pdf