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: R- type, 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.
| 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 |
|
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
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, …
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.
MIPS assembly language
• 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
Design Principle 1
• Simplicity favours regularity
• Regularity makes implementation simpler
• Simplicity enables higher performance at lower cost
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)
Register Operands
• 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
MIPS Register File
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
| • 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ừ |
|
| • Ví dụ: mã lệnh C g = h + A[8]; • g in $s1, h in $s2, base address of A in $s3 |
Biên dịch sang mã MIPS: • Index 8 requires offset of 32 • 4 bytes per word |
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 |
|
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
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
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
Unsigned Binary Integers
Review: Unsigned Binary Representation
| • 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? • 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. |
|
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 can Memory be read or written to) just like data 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 |
|
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
MIPS-32 ISA