Description
Homework 1
Submit a PDF file on Canvas
1. (5 points) For the following C statement, write the corresponding RISC-V assembly code. Assume that the C variables f, g, and h, have already been placed in registers x28, x29, and x30 respectively. Use a minimal number of RISC-V assembly instructions. f = g + (h − 9);
B[8] = A[i] + A[j];
3. (10 points) Translate the following loop into C. Assume that the C-level integer i is held in register x5, x6 holds the C-level integer called result, and x10 holds the base address of the integer MemArray. addi x6, x0, 0 addi x29, x0, 100
LOOP: lw x7, 0(x10) add x5, x5, x7 addi x10, x10, 4 addi x6, x6, 1 blt x6, x29, LOOP
4. (5 points) Show how the value 0x12345678 would be arranged in memory of a littleendian and a big-endian machine. Assume the data are stored starting at word address 0.
5. (10 points) Assume the following register contents:
x5 = 0x0000AAAA, x6 = 0x12345678
a. For the register values shown above, what is the value of x7 for the following sequence of instructions?
slli x7, x5, 4
b. For the register values shown above, what is the value of x7 for the following sequence of instructions?
srli x7, x5, 3 andi x7, x7, 0xFEF
6. (10 points) Assume x5 holds the value 0x01010000. What is the value of x6 after the following instructions?
bge x5, x0, ELSE jal x0, DONE ELSE: ori x6, x0, 2
DONE: ……
7. Consider the following RISC-V loop:
LOOP: beq x6, x0, DONE addi x6, x6, -1 addi x5, x5, 2
jal x0, LOOP
DONE: ……
(1) (10 points) Assume that the register x6 is initialized to the value 10. What is the final value in register x5 assuming the x5 is initially zero?
(2) (10 points) For the loop above, write the equivalent C code. Assume that the registers x5 and x6 are integers acc and i, respectively.
(3) (5 points) For the loop written in RISC-V assembly above, assume that the register x6 is initialized to the value N. How many RISC-V instructions are executed?
(4) (5 points) For the loop written in RISC-V assembly above, replace the instruction “beq x6, x0, DONE” with the instruction “blt x6, x0, DONE” and write the equivalent C code.
8. (20 points) Translate the following C code to RISC-V assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers x5, x6, x7, and x29, respectively. Also, assume that register x10 holds the base address of the array D. for(i=0; i<a; i++) for(j=0; j<b; j++) D[4 j] = i + j;
Reviews
There are no reviews yet.