HW2 - TCMT - Nhóm A
HW2 - TCMT - Nhóm A
Nhóm A
Tên Thành Viên: Ngô Quang Bình
Đoàn Minh Tuệ
Bùi Trần Diệp Huy
Hồ Văn Tuấn
Phạm Huy
2.3 For the following C statement, write the corresponding RISC-V assembly code.
Assume that the variables f, g, h, i, and j are assigned to registers x5, x6, x7, x28, and x29,
respectively. Assume that the base address of the arrays A and B are in registers x10 and
x11, respectively.
B[8] = A[i−j];
f g h i j Base address
A B
x5 x6 x7 x28 x29 x10 x11
sub x5, x28, x29 //x5 được lưu trữ bởi f=i-j
sll x5, x5, 3 //dịch trái thanh ghi x5 3 bit = f*8
add x5, x10, x5 //x5 = địa chỉ của A[f]=A[i-j]
ld x10,0(x5) // Tải doubleword từ memory đến register x10 = A[i-j]
sd x10, 64(x11)// lưu trữ doubleword từ memory đến register.
2.4
B[g]= A[f] + A[f+1]
slli x30, x5, 3 // x30 = f*8
add x30, x10, x30 // x30 = &A[f]
slli x31, x6, 3 // x31 = g*8
add x31, x11, x31 // x31 = &B[g]
ld x5, 0(x30) // f = A[f]
addi x12, x30, 8 // x12 = &A[f]+8 (i.e. &A[f+1])
ld x30, 0(x12) // x30 = A[f+1]
add x30, x30, x5 // x30 = A[f+1] + A[f]
sd x30, 0(x31) // B[g] = x30 (i.e. A[f+1] + A[f])2.12 R-type: add x1, x1, x1
2.5 [5] <§2.3> Show how the value 0xabcdef12 would be arranged in memory of a little-
endian and a big-endian machine. Assume the data are stored starting at address 0 and
that the word size is 4 bytes.
2.9 For each RISC-V instruction in Exercise 2.8, sh ow the value of the opcode (op),
source register (rs1), and destination register (rd) fields. For the I-type instructions, show
the value of the immediate field, and for the R-type instructions, show the value of the
second source register (rs2). For non U- and UJ-type instructions, show the funct3 field,
and for R-type and S-type instructions, also show the funct7 field.
addi x30, x10, 8
addi x31, x10, 0
sd x31, 0(x30)
ld x30, 0(x30)
add x5, x30, x31
ld x6, 0(x17)
slli x6, x6, 4
2.25.Translate the following C code to RISC-V assembly code. Use
a minimum number of instructions. Assume that the values ofa , b, i, and j are in
registers x5, x6, x7, and x29, respectively. Also, assume that register x10 holdsthe base
address of the array D.
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[4*j] = i + j;
Solutions:
LOOPI:
addi x7, x0, 0
bge x7, x5, ENDI
addi x30, x10, 0
addi x29, x0, 0
LOOPJ:
bge x29, x6, ENDJ
add x31, x7, x29
sd x31, 0(x30)
addi x30, x30, 32
addi x29, x29, 1
jal x0, LOOPJ
ENDJ:
addi x7, x7, 1
jal x0, LOOPI
ENDI:
2.26 T e code requires 13 RISC-V instructions. When a = 10 and b = 1, this results in 123
instructions being executed.
Solutions:
The code requires 13 RISC-V instructions. When a = 10 and b = 1, this
results in 123 instructions being executed.