archi12Assembleur2
archi12Assembleur2
archi12Assembleur2
Assembly Language
• Instructions: commands in a computer’s
language
– Assembly language: human-readable format of
instructions
– Machine language: computer-readable format
(1’s and 0’s)
Architecture • RISC-V architecture:
– Developed by Krste Asanovic, David Patterson
and their colleagues at UC Berkeley in 2010.
– First widely accepted open-source computer
architecture
1
12/4/2022
5 Digital Design & Computer Architecture Architecture 6 Digital Design & Computer Architecture Architecture
7 Digital Design & Computer Architecture Architecture 8 Digital Design & Computer Architecture Architecture
2
12/4/2022
Design Principle 2
Make the common case fast
• RISC-V includes only simple, commonly used
instructions
• Hardware to decode and execute instructions can
be simple, small, and fast
• More complex instructions (that are less common)
Operands
performed using multiple simple instructions
• RISC-V is a reduced instruction set computer (RISC),
with a small number of simple instructions
• Other architectures, such as Intel’s x86, are complex
instruction set computers (CISC)
11 Digital Design & Computer Architecture Architecture 12 Digital Design & Computer Architecture Architecture
3
12/4/2022
13 Digital Design & Computer Architecture Architecture 14 Digital Design & Computer Architecture Architecture
4
12/4/2022
17 Digital Design & Computer Architecture Architecture 18 Digital Design & Computer Architecture Architecture
Operands: Memory
• Too much data to fit in only 32 registers
• Store more data in memory
• Memory is large, but slow
5
12/4/2022
6
12/4/2022
Byte Address
# write t7 into address 16
27 Digital Design & Computer Architecture Architecture 28 Digital Design & Computer Architecture Architecture
7
12/4/2022
Logical Instructions
• and, or, xor
– and: useful for masking bits
• Masking all but the least significant byte of a value:
0xF234012F AND 0x000000FF = 0x0000002F
31 Digital Design & Computer Architecture Architecture 32 Digital Design & Computer Architecture Architecture
8
12/4/2022
Multiplication Division
32 × 32 mulKplicaKon → 64 bit result 32-bit division → 32-bit quotient & remainder
35 Digital Design & Computer Architecture Architecture 36 Digital Design & Computer Architecture Architecture
9
12/4/2022
Branching
• Execute instructions out of sequence
• Types of branches:
– Conditional
• branch if equal (beq)
39 Digital Design & Computer Architecture Architecture 40 Digital Design & Computer Architecture Architecture
10
12/4/2022
target:
s1, s1, s0 # not executed
Conditional
add s1, s1, s0 # s1 = 1 + 4 = 5
43 Digital Design & Computer Architecture Architecture 44 Digital Design & Computer Architecture Architecture
11
12/4/2022
45 Digital Design & Computer Architecture Architecture 46 Digital Design & Computer Architecture Architecture
47 Digital Design & Computer Architecture Architecture 48 Digital Design & Computer Architecture Architecture
12
12/4/2022
49 Digital Design & Computer Architecture Architecture 50 Digital Design & Computer Architecture Architecture
Arrays
• Access large amounts of similar data
• Index: access each element
• Size: number of elements
Arrays
13
12/4/2022
53 Digital Design & Computer Architecture Architecture 54 Digital Design & Computer Architecture Architecture
Accessing Arrays Using For Loops Accessing Arrays Using For Loops
// C Code # RISC-V assembly code
int array[1000]; # s0 = array base address, s1 = i
int i; # initialization code
lui s0, 0x23B8F # s0 = 0x23B8F000
ori s0, s0, 0x400 # s0 = 0x23B8F400
for (i=0; i < 1000; i = i + 1)
addi s1, zero, 0 # i = 0
array[i] = array[i] * 8;
addi t2, zero, 1000 # t2 = 1000
55 Digital Design & Computer Architecture Architecture 56 Digital Design & Computer Architecture Architecture
14
12/4/2022
15
12/4/2022
61 Digital Design & Computer Architecture Architecture 62 Digital Design & Computer Architecture Architecture
63 Digital Design & Computer Architecture Architecture 64 Digital Design & Computer Architecture Architecture
16
12/4/2022
Input Arguments & Return Value Input Arguments & Return Value
C Code RISC-V assembly code
int main() # s7 = y
main:
{
. . .
int y; addi a0, zero, 2 # argument 0 = 2
... addi a1, zero, 3 # argument 1 = 3
y = diffofsums(2, 3, 4, 5); // 4 arguments addi a2, zero, 4 # argument 2 = 4
addi a3, zero, 5 # argument 3 = 5
... jal diffofsums # call function
} add s7, a0, zero # y = returned value
. . .
int diffofsums(int f, int g, int h, int i) # s3 = result
{ diffofsums:
int result; add t0, a0, a1 # t0 = f + g
add t1, a2, a3 # t1 = h + i
result = (f + g) - (h + i);
sub s3, t0, t1 # result = (f + g) − (h + i)
return result; // return value add a0, s3, zero # put return value in a0
} jr ra # return to caller
65 Digital Design & Computer Architecture Architecture 66 Digital Design & Computer Architecture Architecture
Preserved Nonpreserved
Callee-Saved Caller-Saved
s0-s11 t0-t6
sp a0-a7 Machine Language
ra
stack above sp stack below sp
17
12/4/2022
Machine Language:
funct7 rs2 rs1 funct3 rd op funct7 rs2 rs1 funct3 rd op
add s2, s3, s4
add x18,x19,x20 0 20 19 0 18 51 0000 000 10100 10011 000 10010 011 0011 (0x01498933)
sub t0, t1, t2 32 7 6 0 5 51 0100 000 00111 00110 000 00101 011 0011 (0x407302B3)
sub x5, x6, x7
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits 7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
More Formats
18
12/4/2022
I-Type
31:20 19:15 14:12 11:7 6:0
imm11:0 rs1 funct3 rd op
12 bits 5 bits 3 bits 5 bits 7 bits
73 Digital Design & Computer Architecture Architecture 74 Digital Design & Computer Architecture Architecture
S/B-Type S-Type
• Store-Type • Store-Type
• 3 operands:
• Branch-Type – rs1: base register
• Differ only in immediate encoding – rs2: value to be stored to memory
– imm: 12-bit two’s complement immediate
• Other fields:
– op: the opcode
– Simplicity favors regularity: all instructions have opcode
31:25 24:20 19:15 14:12 11:7 6:0
– funct3: the function (3-bit function code)
imm11:5 rs2 rs1 funct3 imm4:0 op S-Type – with opcode, tells computer what operation to perform
imm12,10:5 rs2 rs1 funct3 imm4:1,11 op B-Type
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits S-Type
31:25 24:20 19:15 14:12 11:7 6:0
imm11:5 rs2 rs1 funct3 imm4:0 op
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
75 Digital Design & Computer Architecture Architecture 76 Digital Design & Computer Architecture Architecture
19
12/4/2022
B-Type
31:25 24:20 19:15 14:12 11:7 6:0
imm12,10:5 rs2 rs1 funct3 imm4:1,11 op
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
77 Digital Design & Computer Architecture Architecture 78 Digital Design & Computer Architecture Architecture
79 Digital Design & Computer Architecture Architecture 80 Digital Design & Computer Architecture Architecture
20
12/4/2022
81 Digital Design & Computer Architecture Architecture 82 Digital Design & Computer Architecture Architecture
21
12/4/2022
85 Digital Design & Computer Architecture Architecture 86 Digital Design & Computer Architecture Architecture
22
12/4/2022
89 Digital Design & Computer Architecture Architecture 90 Digital Design & Computer Architecture Architecture
31 31 31 31 30:25 11:8 7 S
31 31 31 30 29:25, 11 10:7 0 B
31
31
30:20
31
19:12
19:12
0 0
20 21:16
0
15:12
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 U
0 J Addressing Operands
Immediate Bits
91 Digital Design & Computer Architecture Architecture
23
12/4/2022
95 Digital Design & Computer Architecture Architecture 96 Digital Design & Computer Architecture Architecture
24
12/4/2022
97 Digital Design & Computer Architecture Architecture 98 Digital Design & Computer Architecture Architecture
5 bits
001
3 bits
op
0010 0 110 0011 (0xCB8C9263)
5 bits 7 bits
Programs
99 Digital Design & Computer Architecture Architecture
25
12/4/2022
101 Digital Design & Computer Architecture Architecture 102 Digital Design & Computer Architecture Architecture
Linker
• How big is memory?
– At most 232 = 4 gigabytes (4 GB)
Executable
– From address 0x00000000 to 0xFFFFFFFF
Loader
Memory
103 Digital Design & Computer Architecture Architecture 104 Digital Design & Computer Architecture Architecture
26
12/4/2022
Global Data gp
void main() {
0x10000000 f = 2;
g = 3;
Text y = func(f,g);
0x00008000 return;
}
Exception
Handlers
0x00000000 pc
105 Digital Design & Computer Architecture Architecture 106 Digital Design & Computer Architecture Architecture
107 Digital Design & Computer Architecture Architecture 108 Digital Design & Computer Architecture Architecture
27
12/4/2022
0xc4e1aa23
00011a30 g O .bss 00000004 f
0x000115E0
0x00300713 Address of main:
0xc4e1a823
00011a34 g O .bss 00000004 g 0x00008067
0x01010113
0x00200713
0x10180
0x00c12083
109 Digital Design & Computer Architecture Architecture 110 Digital Design & Computer Architecture Architecture
28