Question: Please answer all questions 5, 5 part 1, 5 part 2, 5 part 3. 5. (30%) Compile C to RISC-V assembly code. Instructions that you
Please answer all questions 5, 5 part 1, 5 part 2, 5 part 3.


5. (30%) Compile C to RISC-V assembly code. Instructions that you might use include: add rd, rs, rs2 #rd=rs1 + rs2 addi rd, rs1, #immediate #rd = rs1 + #immediate sub rd, rs1, rs2 #rd = rs1 - rs2 slli rd, rs1, #immediate # shift left logic rs1 by #immediate number # of bits and store results in rd lw rd #offset(rs1) #load a word from memory at address rs1+#offset to rd sw rs2, #offset(rs) # store a word from rs2 to memory at address rsl + #offset beq rs1, rs2, #label #if rsl =rs2, branch to the instruction labeled as #label bne rs1, rs2, #label #if rs11=rs2, branch to the instruction labeled as #label bge rs1, rs2, #label #if rsl >= rs2, branch to the instruction labeled as #label. 1) (5%) Using ONLY the add, sub and slli instruction to convert the following C statement to the corresponding RISC-V assembly. Assume that the variables f, g, and j are integers assigned to registers to, t1, and t2 respectively. You can use other temporary registers such as t3, t4, t5, t6, etc. f = g * 3 j * 16; 2) (10%) Convert the following statement to its corresponding RISC-V assembly code using arithmetic/logic and load/store instructions. Assume that the base address of array A is in register s 0 and each element is a 4-byte word. i is in register to and f is in register t1. You can use temp registers t2, t3, t4 ... A[i] = A[i-1] + fi 3) (15%) The following C code check whether two arrays (int A[N] and int B[N]) are the same or not. Convert the C code to its corresponding RISC-V assembly code using arithmetic/logic, load and branch instructions. Assume that the base address of array A and B is in register s and si respectively. i is in register to and N's value is already in register t1. You can use temp registers t2, t3, etc. Register x0 always contains 0. The break statement terminates the loop. i = 0; while (A[i] == B[i]) { Page 3 of 5 i++; if (i == N) break; } 5. (30%) Compile C to RISC-V assembly code. Instructions that you might use include: add rd, rs, rs2 #rd=rs1 + rs2 addi rd, rs1, #immediate #rd = rs1 + #immediate sub rd, rs1, rs2 #rd = rs1 - rs2 slli rd, rs1, #immediate # shift left logic rs1 by #immediate number # of bits and store results in rd lw rd #offset(rs1) #load a word from memory at address rs1+#offset to rd sw rs2, #offset(rs) # store a word from rs2 to memory at address rsl + #offset beq rs1, rs2, #label #if rsl =rs2, branch to the instruction labeled as #label bne rs1, rs2, #label #if rs11=rs2, branch to the instruction labeled as #label bge rs1, rs2, #label #if rsl >= rs2, branch to the instruction labeled as #label. 1) (5%) Using ONLY the add, sub and slli instruction to convert the following C statement to the corresponding RISC-V assembly. Assume that the variables f, g, and j are integers assigned to registers to, t1, and t2 respectively. You can use other temporary registers such as t3, t4, t5, t6, etc. f = g * 3 j * 16; 2) (10%) Convert the following statement to its corresponding RISC-V assembly code using arithmetic/logic and load/store instructions. Assume that the base address of array A is in register s 0 and each element is a 4-byte word. i is in register to and f is in register t1. You can use temp registers t2, t3, t4 ... A[i] = A[i-1] + fi 3) (15%) The following C code check whether two arrays (int A[N] and int B[N]) are the same or not. Convert the C code to its corresponding RISC-V assembly code using arithmetic/logic, load and branch instructions. Assume that the base address of array A and B is in register s and si respectively. i is in register to and N's value is already in register t1. You can use temp registers t2, t3, etc. Register x0 always contains 0. The break statement terminates the loop. i = 0; while (A[i] == B[i]) { Page 3 of 5 i++; if (i == N) break; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
