cs110_disc5
cs110_disc5
CS110 Discussion 5
Suting Chen
RISC-V Instructions
Di erent types
ff
RISC-V Instructions
What are those?
• imm — “Numbers”
RISC-V Instructions
R-type
t3 = t1 + t2 add t3 t1 t2
a1 = a6 - a0 sub a1 a6 a0
a7 += a4 add a7 a7 a4
RISC-V Instructions
R-type — sll, srl, sra
• 0b11001010 — 1
• 0b01100011 — 0
c = (a < b) ? 1 : 0; slt t3 t1 t2
RISC-V Instructions
R-type — Set less than
c = (a < b) ? 1 : 0; slt t3 t1 t2
RISC-V Instructions
R-type — Set less than (unsigned)
c = (a < b) ? 1 : 0; sltu t3 t1 t2
RISC-V Instructions
R-type — Set less than (unsigned)
c = (a < b) ? 1 : 0; sltu t3 t1 t2
RISC-V Instructions
R-type — Conclusion
RISC-V Instructions
I-type
lb rd imm(rs1)
lw rd imm(rs1)
lu rd imm(rs1)
lbu rd imm(rs1)
lhu rd imm(rs1)
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.data
number:
.word 0x01234567 0x89ABCDEF
.text
la t0 number
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0)
lh t1 1(t0)
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.text
la t0 number 89 AB CD EF
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0) 01 23 45 67
lh t1 1(t0)
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.text
la t0 number 89 AB CD EF
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0) 01 23 45 67
lh t1 1(t0)
0x01234567
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.text
la t0 number 89 AB CD EF
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0) 01 23 45 67
lh t1 1(t0)
0x00000067
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.text
la t0 number 89 AB CD EF
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0) 01 23 45 67
lh t1 1(t0)
0x00000001
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu
.text
la t0 number 89 AB CD EF
lw t1 0(t0)
lb t1 0(t0)
lb t1 3(t0) 01 23 45 67
lh t1 1(t0)
0x00002345
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu (Strange cases)
.text
la t0 number
89 AB CD EF
lw t1 3(t0)
lhu t1 5(t0)
lh t1 5(t0)
lbu t1 7(t0)
01 23 45 67
lb t1 7(t0)
0xABCDEF01
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu (Strange cases)
.text
la t0 number
89 AB CD EF
lw t1 3(t0)
lhu t1 5(t0)
lh t1 5(t0)
lbu t1 7(t0)
01 23 45 67
lb t1 7(t0)
0x0000ABCD
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu (Strange cases)
.text
la t0 number
89 AB CD EF
lw t1 3(t0)
lhu t1 5(t0)
lh t1 5(t0)
lbu t1 7(t0)
01 23 45 67
lb t1 7(t0)
0xFFFFABCD
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu (Strange cases)
.text
la t0 number
89 AB CD EF
lw t1 3(t0)
lhu t1 5(t0)
lh t1 5(t0)
lbu t1 7(t0)
01 23 45 67
lb t1 7(t0)
0x00000089
RISC-V Instructions
I-type — lb, lw, lu, lbu, lhu (Strange cases)
.text
la t0 number
89 AB CD EF
lw t1 3(t0)
lhu t1 5(t0)
lh t1 5(t0)
lbu t1 7(t0)
01 23 45 67
lb t1 7(t0)
0xFFFFFF89
RISC-V Instructions
I-type — Conclusion
RISC-V Instructions
S-type
sb rs2 imm(rs1)
sh rs2 imm(rs1)
sw rs2 imm(rs1)
RISC-V Instructions
S-type
89 AB CD EF
01 23 45 67
RISC-V Instructions
S-type
.text
la t0 number 89 AB CD EF
li t1 0x91
sw t1 0(t0)
sw t1 3(t0) 00 00 00 91
sb t1 2(t0)
sh t1 6(t0)
RISC-V Instructions
S-type
.text
la t0 number 89 00 00 00
li t1 0x91
sw t1 0(t0)
sw t1 3(t0) 91 00 00 91
sb t1 2(t0)
sh t1 6(t0)
RISC-V Instructions
S-type
.text
la t0 number 89 00 00 00
li t1 0x91
sw t1 0(t0)
sw t1 3(t0) 91 91 00 91
sb t1 2(t0)
sh t1 6(t0)
RISC-V Instructions
S-type
.text
la t0 number 00 91 00 00
li t1 0x91
sw t1 0(t0)
sw t1 3(t0) 91 91 00 91
sb t1 2(t0)
sh t1 6(t0)
RISC-V Instructions
S-type
RISC-V Instructions
U-type
auipc t0 0x3
lui t0 0x3ab85
rd = PC + imm << 12
t0 = 0x3ab85000
t0 = PC + 0x3 << 12
RISC-V Instructions
U-type
RISC-V Instructions
B-type
0x0 addi x0 x0 0
0x4 blt x0 x0 8 0x0 0x00000013
0x8 addi x0 x0 0 0x4 0x00004463
label_1: 0x8 0x00000013
0xC addi x0 x0 0 0xC 0x00000013
RISC-V Instructions
How labels become immediates?
0 la t0 label_2
1 jalr ra t0 4 # load address
2 nop 0x8 jalr x1 x5 4
3 nop 0xC addi x0 x0 0
label_2: 0x10 addi x0 x0 0
4 nop label_2:
5 nop 0x14 addi x0 x0 0
RISC-V Instructions
Jump And Link Register
• C: Compiler
• A: Assembler
• L: Linker
• L: Loader
• These parts have a demo, so come to the discussion if you need that.
Compiler — Before compiling
The C PreProcessor (cpp)
~ gcc -S main.i
Assembler
~ as main.s -o main.o
~ hexdump -C main.o
~ readelf -a main.o
~ objdump -r main.o
Linker, Loader
fi
fi
Symbol table
fi
What problem should linkers solve?
• Assume that things start at address zero, leave for linker re-arrange.
Cite: https://web.stanford.edu/~ouster/cgi-bin/cs140-winter12/lecture.php?topic=linkers
fi
Question
When does the instruction is nally determined?
C. After link
D. After load
fi