Starting from:

$30

VE370 Homework 1 -Solved

1. 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); 

2.  Translate the following C code to RISC-V. Assume that the variables f, g, h, i, and j are assigned to registers x5, x6, x7, x28, and x29, respectively. Assume that the base address of the arrays A and B are in registers x10 and x11, respectively. Assume that the elements of the arrays A and B are 4-byte words:

B[8] = A[i] + A[j]; 

3. 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.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.  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. 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)  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)  For the loop above, write the equivalent C code. Assume that the registers x5 and x6 are integers acc and i, respectively.

(3) 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) 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. 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; 

More products