Question

Translate the following C code into RISC-V Assembly. (no need to implement the prints just use...

Translate the following C code into RISC-V Assembly. (no need to implement the prints just use ecall)

int a[] = {2,2,5,3,4,8,3};

int b[] = {1,4,2,0,-1,5,4};

int z[7];

void multiply(int* a, int* b, int* c, int d)

{

for (int i=0; i < d; i++) {

c[i] = a[i] * b[i];

}

}

int main()

{

int n = 7;

multiply(a,b,z,n);

printf("%s", "res: ");

for (int i = 0; i < z; i++)

{

printf("%d", d[i]);

}

printf("\n");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# multiply function starts

multiply:

# Load all the arguments

lw x9, a0 # x9 = a

lw x10, a1 # x10 = b

lw x15, a3 # x15 = c

lw x16, a4 # x16 = d

# initilaze variables

addi x9, x8, 0 # x9=&a[0]

addi x10, x8, 0 # x10=&b[0]

addi x11, x0, 0 # i=0

Loop:

lw x12, 0(x9) # x12=a[i]

lw x13, 0(x10) # x13=b[i]

add x14,x12,x13 # x[14] = a[i]+b[i]

lw 0(x15), x14 # c[i] = x[14]

addi x9,x9,4 # &a[i++]

addi x10,x10,4 # &b[i++]

addi x15,x15,4 # &c[i++]

addi x11,x11,1 # i++

addi x13,x0,x16 # x13=d

blt x11,x13,Loop

# jump to calling function

jr ra

Add a comment
Know the answer?
Add Answer to:
Translate the following C code into RISC-V Assembly. (no need to implement the prints just use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT