Department of Computer Science National Tsing Hua University CS4100 Computer Architecture
Department of Computer Science National Tsing Hua University CS4100 Computer Architecture
Note: When you check the assembly code of this project, you may see some instructions that start with
a “c.”. These instructions are RISC-V standard compressed instruction set extension. The extension
reduces program code size by adding short 16-bit instruction encodings for common operations. For
example, the instruction c.add rd,rs2 adds the values in registers rd and rs2 and writes the
result to register rd. It expands into add rd,rd,rs2 for normal 32-bit RISC-V instruction
encoding. Since there are only two registers in c.add rd,rs2 (2-address), it is possible to encode
the instruction with 16 bits, thereby reducing the code size. You can read the “RISC-
V_C_Extension_Instruction_Set.pdf” for more details.
Answer the following questions.
(a) (10 points) First, show how the memory address of the gp register is initialized. Next, show how to
get the memory address of the “result” variable by referencing the gp register in “main”. Finally,
find the memory address of the procedure “sudan”.
2. (5 points) Respectively show how the value 14A7CF9Ehex would be arranged in the memory of a little-
endian machine and a big-endian machine. Assume that the machines are byte-addressable and the data
are stored starting at address 0x00000000.
3. (10 points) For each of the following C statements, write the corresponding RISC-V assembly code.
Assume that the base addresses of arrays A and B are in registers x10 and x11, respectively. Each
element of A or B is 8 bytes, and the variables g, h, i, j are assigned to registers x5, x6, x7 and x8,
respectively.
(a) (5 points) B[8] = A[g + h];
(b) (5 points) i = A[B[4]] - j;
4. (10 points) Consider the following code sequence, assuming that LOOP is at memory location1024 10.
What is the binary representation for the 4th instruction (beq) and the 8th instruction (jal)?
LOOP: slli x10,x22,3
add x10,x10,x1
ld x9,0(x10)
beq x9,x24,EXIT
addi x22,x21,2
addi x22,x22,1
addi x23,x23,1
jal x0,LOOP
EXIT:
7. (10 points) Implement the following C code in RISC-V assembly. Note: According to RISC-V spec,
"In the standard RISC-V calling convention, the stack grows downward and the stack pointer is always
kept 16-byte aligned.".
8. (10 points) We would like to expand the RISC-V register file to 128 registers and expand the
instruction set to contain four times as many instructions.
(a) How would this affect the size of each of the bit fields in the R-type instructions?
(b) How could each of the two proposed changes along decrease the size of a RISC-V assembly
program? On the other hand, how could the two proposed changes together increase the size of an
RISC-V assembly program?