MIPS 汇编指令学习
MIPS comes with 32 general purpose registers named $0. . . $31
Registers also have symbolic names reflecting their conventional8 use:
$0? $zero constant 0
$1? $at used by assembler
$2? $v0 function result
$3? $v1 function result
$4? $a0 argument 1
$5? $a1 argument 2
$6? $a2 argument 3
$7? $a3 argument 4
$8? $t0 unsaved temporary
$9? $t1 unsaved temporary
$10 $t2 unsaved temporary
$11 $t3 unsaved temporary
$12 $t4 unsaved temporary
$13 $t5 unsaved temporary
$14 $t6 unsaved temporary
$15 $t7 unsaved temporary
$16 $s0 saved temporary
$17 $s1 saved temporary
$18 $s2 saved temporary
$19 $s3 saved temporary
$20 $s4 saved temporary
$21 $s5 saved temporary
$22 $s6 saved temporary
$23 $s7 saved temporary
$24 $t8 unsaved temporary
$25 $t9 unsaved temporary
$26 $k0 reserved for OS kernel
$27 $k1 reserved for OS kernel
$28 $gp pointer to global data
$29 $sp stack pointer
$30 $fp frame pointer
$31 $ra return address
寄存器號??????????? 符號名??????????? 用途
?0???????????????? 始終為0???? 看起來象浪費(fèi),其實(shí)很有用
?1???????????????? at????????? 保留給匯編器使用
?2-3?????????????? v0,v1?????? 函數(shù)返回值
?4-7?????????????? a0-a3?????? 前頭幾個函數(shù)參數(shù)
?8-15????????????? t0-t7?????? 臨時寄存器,子過程可以不保存就使用
?24-25???????????? t8,t9?????? 同上
?16-23???????????? s0-s7?????? 寄存器變量,子過程要使用它必須先保存
?????????????????????????????? 然后在退出前恢復(fù)以保留調(diào)用者需要的值
?26,27???????????? k0,k1?????? 保留給異常處理函數(shù)使用
?28??????????????? gp????????? global pointer;用于方便存取全局或者靜態(tài)變量
?29??????????????? sp????????? stack pointer
?30??????????????? s8/fp?????? 第9個寄存器變量;子過程可以用它做frame pointer
?31???????????????? ra???????? 返回地址
硬件上這些寄存器并沒有區(qū)別(除了0號),區(qū)分的目的是為了不同的編譯器產(chǎn)生的代碼可以通用
=========================================
lui 中i表示加載常數(shù)
li r, c:加載16bit或32bit常數(shù)到r
lui r, c:加載16bit常數(shù)到r的高16位load constant halfword c into upper halfword of register r
(translation of pseudo instructions)
偽指令????????????????????? 翻譯的實(shí)際指令
not r, s??????? ==>???????? nor r, s, $0
move r, s?????? ==>???????? or r, s, $0
li r, c???????? ==>???????? ori r, $0, c???? load immediate (c: 16 bit constant)
li r, 0xABCDEF00==>???????? lui $at, 0xABCD和ori r, $at, 0xEF00 (c: 32 bit constant)
and $t0, $t0, 0xFFFFFF00==> lui $at, 0xFFFF
??????????????????????????? ori $at, 0xFF00
??????????????????????????? and $t0, $t0, $at
.ascii s????????????? ASCII encoded characters of string s
.asciiz s???????????? like .ascii, null-terminated
.word w1, w2, . . .?? 32-bit words w1, w2, . . .
.half h1, h2, . . .?? 16-bit halfwords h1, h2, . . .
.byte b1, b2, . . .?? 8-bit bytes b1, b2, . . .
.float f1, f2, . . .? 32-bit single precision floating point numbers f1, f2, . . .
.double d1, d2, . . . 64-bit double precision floating point numbers d1, d2, . . .
.space n????????????? n zero bytes
使用la偽指令訪問數(shù)據(jù)區(qū)
la? $t0, str
lb? $t1, ($t0)? # access byte at address $t0 (’f’)
add $t0, $t0, 3
lb? $t2, ($t0)? # access byte at address $t0 + 3 (’b’)
.data
str: .asciiz "foobar"
load word/halfword/byte at address a into target register r
lw r, a
lh r, a? sign extension
lb r, a? sign extension
lhu r, a no sign extension
lbu r, a no sign extension
store word/halfword/byte in register r at address a
sw r, a
sh r, a stores low halfword
sb r, a stores low byte
Example (copy a sequence of n bytes from address src to address dst):
?????? .text
?????? .globl?? __start
__start:
?????? # length n of byte sequence - 1
?????? li?????? $t0, 5
copy:
?????? lb?????? $t1, src($t0)? # pseudo! (src: 32 bits wide)
?????? sb?????? $t1, dst($t0)
?????? sub????? $t0, $t0, 1
?????? bgez???? $t0, copy
????? .data
src:? .byte 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
dst:? .space 6
=========================================
http://www.mips-in-china.com/Study/ShowArticle.asp?ArticleID=147
mfc0 - move from c0
cfc0 - copy from c0
mfc0??? t0,c0_status
lui???? at,0x1000
ori???? at,at,0x1f
or????? t0,t0,at
xori??? t0,t0,0x1f
mtc0??? t0,c0_statu
于協(xié)處理器CP0的訪問,需要使用特別的指令。這些指令屬于“特權(quán)級指令”,只有在內(nèi)核態(tài)(Kernel Mode)下才能執(zhí)行。如果在用戶態(tài)下,會引起一個異常(Exception)。?
對CP0的主要操作有以下的指令:?
mfc0 rt, rd 將CP0中的rd寄存器內(nèi)容傳輸?shù)絩t通用寄存器;?
mtc0 rt, rd 將rt通用寄存器中內(nèi)容傳輸?shù)紺P0中寄存器rd;?
mfhi/mflo rt 將CP0的hi/lo寄存器內(nèi)容傳輸?shù)絩t通用寄存器中;?
mthi/mtlo rt 將rt通用寄存器內(nèi)容傳輸?shù)紺P0的hi/lo寄存器中;?
當(dāng)MIPS體系結(jié)構(gòu)演進(jìn)到MIPS IV的64位架構(gòu)后,新增了兩條指令dmfc0和dmtc0,向CP0的寄存器中讀/寫一個64bit的數(shù)據(jù)。
r4k MIPS CPU中和異常相關(guān)的控制寄存器(這些寄存器由協(xié)處理器cp0控制,有獨(dú)立的存取方法)有:
????? 1.status 狀態(tài)寄存器
????? 31? 28 27 26 25 24??????? 16 15????????? 8 7 6? 5 4 3? 2?? 1?? 0
???? ------------------------------------------------------------------
???? | cu0-3|RP|FR|RE| Diag Status|?? IM7-IM0? |KX|SX|UX|KSU|ERL|EXL|IE|
???? ------------------------------------------------------------------
???? 其中KSU,ERL,EXL,IE位在這里很重要:
?????? KSU: 模式位 00 -kernel? 01--Supervisor 10--User
?????? ERL: error level,0->normal,1->error
?????? EXL: exception level,0->normal,1->exception,異常發(fā)生是EXL自動置1
?????? IE: interrupt Enable, 0 -> disable interrupt,1->enable interrupt
?????? (IM位則可以用于enbale/disable具體某個中斷,ERL||EXL=1 也使得中斷不能響應(yīng))
????? 系統(tǒng)所處的模式由KSU,ERL,EXL決定:
??????? User mode: KSU = 10 && EXL=0 && ERL=0
?Supervisor mode(never used): KSU=01 && EXL=0 && ERL=0
?Kernel mode: KSU=00 || EXL=1 || ERL=1
????? 2.cause寄存器
?????? 31 30 29 28 27????????? 16 15?????????? 8 7 6????????? 2? 1? 0
????? ----------------------------------------------------------------
????? |BD|0 | CE? |???? 0??????? | IP7 - IP0??? |0|Exc code???? | 0? |
????? ----------------------------------------------------------------
????? 異常發(fā)生時cause被自動設(shè)置
????? 其中:
??????? BD指示最近發(fā)生的異常指令是否在delay slot中
?CE 發(fā)生coprocessor unusable異常時的coprocessor編號(mips有4個cp)
?IP: interrupt pending, 1->pending,0->no interrupt,CPU有6個中斷
???? 引腳,加上兩個軟件中斷(最高兩個)
?Exc code:異常類型,所有的外設(shè)中斷為0,系統(tǒng)調(diào)用為8,...
????? 3.EPC
???????? 對一般的異常,EPC包含:
??? . 導(dǎo)致異常的指令地址(virtual)
??? or. if 異常在delay slot指令發(fā)生,該指令前面那個跳轉(zhuǎn)指令的地址
? 當(dāng)EXL=1時,處理器不寫EPC
????? 4.和存儲相關(guān)的:
??????? context,BadVaddr,Xcontext,ECC,CacheErr,ErrorEPC
?以后再說
????? 一般異常處理程序都是先保存一些寄存器,然后清除EXL以便嵌套異常,
????? 清除KSU保持核心態(tài),IE位看情況而定;處理完后恢復(fù)一些保存內(nèi)容以及CPU狀態(tài)
=========================================
=========================================
=========================================
| MIPS?指令集(共 31條) | ||
|
| ||
| ? |
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
總結(jié)
以上是生活随笔為你收集整理的MIPS 汇编指令学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MIPS Spim
- 下一篇: MIPS 通用寄存器