0% found this document useful (0 votes)
171 views3 pages

RISC-V Instruction Set Exercises

The document contains exercises related to RISC-V assembly language and computer architecture, focusing on instruction sets, memory operations, and recursive function optimization. It includes questions on data storage in big-endian and little-endian formats, arithmetic operations with overflow considerations, and translating C code into RISC-V assembly. Additionally, it covers loop constructs, stack management, and the implications of tail-call optimization in recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views3 pages

RISC-V Instruction Set Exercises

The document contains exercises related to RISC-V assembly language and computer architecture, focusing on instruction sets, memory operations, and recursive function optimization. It includes questions on data storage in big-endian and little-endian formats, arithmetic operations with overflow considerations, and translating C code into RISC-V assembly. Additionally, it covers loop constructs, stack management, and the implications of tail-call optimization in recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ICT1.

003 – Computer Architecture


Chapter 3: Instruction Set exercises
1. Consider the following code:
lb x6, 0(x7)
sw x6, 8(x7)
Assume that the register x7 contains the address 0×10000000 and the data at address is
0×1122334455667788.
a. What value is stored in 0×10000008 on a big-endian machine?
b. What value is stored in 0×10000008 on a li le-endian machine?
2. Assume that registers x5 and x6 hold the values 0x80000000 and 0xD0000000,
respec vely
a. What is the value of x30 for the following assembly code?
add x30, x5, x6
Is the result in x30 the desired result, or has there been overflow?
b. For the contents of registers x5 and x6 as specified above, what is the value of x30
for the following assembly code?
sub x30, x5, x6
Is the result in x30 the desired result, or has there been overflow?
c. For the contents of registers x5 and x6 as specified above, what is the value of x30
for the following assembly code?
add x30, x5, x6
add x30, x30, x5
Is the result in x30 the desired result, or has there been overflow?
3. Provide a minimal set of RISC-V instruc ons that may be used to implement the
following pseudo instruc on:
not x5, x6 // bit-wise invert
4. For the following C statement, write a minimal sequence of RISC-V assembly instruc ons
that performs the iden cal opera on. Assume x6 = A, and x17 is the base address of C.
A = C[0] << 4;
5. Convert the C code into assembly
a. Implement the following C code in RISC-V assembly. Hint: Remember that the stack
pointer must remain aligned on a mul ple of 16.

int fib(int n){


if (n==0)
return 0;
else if (n == 1)
return 1;
else
return fib(n−1) + fib(n−2);
}

b. For each func on call, show the contents of the stack a er the func on call is made.
Assume the stack pointer is originally at address 0x7ffffffc.
6. Consider the following RISC-V loop:

LOOP: beq x6, x0, DONE


addi x6, x6, -1
addi x5, x5, 2
jal x0, LOOP
DONE:

a. Assume that the register x6 is ini alized to the value 10. What is the final value in
register x5 assuming the x5 is ini ally zero?
b. For the loop above, write the equivalent C code. Assume that the registers x5 and x6
are integers acc and i, respec vely.
c. For the loop wri en in RISC-V assembly above, assume that the register x6 is
ini alized to the value N. How many RISC-V instruc ons are executed?
d. For the loop wri en in RISC-V assembly above, replace the instruc on “beq x6, x0,
DONE” with the instruc on “blt x6, x0, DONE” and write the equivalent C code.
7. Convert from Assembly into C
a. 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, 8
addi x6, x6, 1
blt x6, x29, LOOP

b. Rewrite the loop to reduce the number of RISC-V instruc ons executed. Hint: No ce
that variable i is used only for loop control.
8. Recursive call op miza on:
a. Translate func on f into RISC-V assembly language. Assume the func on declara on
for g is int g(int a, int b). The code for func on f is as follows:

int f(int a, int b, int c, int d){


return g(g(a,b), c+d);
}
b. Can we use the tail-call op miza on in this func on? If no, explain why not. If yes,
what is the difference in the number of executed instruc ons in f with and without
the op miza on?
c. Right before your func on f returns, what do we know about contents of registers
x10-x14, x8, x1, and sp? Keep in mind that we know what the en re func on f
looks like, but for func on g we only know its declara on.

You might also like