Professional Documents
Culture Documents
Instructor: Steven Ho
Great Idea #1: Levels of
Representation/Interpretation
Higher-Level Language temp = v[k];
Program (e.g. C) v[k] = v[k+1];
v[k+1] = temp;
Compiler
lw t0, 0(x2) We__
Assembly Language lw t1, 4(x2)
are__
Program (e.g. RISCV) sw t1, 0(x2)
sw t0, 4(x2) here._
Assembler
0000 1001 1100 0110 1010 1111 0101 1000
Machine Language 1010 1111 0101 1000 0000 1001 1100 0110 We__
Program (RISCV) 1100 0110 1010 1111 0101 1000 0000 1001 are__
0101 1000 0000 1001 1100 0110 1010 1111 here._
Machine
Interpretation
Hardware Architecture Description
(e.g. block diagrams)
Architecture
Implementation
Logic Circuit Description
(Circuit Schematic Diagrams)
6/27/2018 CS61C Su18 - Lecture 7 2
Agenda
• Stored-Program Concept
• R-Format
• I-Format
• Administrivia
• S-Format
• SB-Format
• U-Format
• UJ-Format
6/27/2018 CS61C Su18 - Lecture 7 3
Big Idea: Stored-Program Concept
rd = t0 = x5
rs1 = t1 = x6
rt2 = t2 = x7
31 0
???
0 ???
7 ???
6 ???
0 ???
5 0x33
???
funct7 rs2 rs1 funct3 rd opcode
6/27/2018 CS61C Su18 - Lecture 7 13
R-Format Example
• RISCV Instruction: add x5,x6,x7
Field representation (decimal):
31 0
0 7 6 0 5 0x33
Field representation (binary):
31 0
0000000 00111 00110 000 00101 0110011
two
15
Agenda
• Stored-Program Concept
• R-Format
• I-Format
• Administrivia
• S-Format
• SB-Format
• U-Format
• UJ-Format
6/27/2018 CS61C Su18 - Lecture 7 16
I-Format Instructions (1/4)
• What about instructions with immediates?
– 5-bit field too small for most immediates
• Ideally, RISCV would have only one instruction
format (for simplicity)
– Unfortunately here we need to compromise
• Define new instruction format that is mostly
consistent with R-Format
– First notice that, if instruction has immediate,
then it uses at most 2 registers (1 src, 1 dst)
6/27/2018 CS61C Su18 - Lecture 7 17
I-Format Instructions (2/4)
• Define “fields” of the following number of bits
each: 12 + 5 + 3 + 5 + 7 = 32 bits
31 0
12 5 3 5 7
• Field names:
31 0
imm[11:0] rs1 func3 rd opcode
• Key Concept: Only imm field is different from
R-format: rs2 and funct7 replaced by
12-bit signed immediate, imm[11:0]
6/27/2018 CS61C Su18 - Lecture 7 18
I-Format Instructions (3/4)
31 0
imm[11:0] rs1 func3 rd opcode
• opcode (7): uniquely specifies the
instruction
• rs1 (5): specifies a register operand
• rd (5): specifies destination register that
receives result of computation
rd = x15
rs1 = x1
31 0
111111001110
??? 00001
??? ?
0 ???
01111 0x13
???
imm[11:0] rs1 func3 rd opcode
6/27/2018 CS61C Su18 - Lecture 7 21
I-Format Example (2/2)
• RISCV Instruction: addi x15,x1,-50
23
Question: If the number of registers were halved,
which statement is true?
29
All RV32 Load Instructions
33
All RV32 Store Instructions
34
Agenda
• Stored-Program Concept
• R-Format
• I-Format
• Administrivia
• S-Format
• SB-Format
• U-Format
• UJ-Format
6/27/2018 CS61C Su18 - Lecture 7 35
Branching Instructions
• beq, bne,bge,blt
– Need to specify an address to go to
– Also take two registers to compare
– Doesn’t write into a register (similar to stores)
• How to encode label, i.e., where to branch to?
• Observations:
– immediate is number of instructions to move
(remember, specifies words) either forward (+) or
backwards (–)
31 7 5 5 3 5 7 0
??????? 01010 10011 000 ????? 1100011
rs2=10 rs1=19 BEQ BRANCH
31 0
0 000000 01010 10011 000 1000 0 1100011
imm[12|10:5] imm[4:1|11]
6/27/2018 CS61C Su18 - Lecture 7 45
RISC-V Immediate Encoding
• Why is it so confusing?!?!
47
Questions on PC-addressing
• Does the value in branch immediate field
change if we move the code?
– If moving individual lines of code, then yes
– If moving all of code, then no (why?)
• What do we do if destination is > 210
instructions away from branch?
– Other instructions save us:
beq x10,x0,far bne x10,x0,next
# next instr → j far
next: # next instr
6/27/2018 CS61C Su18 - Lecture 7 48
Meet the $taff
Sukrit Suvansh
“Help, can’t find my "Do not sushi enter"
Roadside Sign
roadsign” (put picture)
Committing to too
Greatest Weakness Binging TV
many things
56
Agenda
• Stored-Program Concept
• R-Format
• I-Format
• Administrivia
• S-Format
• SB-Format
• U-Format
• UJ-Format
6/27/2018 CS61C Su18 - Lecture 7 57
UJ-Format Instructions (1/3)
• For branches, we assumed that we won’t
want to branch too far, so we can specify a
change in the PC
• For general jumps (jal), we may jump to
anywhere in code memory
– Ideally, we would specify a 32-bit memory address
to jump to
– Unfortunately, we can’t fit both a 7-bit opcode
and a 32-bit address into a single 32-bit word
– Also, when linking we must write to an rd register
6/27/2018 CS61C Su18 - Lecture 7 58
UJ-Format Instructions (2/3)
31 0
imm[20|10:1|11|19:12] rd opcode
20 5 7
offset[31:12] dest JAL
• jal saves PC+4 in register rd (the return address)
• Set PC = PC + offset (PC-relative jump)
• Target somewhere within ±219 locations, 2 bytes apart
• ±218 32-bit instructions
• Reminder: “j” jump is a pseudo-instruction—the assembler
will instead use jal but sets rd=x0 to discard return address
• Immediate encoding optimized similarly to branch instruction
to reduce hardware cost
6/27/2018 CS61C Su18 - Lecture 7 59
UJ-Format Instructions (2/3)
31 0
imm[20|10:1|11|19:12] rd opcode
20 5 7
offset[31:12] dest JAL
• # j pseudo-instruction
• j Label = jal x0, Label # Discard return address
18
• # Call function within 2 instructions of PC
• jal ra, FuncName
61
Uses of jalr
31 0
imm[11:0] rs1 func3 rd opcode
offset base 0 dest JALR
62
Question: When combining two C files into one
executable, we can compile them independently
and then merge them together.
1 2
(A) F F
(B) F T
(C) T F
(D) T T
63
Summary of RISC-V Instruction
Formats
64
Summary
• The Stored Program concept is very powerful
– Instructions can be treated and manipulated the
same way as data in both hardware and software
• RISCV Machine Language Instructions: