chapter 6 Architecture
MIPS Instruction Set — ECS Networking (pacific.edu)
6.1 IntroductionFour design principles:
simplicity favors regularity
make the common case fast
smaller is faster
good design demands good compromises
MIPS Architecture - Microprocessor without interlocked piped stages architecture 簡單易學(xué),但不太實用
6.2.1 Instructions指令add a, b, c
add
mnemonic助記符
a
destination operand目標(biāo)操作數(shù)
b,c
source operands操作數(shù)
簡單設(shè)計:simplicity favors regularity
多條指令
a = b + c - d
→add t, b, c sub a, t, d
加速常見情況:make the common case fast
編碼長度:
RISC(Reduced instruction set computer)
精簡指令集,擁有少量簡單的指令
CISC(Complex instruction set computers)
復(fù)雜指令集
6.2.2 Operands操作數(shù):Registers, Memory, and Constants操作數(shù)的來源:寄存器/內(nèi)存
寄存器
MIPS32有32個32位寄存器;比存儲器快1000倍
越小越快:Smaller is faster
$0 always contains the value 0 because this constant is so frequently used in computer programs.
保存寄存器
臨時變量寄存器
常數(shù)-立即數(shù) 存儲在指令里
存儲器-大但是慢
RISC的指令,只能作用在常數(shù)/寄存器上;MISC還能直接作用到寄存器中
字尋址存儲器:每32位數(shù)據(jù)字對應(yīng)一個唯一的32位地址
MIPS實際使用的是字節(jié)尋址
6.3 Machine Language機器語言三種匯編指令
6.3.1 R-Type Instructions 寄存器類型 6.3.2 I-Type Instructions 立即數(shù)類型 6.3.3 J-Type Instructions 跳轉(zhuǎn)類型地址不是32位的嗎?j類型里addr是26位的??
機器→匯編
6.3.5 The Power of the Stored Program 存儲程序 - 通用計算能力 6.4 Programming編程 6.4.1 Arithmetic/Logical Instructions 算數(shù)/邏輯指令1 邏輯按位操作:
and: 用于掩碼操作(Masking)
or: 用于組合操作(Combining)
nor: 或非,用于反相操作(Inverting)
2 移位指令
3對立即數(shù)操作:
間接加載高位立即數(shù): lui
32-bit constants using load upper immediate (lui) and ori:
4偽指令
nop: 不對處理器執(zhí)行產(chǎn)生影響,僅占用一條指令的時間空間,僅將PC推進到下一條指令
mv: 在寄存器之間傳遞數(shù)據(jù) mv $s0 $s7 → or $s0 $0 $s7
li: 加載立即數(shù)到目標(biāo)寄存器:先判斷位數(shù),在決定多條執(zhí)行指令
5 乘除法:用lo, hi存儲結(jié)果
32*32乘法,得64位結(jié)果
mult $s0, $s1
結(jié)果保存在{hi, lo}
32位除法,得32位商與余數(shù)
div $s0, $s1
商保存在lo, 余數(shù)保存在hi
從lo/hi讀數(shù)
分支發(fā)生:taken;不發(fā)生 not taken
Types of branches:
1 Conditional條件分支
branch if equal (beq)beq $s0, $s1, target
branch if not equal (bne)
2 Unconditional無條件分支 → 跳轉(zhuǎn)指令jump
jump (j) 跳轉(zhuǎn)
jump register (jr) 跳轉(zhuǎn)寄存器
jump and link (jal) 跳轉(zhuǎn)和鏈接
# MIPS assembly
addi $s0, $0, 4 # $s0 = 0 + 4 = 4
addi $s1, $0, 1 # $s1 = 0 + 1 = 1
sll $s1, $s1, 2 # $s1 = 1<< 2 = 4
beq $s0, $s1, target # branch is taken
addi $s1, $s1, 1 # not executed
sub $s1, $s1, $s0 # not executed
target: # label
add $s1, $s1, $s0 # $s1 = 4 + 4 = 8
6.4.3 Conditional Statements 條件1 if
2 if/else
3 switch/case
6.4.4 Getting Loopy 循環(huán)1 while
2 for
3 量值比較
6.4.5 Arrays 數(shù)組1
搞清楚訪問數(shù)據(jù)的大小
char (2bytes)或 longlong (8bytes)
操作字節(jié):
100000 (32) lb rt, imm(rs) load byte [rt] = SignExt ([Address]7:0)
6.4.6 Function Calls 函數(shù)調(diào)用約定:
調(diào)用函數(shù)caller
將參數(shù)傳遞給被調(diào)用者:存在$a0 ~ $a3
中
跳轉(zhuǎn)到被調(diào)用者
jal
被調(diào)用函數(shù)callee
執(zhí)行函數(shù)
返回(結(jié)果)到調(diào)用者:存在$v0 ~ $v1
中
返回到調(diào)用點
不能破壞調(diào)用者的寄存器或內(nèi)存
jr
jal
指令:
將下一條指令的地址存儲到返回地址寄存器$ra中(存下被調(diào)用函數(shù)要返回到的調(diào)用點)
跳轉(zhuǎn)到目標(biāo)指令
001001 (9) | jal label | jump and link 跳轉(zhuǎn)并鏈接 | $ra = PC + 4, PC = JTA |
---|
jr
指令:
把rs的內(nèi)容返回PC,即PC跳到rs存的地址
001000 (8) | jr rs | jump register 跳轉(zhuǎn)到存在rs地址 | PC = [rs] |
---|
int main(){simple();
...
}
void simple() return;
MIPS assembly code
0x00400200 main: jal simple #調(diào)用函數(shù)
0x00400204 ...
...
0x00401020 simple: jr $ra #返回
3 棧但是破壞了調(diào)用函數(shù)的寄存器,因而要把寄存器原來的值存到內(nèi)存中,當(dāng)被調(diào)用函數(shù)返回后,才能夠內(nèi)存中恢復(fù)原來寄存器的值??梢允褂煤筮M先出(LIFO)的“棧”數(shù)據(jù)結(jié)構(gòu)存儲臨時變量。
棧的擴展與縮小:棧指針$sp
開始于高內(nèi)存地址,通過地址遞減來擴展棧空間
在函數(shù)修改寄存器前,將寄存器保存在棧中,返回前從棧中恢復(fù)這些寄存器。
創(chuàng)建棧空間
存儲寄存器
使用寄存器執(zhí)行函數(shù)
從棧中恢復(fù)寄存器原始值
回收??臻g
棧幀stack frame:函數(shù)為自己分配的棧空間
# $s0 = result, $sp為棧頂
diffofsums:
//棧指針減少 3*4 位,用于存儲
addi $sp, $sp, -12 # make space on stack
# to store 3 registers
//保存現(xiàn)場
sw $s0, 8($sp) # save $s0 on stack
sw $t0, 4($sp) # save $t0 on stack
sw $t1, 0($sp) # save $t1 on stack
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
//恢復(fù)現(xiàn)場
lw $t1, 0($sp) # restore $t1 from stack
lw $t0, 4($sp) # restore $t0 from stack
lw $s0, 8($sp) # restore $s0 from stack
//恢復(fù)棧,棧平衡
addi $sp, $sp, 12 # deallocate stack space
//清除痕跡!->計算機安全
jr $ra # return to caller
下圖展示了上述匯編代碼執(zhí)行時棧變化的示意圖
受保護寄存器(preserved):要么被調(diào)用函數(shù)不修改,若修改,則由被調(diào)用函數(shù)維護/ 保存(save)$s0 ~ $s7
存有(調(diào)用函數(shù)的)局部變量,因此必須被調(diào)用函數(shù)保存$ra
存有被調(diào)用函數(shù)要返回的地址,不能修改$sp
棧指針,由被調(diào)用函數(shù)擴展與收縮,被調(diào)用函數(shù)保存
在$sp
之上的??臻g:其他函數(shù)的棧幀,不能修改
不受保護寄存器(nonpreserved):由調(diào)用函數(shù)維護/ 保存(save),可由被調(diào)用函數(shù)任意修改$t0 ~ $t9
存放(調(diào)用函數(shù)的)臨時變量,可由被調(diào)用函數(shù)任意修改,由調(diào)用函數(shù)保存$a0 ~ $a3
一般存調(diào)用函數(shù)要傳給被調(diào)用函數(shù)的參數(shù),一般會由被調(diào)用函數(shù)修改,由調(diào)用函數(shù)保存$v0 ~ $v1
存有被調(diào)用函數(shù)的返回值,會由callee修改,由調(diào)用函數(shù)保存
在$sp
之下的棧空間:callee自己分配的空間,有自己修改,由調(diào)用函數(shù)保存
被調(diào)用函數(shù)必須保存恢復(fù)任何要使用的受保護寄存器,可以隨意改變不受保護寄存器。
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-xcsFeIDo-1669634365736)(image/image_aG9a8E36K4.png)]
5 遞歸函數(shù)調(diào)用High-level code
int factorial(int n) {if (n<= 1)
return 1;
else
return (n * factorial(n-1));
}
MIPS assembly code
// $a0
0x90 factorial: addi $sp, $sp, -8 # make room on stack
0x94 sw $a0, 4($sp) # store $a0
0x98 sw $ra, 0($sp) # store $ra
0x9C addi $t0, $0, 2 # $t0 = 2
0xA0 slt $t0, $a0, $t0 # n<= 1 ?
0xA4 beq $t0, $0, else # no: go to else
0xA8 addi $v0, $0, 1 # yes: return 1
0xAC addi $sp, $sp, 8 # restore $sp
0xB0 jr $ra # return
0xB4 else: addi $a0, $a0, -1 # n = n - 1
0xB8 jal factorial # recursive call
0xBC lw $ra, 0($sp) # restore $ra
0xC0 lw $a0, 4($sp) # restore $a0
0xC4 addi $sp, $sp, 8 # restore $sp
0xC8 mul $v0, $a0, $v0 # n * factorial(n-1)
0xCC jr $ra # return
對齊:MIPS不支持跨邊界訪問lw
對齊4字節(jié)邊界
lb
不需要對齊,因為本身就以字節(jié)為單位
lh
對齊2字節(jié)邊界
The first three modes (register-only, immediate, and base addressing) define modes of reading and writing operands. The last two (PC-relative and pseudo-direct addressing) define modes of writing the program counter, PC.
1 Register-Only Addressing 寄存器尋址Register-only addressing uses registers for all source and destination operands. All R-type instructions use register-only addressing.
2 Immediate Addressing 立即數(shù)尋址Immediate addressing uses the 16-bit immediate along with registers asoperands. Some** I-type instructions**, such as add immediate (addi) and load upper immediate (lui), use immediate addressing.
16位有符號數(shù):$ -2^{15} $~ 2 15 ? 1 2^{15}-1 215?1
對于大立即數(shù):用偽匯編指令
邏輯運算-零擴展
算數(shù)運算-符號擴展
3 Base Addressing 基地址尋址Memory access instructions, such as load word (lw) and store word (sw),use base addressing. The effective address of the memory operand isfound by adding the base address in register rs to the sign-extended16-bit offset found in the immediate field.
基址+符號擴展的立即數(shù)
lw $s4, 72($s0)
地址 = $0 + 72
超大數(shù)組尋址:分步處理
4 PC-Relative Addressing PC相對地址Conditional branch instructions use PC-relative addressing to specify the new value of the PC if the branch is taken. The signed offset in the immediate field is added to the PC to obtain the new PC; hence, the branch destination address is said to be relative to the current PC.
0x10 beq $t0, $0, else
0x14 addi $v0, $0, 1
0x18 addi $sp, $sp, i
0x1C jr $ra
0x20 else: addi $a0, $a0, -1
0x24 jal factorial
若要條很遠(yuǎn)很遠(yuǎn)的地方:中間設(shè)置跳板
5 Pseudo-Direct Addressing 偽直接尋址 6.6 Compiling, Assembling, and Loading編譯、匯編、加載
6.6.1 The Memory Map 內(nèi)存映射轉(zhuǎn)換成二進制代碼(可執(zhí)行文件) 開始執(zhí)行程序
1 Compilation編譯 2 Assembling匯編你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前標(biāo)題:六-體系結(jié)構(gòu)Architecture-創(chuàng)新互聯(lián)
地址分享:http://jinyejixie.com/article22/gpccc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、建站公司、網(wǎng)站設(shè)計、微信小程序、電子商務(wù)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容