Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework#2 CSE120 Spring 2023

Due date: 05/05/2023 11:59 PM PST

Instructions

1. The HW is to be written on physical pages; you may write it up in a word document as well if you prefer. However, in either case, the formulas and equations written/typed should be legible to the grader.

2. Try to be as concise as possible. The HW exercises should not in general require verbose answers but only address the key points

3. For upload, convert into a PDF and upload to Canvas

4. You have 1 week to address your queries in this regrade request form: https://forms.gle/X4dy1mMyfjqwL5857. 1 week after the grades are released, this request form will be closed.

Question 1 (10 pts)

A) Provide a minimal set of RISC-V instructions that may be used to implement the following C code:

a=~b; //bit wise invert (2pts)

B) Disassemble the following RV64I encoded instructions into assembly: (4pts)

(i) 0xfe622a23

(ii) 0xfff20913

C) Assemble the following assembly into encoded RV64I instructions: (4pts)

(i) xori x5, x6, -1

(ii) sd x11, -33(x9)

Question 2 (12 pts)

A) Translate the following C snippets into RV64I assembly. You may assume all variables are already in registers (except for the contents of array a). Each solution should take 4 lines 5 lines or less. Comment each line of your assembly. (4pts)

i) y = a - b + 2*c;

ii) total = total*8 + a[i]; //assume base address of array a, of type doubleword, is in x9

B) Comment the following assembly, and then translate it into 1 line of C: (2pts)

ld x5, 0(x10);

addi x5, x5, 1;

sd x5, 0(x10);

C) Translate the following C snippets into RV64I assembly. Comment each line. (4pts)

i) if (x < 6) x = -1;

ii) if (y <= 4) y = 4;

Question 3 (10 pts)

Translate the following C into RV64I assembly. Be sure to follow the calling (Refer to slides 5-8 of lec07.pptx) and comment each line of your assembly. Remember that long long is 64 bits in size.

Make sure to follow RISC-V function passing conventions as was described in the lectures and Lecture slides.

long long array_total(long long* a, long long n) {

long long total = 0;

for (long long i=0; i

total += a[2*i];

}

return total;

}