CA L7 Unit2 Slides
CA L7 Unit2 Slides
CA L7 Unit2 Slides
1
Outline: Unit 2
• Instruction Encoding
2
Instruction Set
• Instruction Set
– The vocabulary of instructions a computer can understand
• Examples
– RISC-V
– MIPS
– Intel x86
3
Let’s Wear the Hat of an Instruction Set Designer
4
Let’s Wear the Hat of an Instruction Set Designer
5
Let’s Wear the Hat of an Instruction Set Designer
6
Let’s Wear the Hat of an Instruction Set Designer
7
Let’s Wear the Hat of a Computer Systems Designer
8
RISC-V Instruction Set
9
Categories of RISC-V Instructions
• Arithmetic
• Data Transfer
• Logical
• Shift
• Conditional Branch
• Unconditional Branch
10
Categories of RISC-V Instructions
• Arithmetic
• Data Transfer
• Logical
• Shift
• Conditional Branch
• Unconditional Branch
11
Arithmetic Instructions of RISC-V
12
Arithmetic Instructions of RISC-V
13
Arithmetic Instructions of RISC-V
14
Register Operands
15
Computer Architecture: The Big Picture
Memory
Input Output
(Keyboard,
(LCD, Printer)
Mouse)
Processor
16
Computer Architecture: The Big Picture
Memory
Input Output
(Keyboard,
(LCD, Printer)
Mouse)
x0
x1
x2
…
x31
Processor
17
Register Operands
18
RISC-V Registers
19
Register Operands
20
Register Operand Example
• C code:
– f = (g + h) - (i + j);
• Register Assignment:
– f, g, …, j in x19, x20, …, x23
21
Immediate Operands
22
Design Insights
23
Categories of RISC-V Instructions
• Arithmetic
• Data Transfer
• Logical
• Shift
• Conditional Branch
• Unconditional Branch
24
Need for Data Transfer Instructions
• These composite data structures can contain many more data elements
than there are registers in a computer.
25
Data Transfer Instructions
26
Data Transfer Instructions
• Load Instructions
– Load values from memory into registers
• Store Instructions
– Store values from registers into memory
27
Memory Addressing
• Load Instructions
– Load register, memory address
• Store Instructions
– Store register, memory address
28
Memory Addressing
29
Memory Addressing
30
Memory Addressing
• How many bytes does each address identify
– In RISC-V, each memory address identifies one byte (8 bits)
31
Memory Addressing
• Memory address is described through a base register and offset
– Load register, memoryaddress
– Load register, offset(baseRegister)
• Consider
– char cArray[10];
– int iArray[10];
– Long lArray[10];
32
Memory Addressing
• Consider
char cArray[10];
33
Memory Addressing
• Consider
int iArray[10];
34
Memory Addressing
• Consider
long lArray[10];
35
Memory Addressing
• Consider
long lArray[10];
36
Example
• C code:
– A[12] = h + A[8];
37
Design Insights
38
Computer Architecture: The Big Picture
Memory
Input Output
(Keyboard,
(LCD, Printer)
Mouse)
Processor
39
Design Insights
40
RISC-V: Loading a Byte from Memory into a Register
• lb:
– Sign extend loaded byte
• lbu:
– Zero extend loaded byte
41
Unsigned Binary Integers
• Range: 0 to +2n – 1
• Example
– 0000 0000 … 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
42
Signed Binary Integers: 2’s Complement Representation
x x n 1 2n 1 x n 2 2n 2 x1 21 x 0 20
• Example
– 1111 1111 … 1111 11002
= –1×231 + 1×230 + … + 1×22 +0×21 +0×20
= –2,147,483,648 + 2,147,483,644 = –410
43
2’s Complement Representation: Signed Negation
• Example: negate +2
– +2 = 0000 0000 … 0010two
– –2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two
44
2’s Complement Representation
x x n 1 2n 1 x n 2 2n 2 x1 21 x 0 20
45
RISC-V: Loading a Byte from Memory into a Register
46
Signed or Unsigned Interpretation
47
Signed or Unsigned Interpretation
• Signed and Unsigned Data Types in C (a high-level language)
48
Signed or Unsigned Interpretation
49
Representing a Number using More bits while Preserving
the Numeric Value
50
RISC-V: Loading a Byte from Memory into a Register
• lb:
– Sign extend loaded byte
• lbu:
– Zero extend loaded byte
51
RISC-V: Loading a Halfword from Memory into a Register
• lh:
– Sign extend loaded halfword
• lhu:
– Zero extend loaded halfword
52
Categories of RISC-V Instructions
• Arithmetic
• Data Transfer
• Logical
• Shift
• Conditional Branch
• Unconditional Branch
53
RISC-V: Shift and Logical Instructions
54
Shift Instructions
• Shift Left
– Shift left and fill the emptied bits with 0
– Shift left logical (sll): sll x11, x19, x20
– Shift left logical immediate (slli): slli x11, x19, 4
• Shift Right
– Shift right and fill the emptied bits with 0
– Shift right logical (srl): srl x11, x19, x20
– Shift right logical immediate (srli): srli x11, x19, 4
55
Shift Left
56
Shift Right
57
Shift Right Arithmetic
• Shift right and fill the emptied bits with copies of old sign bit
• Shift right arithmetic (sra): sra x11, x19, x20
• Shift right arithmetic immediate (srai): srai x11, x19, 4
58
AND Operation
• A bit-by-bit operation that leaves a 1 in the result only if both bits of the
operands are 1.
and x9,x10,x11
59
OR Operation
60
XOR Operation
61
NOT Operation
xor x9,x10,x11
62
Categories of RISC-V Instructions
• Arithmetic
• Data Transfer
• Logical
• Shift
• Conditional Branch
• Unconditional Branch
63
Conditional Branches
64
Conditional Branches: Compiling If Statements
• C code:
– if (i==j) f = g+h;
else f = g-h;
• f, g, … in x19, x20, …
65
Conditional Branches: Compiling If Statements
• C code:
– if (i==j) f = g+h;
else f = g-h;
• f, g, … in x19, x20, …
• C code:
– while (save[i] == k)
i += 1;
67
More Conditional Branches
68
More Conditional Branches: Example
• C code:
– if (a > b)
a += 1;
• a in x22, b in x23
69
More Conditional Branches: Elaborating Signed vs Unsigned
• Example
– x22 = 1111 1111 1111 1111 1111 1111 1111 1111
– x23 = 0000 0000 0000 0000 0000 0000 0000 0001
70
RISC-V: Procedure Calling
• Steps required
– Place parameters in registers x10 to x17
71
RISC-V: Procedure Call Instructions
72
RISC-V: Procedure Calling (Summary v1)
73
RISC-V Procedure Calling: Introducing Stack
74
RISC-V Procedure Calling: Introducing Stack
75
RISC-V Procedure Calling: Introducing Stack
“Push” “Pop”
78
RISC-V: Memory Layout Convention
• Text
– program machine code
• Static data
– global variables e.g., static variables in C, constant arrays and strings
– x3 (global pointer) initialized to the first address of this segment, allowing ±offsets into
this segment
• Dynamic data (Heap)
– Data structures like linked list that grow and shrink during their lifetime
– malloc in C, new in Java
• Stack
– automatic variables
79
RISC-V Procedure Calling: Utilizing Stack
• This convention means that you push values onto the stack by subtracting from
the stack pointer. Adding to the stack pointer shrinks the stack, thereby popping
values off the stack. 80
RISC-V Procedure Calling: Introducing Stack
81
RISC-V Procedure Calling: Utilizing Stack
82
RISC-V Procedure Calling: Introducing Stack
83
RISC-V Procedure Calling: Utilizing Stack Efficiently
84
RISC-V Procedure Calling: Utilizing Stack Efficiently
• To avoid saving and restoring a register whose value is never used, RISC-V
introduces two groups of registers
85
RISC-V Procedure Calling: Utilizing Stack Efficiently
86
RISC-V Procedure Calling: Utilizing Stack
87
RISC-V Procedure Calling: Utilizing Stack
“Push” “Pop”
88
RISC-V Procedure Calling: Utilizing Stack Efficiently
• To avoid saving and restoring a register whose value is never used, RISC-V
introduces two groups of registers
89
RISC-V: Procedure Calling (Summary v2)
90
RISC-V Procedure Calling: Non-Leaf Procedures
91
RISC-V Procedure Calling: Non-Leaf Procedures
• Solution:
– The caller pushes any argument registers (x10–x17) that are needed after the call.
– The callee pushes the return address register x1.
92
RISC-V: Procedure Calling (Summary v3)
93
RISC-V: Procedure Calling (Final Summary)
94
RISC-V Procedure Calling: Utilizing Stack
100
Instruction Encoding
101
Hexadecimal: Review
• Base 16
– Compact representation of bit strings
– 4 bits per hex digit
102
RISC-V R-format Instructions
• Instruction fields
– opcode: operation code
– rd: destination register number
– funct3: 3-bit function code (additional opcode)
– rs1: the first source register number
– rs2: the second source register number
– funct7: 7-bit function code (additional opcode)
103
RISC-V I-format Instructions
• Instruction fields
– opcode: operation code
– rd: destination register number
– funct3: 3-bit function code (additional opcode)
– rs1: source or base address register number
– Immediate: constant operand, or offset added to base address (2’s
complement, sign extended)
104
RISC-V S-format Instructions
• Instruction fields
– opcode: operation code
– funct3: 3-bit function code (additional opcode)
– rs1: base address register number
– rs2: source operand register number
– immediate: offset added to base address (Split so that rs1 and rs2 fields always
in the same place across various instruction formats)
105
RISC-V Instruction Encoding Format
• R-format
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
• I-format
immediate rs1 funct3 rd opcode
12 bits 5 bits 3 bits 5 bits 7 bits
• S-format
106
RISC-V Instructions & their Encoding Format
107
Instruction Encoding Example
108
Instruction Encoding Example
109
Instruction Encoding Example
110