Professional Documents
Culture Documents
Kien-Truc-May-Tinh - Truong-Van-Cuong - Lecture-04-Chapter-02 - Language-Of-The-Computer - 03 - (Cuuduongthancong - Com)
Kien-Truc-May-Tinh - Truong-Van-Cuong - Lecture-04-Chapter-02 - Language-Of-The-Computer - 03 - (Cuuduongthancong - Com)
Kien-Truc-May-Tinh - Truong-Van-Cuong - Lecture-04-Chapter-02 - Language-Of-The-Computer - 03 - (Cuuduongthancong - Com)
Chapter 2
Lecture 3
Instruction: Language of
the Computer
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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.
Sep-16 2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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. MIPS Memory Access Instructions
8. Branch/jump instruction
Sep-16 3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
References
• Slide:
• Computer Organization and Design, 4th Edition, Patterson & Hennessy, ©
2008, MK
• Mary Jane Irwin, Penn State University
• KTMT, ĐH BKTPHCM
• 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
• https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/jump.html
• https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/load32.ht
ml
• https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/pseudojum
p.html
Sep-16 4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Lecture contents
1. MIPS Memory Access Instructions
instruction
2. Logical instruction
Sep-16 5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
MIPS Memory Access Instructions
Register File
Tại sao cần sử dụng các 32 bits
5
lệnh di chuyển dữ liệu src1 addr 32 src1
5 data
giữa bộ nhớ và thanh src2 addr 32
5 locations
ghi? dst addr
32 src2
32
write data data
• MIPS có hai lệnh di chuyển dữ
liệu cơ bản để truy xuất bộ nhớ write control
lw $t0, 24($s3)
35 19 8 2410
Memory
2410 + $s3 = 0xf f f f f f f f
0x120040ac
0x0000000c
0x00000008
0x00000004
0x00000000
Sep-16 1. MIPS Memory Access data
Instructions word address (hex)
10
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Byte Addresses
• Hầu hết các kiến trúc đều đánh địa chỉ từng byte
trong bộ nhớ
• Địa chỉ của 1 word trong bộ nhớ phải chia hết cho 4
• Big Endian: địa chỉ của word là MSB
IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA
• Little Endian: địa chỉ của word là LSB
Intel 80x86, DEC Vax, DEC Alpha (Windows NT)
x..x1101 x..x1101
x..x1100 x..x1100
x..x0111 00000001 x..x0111 01100111
x..x0110 00100011 x..x0110 01000101
x..x0101 01000101 x..x0101 00100011
x..x0100 01100111 x..x0100 00000001
x..x0011 x..x0011
x..x0010 x..x0010
2. Conditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Example
bne $s0, $s1, Lbl #go to Lbl if
$s0$s1
beq $s0, $s1, Lbl #go to Lbl if
$s0=$s1
• Ex: if (i==j) h = i + j;
bne $s0, $s1, Lbl1
add $s3, $s0, $s1
Lbl1: ...
Định dạng lệnh (I format):
0x05 16 17 16 bit offset
2. Conditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Specifying Branch Destinations
• Sử dụng một thanh ghi để cộng thêm 16bit offset-
• Thanh ghi nào? Instruction Address Register
(the PC)
• PC được tự động cập nhật (PC+4) trong chu
kỳ nạp lệnh vì vậy nó giữ địa chỉ của lệnh tiếp
theo
• Giới hạn khoảng rẽ nhánh là -215 to +215-1
2. Conditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Specifying Branch Destinations
00
Địa chỉ rẽ nhánh đích
32 32
Add
PC 32
32 Add
32
32
4
32 ?
2. Conditional branch 2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Aside: More Branch Instructions
• Có thể dung slt, beq, bnevà thanh ghi $zero
để tạo ra các điều kiện khác
• less than blt $s1, $s2, Label
00
32
4
PC 32
3. Unonditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Branching Far Away
• Làm thể nào khi địa chỉ rẽ nhánh nằm ngoài tầm
offset 16 bit
Chèn lệnh jump để có thể “nhảy” xa hơn
beq $s0, $s1, L1
Thành:
bne $s0, $s1, L2
j L1
L2:
3. Unonditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Assignments
• Translating Some Pseudoinstructions
Pseudoinstruction Translation
li $rs, small
li $rs, big
la $rs, big
lw $rt, big($rs)
2. Conditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Compiling Loop Statements
• C code:
while (save[i] == k) i += 1;
• i in $s3, k in $s5, address of save in $s6
• Compiled MIPS code:
2. Conditional branch
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Compiling switch Statement
switch (i) {
case 0: f = i + j; break;
case 1: f = g + h; break;
case 2: f = g – h; break;
case 3: f = i – j; break;
Default: f = 0; break
}
Sep-16 3. Procedures 29
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Instructions for Accessing Procedures
• MIPS: Lệnh gọi thủ tục
jal ProcedureAddress #jump and
link
• Lưu địa chỉ lệnh tiếp theo (PC+4) và nhảy tới hàm
được gọi
• Định dạng lệnh (J format):
0x03 26 bit address
• Nhảy tới địa chỉ lệnh tiếp thep (PC+4) được lưu
trong $ra
jr $ra #return
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Six Steps in Execution of a Procedure
1. Caller đặt các tham số vào nơi mà thủ tục (callee)
có thể truy xuất chúng
• $a0 - $a3: 4 thanh ghi đối số ngõ vào
2. Caller di chuyển sự điều khiển đến callee
3. Callee thu thập các tài nguyên lưu trữ cho thủ tục
4. Callee thực hiện các nhiệm vụ
5. Callee đặt các kết quả vào nơi mà chương trình
có thể truy xuất nó
• $v0 - $v1: hai thanh ghi chứa giá trị trả về
6. Callee trả quyền điều khiển về cho Caller
• $ra: thanh ghi chứa địa chỉ trở về return address
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Leaf Procedure Example
• C code (hàm không gọi các hàm khác):
int leaf_example (int g, h, i, j)
{ int f;
f = (g + h) - (i + j);
return f;
}
• Các đối số g, …, j in $a0, …, $a3
• Kết quả trong $v0
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Leaf Procedure Example
• MIPS code:
leaf_example:
add $t0, $a0, $a1 Procedure body
add $t1, $a2, $a3
sub $v0, $t0, $t1 Result
jr $ra
Return
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Spilling Registers
• Điều gì xảy ra khi sô thanh ghi lớn hơn các đối số
phân bổ và các giá trị trả về ?
• callee sử dụng stack – a last-in-first-out queue (LIFO)
Thanh ghi $sp (29) được sử dụng
high addr
để giữ địa chỉ của stack (stack
“lớn” theo chiều đi xuống)
top of stack $sp
Chèn dữ liệu vào stack– push
$sp = $sp – 4
sw data,0($sp)
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Leaf Procedure Example
• MIPS code:
leaf_example:
addi $sp, $sp, -4
sw $s0, 0($sp) Save $s0 on stack
add $t0, $a0, $a1
Procedure body
add $t1, $a2, $a3
sub $s0, $t0, $t1
add $v0, $s0, $zero Result
lw $s0, 0($sp)
addi $sp, $sp, 4 Restore $s0
jr $ra Return
3. Procedures
CuuDuongThanCong.com https://fb.com/tailieudientucntt