lecture-02-Chapter 02 - language of the Computer - 01-sv.pdf. lecture-02-Chapter 02 - language of the Computer - 01-sv.p
COMPUTER ARCHITECTURE
Chapter 2 Lecture 1
Instruction: Language of the Computer
The goals • Bốn nguyên tắc thiết kế cơ bản • Ba toán hạng trong máy tính • Biểu diễn lệnh trong máy tính • Phân loại được lệnh theo 3 định dạng: Rtype, I-Type, J-Type • Phân loại được lệnh theo chức năng • Chuyển đổi lệnh giữa các ngôn ngữ: cấp cao, hợp ngữ, và ngôn ngữ máy. Sep-16
2
Chapter contents 1. Introduction 2. Operations/operands of the Computer Hardware 3. Signed and Unsigned number (review) 4. MIPS Instruction 5. Arithmetic instruction 6. Logical instruction 7. Branch/jump instruction Sep-16
3
References • Slide: • Computer Organization and Design, 4th Edition, Patterson & Hennessy, © 2008, MK • Mary Jane Irwin, Penn State University
• Textbook: Chương 2 (p.74 – p.221) • Software: • Mars / Qtspim
• MIPS reference Data • Resources: • https://uit.edu.vn/~ktmt/mips-tutorial • https://uit.edu.vn/~ktmt/references Sep-16
4
Lecture goals • Giới thiệu về lệnh, tập lệnh • Kiến trúc tập lệnh MIPS • Phép toán/toán hạng trong máy tính • Xem lại số có dấu, số không dấu • Biểu diễn lệnh, phân loại lệnh
Sep-16
5
Lecture contents • Introduction • Operations of the Computer Hardware • Operands of the Computer hardware • Representing Instruction in Computer Hardware • Instruction class
Sep-16
6
§2.1 Introduction
Brainstorming ...
Sep-16
7
Brainstorming...
Computer language Sep-16
8
Definitions • Để ra lệnh cho phần cứng máy tính, cần ngôn ngữ của phần cứng máy tính: • Instruction: lệnh • Instruction set: Tập lệnh, hay có thể gọi là từ vừng của máy tính
Sep-16
Introduction
9
Instruction Set • Tập lệnh của một máy tính • Máy tính khác nhau thì có tập lệnh khác nhau • Nhưng với nhiều khía cạnh là giống nhau
• Máy tính sơ khai thì có tập lệnh đơn giản • Sự thực hiện đơn giản
• Nhiều máy tính hiện đại cũng có tập lệnh đơn giản
Sep-16
Introduction
10
The MIPS Instruction Set • Được sử dụng trong môn học • Cung cấp bởi MIPS Technologies www.mips.com • Chiếm phần lớn trong thị trường chip nhúng • Ứng dụng trong các lĩnh vực electronics, network/storage equipment, cameras, printers, …
Sep-16
Introduction
11
§2.2 Operations of the Computer Hardware
Arithmetic Operations • Add và subtract, có 3 toán hạng • 2 toán hạng nguồn, 1 toán hạng đích
add a, b, c # a = b + c • Tất cả các phép toán đều có dạng như trên.
Sep-16
Operations of the Computer Hardware
12
§2.2 Operations of the Computer Hardware
Arithmetic Operations
There must certainly be instructions for performing the fundamental arithmetic operations. Burks, Goldstine, and von Neumann, 1947
Sep-16
Operations of the Computer Hardware
13
MIPS operands
Sep-16
Operations of the Computer Hardware
14
MIPS assembly language
Sep-16
Operations of the Computer Hardware
15
Arithmetic Example
• C code:
f = (g + h) - (i + j);
Sep-16
Operations of the Computer Hardware
16
Arithmetic Example • Compiled MIPS code: add t0, g, h
# temp t0 = g + h
add t1, i, j
# temp t1 = i + j
sub f, t0, t1
# f = t0 - t1
Sep-16
Operations of the Computer Hardware
17
Design Principle 1 • Simplicity favours regularity • Regularity makes implementation simpler • Simplicity enables higher performance at lower cost
Sep-16
Operations of the Computer Hardware
18
Operands Có ba loại toán hạng: 1. Toán hạng thanh ghi (Register Operand) 2. Toán hạng bộ nhớ
(Memory Operand)
3. Toán hạng trực tiếp (Immediate Operand)
Sep-16
Operands of the Computer Hardware
19
• Các lệnh toán học sử dụng các toán hạng thanh ghi • MIPS có một tập thanh ghi 32 × 32-bit • Sử dụng cho việc truy xuất dữ liệu thường xuyên • Được đánh số 0-31 • 32-bit data called a “word”
• Gọi tên theo chức năng các thanh ghi • $t0, $t1, …, $t9 for temporary values • $s0, $s1, …, $s7 for saved variables
• Design Principle 2: Smaller is faster
Sep-16
Operands of the Computer Hardware
20
§2.3 Operands of the Computer Hardware
Register Operands
MIPS Register File • Holds thirty-two 32-bit registers
• Two read ports and • One write port Registers are
Faster than main memory
src1 addr src2 addr dst addr
Register File 32 bits
5 5 5
32 src1
data 32 locations 32 src2
32
write data - But register files with more locations are slower (e.g., a 64 word file could be as much as 50% slower than a 32 word file) - Read/write port increase impacts speed quadratically
data
write control
Easier for a compiler to use - e.g., (A*B) – (C*D) – (E*F) can do multiplies in any order vs. stack
Can hold variables so that - code density improves (since register are named with fewer bits than a memory location) Sep-16
Representing Instructions in the Computer
21
Bảng các thanh ghi MIPS Register Number
Name $zero $at $v0 - $v1 $a0 - $a3 $t0 - $t7 $s0 - $s7 $t8 - $t9 $gp $sp $fp $ra Sep-16
0 1 2-3 4-7 8-15 16-23 24-25 28 29 30 31
Usage constant 0 (hardware) reserved for assembler returned values arguments temporaries saved values temporaries global pointer stack pointer frame pointer return addr (hardware) Operands of the Computer Hardware
Preserve on call? n.a. n.a. no yes no yes no yes yes yes yes 22
Register Operand Example
• Ví dụ: mã C của lệnh
f = (g + h) - (i + j); • f, …, j in $s0, …, $s4
Sep-16
Operands of the Computer Hardware
23
Register Operand Example
• Biên dịch sang mã MIPS: add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1 Sep-16
Operands of the Computer Hardware
24
Memory Operands • Bộ nhớ chính được sử dụng cho dữ liệu hỗn hợp • Arrays, structures, dynamic data
• Ứng dụng cho các phép toán số học • Nạp dữ liệu từ bộ nhớ • Lưu kết quả từ thanh ghi vào bộ nhớ
• Bộ nhớ được đánh địa chỉ theo byte • Mỗi địa được xác định bởi 1 byte 8bit
• Các word được sắp xếp trong bộ nhớ • Địa chỉ phải chia hết cho 4
• MIPS is Big Endian • Bit cao là bit địa chỉ thấp của 1 từ • c.f. Little Endian: Bit thấp là bit địa chỉ thấp của 1 từ
Sep-16
Operands of the Computer Hardware
25
Memory Operands
Sep-16
Operands of the Computer Hardware
26
Memory Operand Example 1
• Ví dụ: mã lệnh C g = h + A[8]; • g in $s1, h in $s2, base address of A in $s3
Sep-16
Operands of the Computer Hardware
27
Memory Operand Example 1 • Biên dịch sang mã MIPS: • Index 8 requires offset of 32 • 4 bytes per word lw $t0, 32($s3) add $s1, $s2, $t0 offset
Sep-16
base register
Operands of the Computer Hardware
28
Memory Operand Example 2 • C code: A[12] = h + A[8]; • h in $s2, base address of A in $s3
• Compiled MIPS code: • Index 8 requires offset of 32
lw $t0, 32($s3) add $t0, $s2, $t0 sw $t0, 48($s3)
Sep-16
# load word # store word
Operands of the Computer Hardware
29
Registers vs. Memory • Các thanh ghi thì truy xuất nhanh hơn bộ nhớ • Hoạt động trên dữ liệu bộ nhớ yều cầu nạp và lưu dữ liệu • Cần nhiều lệnh hơn để thực thi so với thanh ghi
• Trình biên dịch phải sử dụng các thanh ghi cho các biến nhiều nhất có thể • Chỉ dùng cho bộ nhớ đối với các biến ít sử dụng • Việc tối ưu thanh ghi là quan trọng
Sep-16
Operands of the Computer Hardware
30
Immediate Operands • Các hằng số được sử dụng trong lệnh addi $s3, $s3, 4 • Không có lệnh trừ trực tiếp • Sử dụng lệnh cộng với số âm addi $s2, $s1, -1
• Design Principle 3: Make the common case fast • Các hằng số nhỏ là phổ biến • Các toán hạng trực tiếp sử dụng để tránh việc sử dụng lệnh load
Sep-16
Operands of the Computer Hardware
31
The Constant Zero • Thanh ghi MIPS 0 ($zero) là hằng số zero • Không được ghi
• Hữu dụng cho các phép toán phổ biến • E.g., di chuyển giữa các thanh ghi add $t2, $s1, $zero
Sep-16
Operands of the Computer Hardware
32
§2.4 Signed and Unsigned Numbers
Unsigned Binary Integers • Given an n-bit number
x x n1 2
x n2 2
n2
1
x1 2 x 0 2
0
Range: 0 to +2n – 1 Example
n 1
0000 0000 0000 0000 0000 0000 0000 10112 = 0 + … + 1×23 + 0×22 +1×21 +1×20 = 0 + … + 8 + 0 + 2 + 1 = 1110
Using 32 bits
0 to +4,294,967,295 Sep-16
Review: Signed and Unsigned Numbers
33
Review: Unsigned Binary Representation Hex
Binary
Decimal
0x00000000
0…0000
0
0x00000001
0…0001
1
0x00000002
0…0010
2
0x00000003
0…0011
3
0x00000004
0…0100
4
0x00000005
0…0101
5
0x00000006
0…0110
6
0x00000007
0…0111
7
0x00000008
0…1000
8
0x00000009
0…1001
9
1…1100
0xFFFFFFFD
1…1101
0xFFFFFFFE
1…1110
0xFFFFFFFF
1…1111
Sep-16
...
23 22 21 20
31 30 29
...
3 2 1
1 1 1
...
1 1 1 1 bit
1 0 0 0
...
0
bit weight bit position
0 0 0 0 - 1
232 - 1
… 0xFFFFFFFC
231 230 229
232 - 4 232 - 3 232 - 2 232 - 1 Review: Signed and Unsigned Numbers
34
2s-Complement Signed Integers • Given an n-bit number
x x n1 2
n2
x n2 2
1
x1 2 x 0 2
0
Range: –2n – 1 to +2n – 1 – 1 Example
n 1
1111 1111 1111 1111 1111 1111 1111 11002 = –1×231 + 1×230 + … + 1×22 +0×21 +0×20 = –2,147,483,648 + 2,147,483,644 = –410
Using 32 bits
–2,147,483,648 to +2,147,483,647 Sep-16
Review: Signed and Unsigned Numbers
35
2s-Complement Signed Integers • Bit 31 is sign bit • 1 for negative numbers • 0 for non-negative numbers
• –(–2n – 1) can’t be represented • Non-negative numbers have the same unsigned and 2s-complement representation • Some specific numbers • • • •
0: 0000 0000 … 0000 –1: 1111 1111 … 1111 Most-negative: 1000 0000 … 0000 Most-positive: 0111 1111 … 1111 Sep-16
Review: Signed and Unsigned Numbers
36
Signed Binary Representation
2’sc binary
decimal
-23 =
1000
-8
-(23 - 1) =
1001
-7
1010
-6
1011
-5
1100
-4
1101
-3
1110
-2
1111
-1
0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
complement all the bits 1011
0101 and add a 1
and add a 1 1010
0110
complement all the bits
23 - 1 = Sep-16
Review: Signed and Unsigned Numbers
37
Sign Extension • Biểu diễn một số khi sử dụng nhiều bit hơn • Giữ nguyên giá trị
• Trong tập lệnh MIPS • addi: extend immediate value • lb, lh: extend loaded byte/halfword • beq, bne: extend the displacement
• Nhân rộng các bit dấu bên trái • c.f. unsigned values: extend with 0s
• Examples: 8-bit to 16-bit • +2: 0000 0010 => 0000 0000 0000 0010 • –2: 1111 1110 => 1111 1111 1111 1110 Sep-16
Review: Signed and Unsigned Numbers
38
Review : Evaluating ISAs • Design-time metrics: • Can it be implemented? With what performance, at what costs (design, fabrication, test, packaging), with what power, with what reliability? • Can it be programmed? Ease of compilation?
• Static Metrics: • How many bytes does the program occupy in memory?
• Dynamic Metrics: • How many instructions are executed? How many bytes does the processor fetch to execute the program? CPI • How many clocks are required per instruction? • How "lean" a clock is practical?
Best Metric: Time to execute the program! depends on the instructions set, the processor organization, and compilation techniques. Inst. Count Sep-16
Representing Instructions in the Computer
Cycle Time 39
Two Key Principles of Machine Design 1. Instructions are represented as numbers and, as such, are indistinguishable from data 2. Programs are stored in alterable memory (that canMemory be read or written to) just like data Accounting prg (machine code) Stored-program concept
Programs can be shipped as files of binary numbers – binary compatibility Computers can inherit ready-made software provided they are compatible with an existing ISA – leads industry to align around a small number of ISAs
Sep-16
C compiler (machine code) Payroll
data
Source code in C for Acct prg
Representing Instructions in the Computer
40
MIPS (RISC) Design Principles • Simplicity favors regularity • fixed size instructions • small number of instruction formats • opcode always the first 6 bits
• Smaller is faster • limited instruction set • limited number of registers in register file • limited number of addressing modes
• Make the common case fast • arithmetic operands from the register file (load-store machine) • allow instructions to contain immediate operands
• Good design demands good compromises • three instruction formats Sep-16
Representing Instructions in the Computer
41
MIPS-32 ISA
Registers
• Phân loại theo chức năng • • • •
R0 - R31
Computational Load/Store Jump and Branch Floating Point
PC HI
• coprocessor
LO
• Memory Management • Special
Phân loại theo định dạng lệnh op
rs
rt
op
rs
rt
op
rd
sa
funct
immediate
jump target Sep-16
Representing Instructions in the Computer
R format I format J format 42
MIPS Instruction Fields • Các trường của MIPS op 6 bits
rs 5 bits
rt 5 bits
rd shamt funct 5 bits 5 bits 6 bits
Op: phép toán cơ bản của lệnh, thương được gọi là opcode (mã lệnh) Rt: toán hạngRs : toán hạng thanh ghi thứ nhất nguồn thanh ghi thứ hai. Rd: toán hạng thanh ghi đích. Nơi lưu kết quả của phép toán. Shamt: shit amount Funct: trường này thường được gọi là mã hàm (function code), chọn biến cụ thể của phép toán trong trường mã lệnh. Sep-16
Lecture review
43
Question • Có tất cả bao nhiêu lệnh MIPS được biểu diễn? • Tầm địa chỉ của lệnh I-TYPE? • Tầm địa chỉ của lệnh J-Type?
Sep-16
Lecture review
44
MIPS (RISC) Design Principles Có 4 nguyên tắc thiết kế cơ bản:
1. Simplicity favors regularity 2. Smaller is faster 3. Make the common case fast 4. Good design demands good compromises
Sep-16
Lecture review
45
Operands of the computer hardware Có ba loại toán hạng:
1. Toán hạng thanh ghi (Register Operand) 2. Toán hạng bộ nhớ
(Memory Operand)
3. Toán hạng trực tiếp (Immediate Operand)
Sep-16
Lecture review
46
MIPS Instruction Fields • Các trường của MIPS op 6 bits
rs 5 bits
rt 5 bits
rd shamt funct 5 bits 5 bits 6 bits
Op: phép toán cơ bản của lệnh, thương được gọi là opcode (mã lệnh) Rs : toán hạng thanh ghi thứ nhất Rt: toán hạng nguồn thanh ghi thứ hai. Rd: toán hạng thanh ghi đích. Nơi lưu kết quả của phép toán. Shamt: shit amount Funct: trường này thường được gọi là mã hàm (function code), chọn biến cụ thể của phép toán trong trường mã lệnh. Sep-16
Lecture review
47
Instruction format 3 Instruction Formats: all 32 bits wide op
rs
rt
op
rs
rt
op
rd
sa
immediate
jump target
funct
R format I format J format
R format: Các lệnh có các toán hạng là toán hạng thanh ghi I format: Các lệnh có các toán hạng là toán hạng bộ nhớ J format: Các lệnh nhảy (Jump)
Sep-16
Lecture review
48
MIPS instruction class Instruction class Arithmetic Số học Data transfer Di chuyển dữ liệu Logical Luận lý Conditional branch Rẽ nhánh có điều kiện Jump Nhảy
MIPS example
add, sub, addi lw, sw, lb, lbu, lh, lhu, sb, lui and, or, nor, andi, ori, sll, srl beq, bne, slt, slti, sltiu j, jr, jal FIGURE 2.45, textbook, p.179
Sep-16
Lecture review
49
Next Lecture and Reminder • Next lecture • Instruction: language of the Computer – L02 • Các lệnh số học • Các lệnh luận lý
• Reminder
Sep-16
50