1. 寄存器種類
2. 算術及尋址指令
3. 程序結構
4. 系統調用
需要使用的工具是:Mars4.4
下載地址:
http://courses.missouristate.edu/KenVollmar/mars/download.htm
1 數據類型
1. 所有MIPS指令都是32位長
2. 1字節 = 8位,半字長 = 2個字節,1字長 = 4個字節
3. 一個字符空間 = 1個字節
4. 一個整型 = 一個字長 = 4個字節
5. 單個字符用單引號,例如:'b'
6. 字符串用雙引號,例如:"A string"
2 寄存器
1. MIPS下一共有32個通用寄存器
2. 在匯編中,寄存器標志由$符開頭
3. 寄存器表示可以有兩種方式
直接使用該寄存器對應的編號,例如:從0到31
使用對應的寄存器名稱,例如:t1,sp(詳細名稱見下表)
4. Lo和Hi寄存器專門用來存儲乘法和除法的結果
對于以上兩者,不存在直接尋址;必須要通過特殊指令mfhi ("move from Hi") 和 mflo ("move from Lo")來訪問內容
5. 棧的走向是高地址到低地址
3 程序結構
1. 本質其實就是數據聲明 + 普通文本 + 程序編碼(文件擴展名為 .s或者 .asm都可以)
2. 數據聲明在代碼段之后(其實在之前也沒啥問題,也更符合高級程序的設計習慣)
數據聲明:
1. 數據段以.data為開始標志
2. 聲明變量后,即在主存中分配空間
代碼:
1. 代碼段以 .text為開始的標志
2. 其實就是各項指令操作
3. 程序入口為main:標志(這個都一樣)
4. 程序結束標志
注釋:
1. 是注釋符
2. MIPS程序的基本模版如下:
# Comment giving name of program and description of function# 說明下程序的目的和作用(其實和高級語言都差不多了)# Template.s# Bare-bones outline of MIPS assembly language program .data # variable declarations follow this line # 數據變量聲明 # ... .text # instructions follow this line # 代碼段部分 main: # indicates start of code (first instruction to execute) # 主程序 # ...# End of program, leave a blank line afterwards to make SPIM happy# 必須多給你一行,你才歡?
4 數據聲明
聲明的格式:
name: storage_type value(s)
通常給變量賦一個初始值;對于.space,需要指明需要多少大小空間(bytes)
注意:name后面始終要跟著冒號
examplevar1: .word 3 # create a single integer variable with initial value 3# 聲明一個 word 類型的變量 var1, 同時給其賦值為 3array1: .byte 'a','b'# create a 2-element character array with elements initialized# to a and b# 聲明一個存儲2個字符的數組 array1,并賦值 'a', 'b'array2: .space 40 # allocate 40 consecutive bytes, with storage uninitialized# could be used as a 40-element character array, or a# 10-element integer array; a comment should indicate which! # 為變量 array2 分配 40字節(bytes)未使用的連續空間,當然,對于這個變量# 到底要存放什么類型的值, 最好事先聲明注釋下!
5 加載保存【讀取寫入】
1. 如果要訪問內存,不好意思,你只能用 load 或者 store 指令
2. 其他的只能都一律是寄存器操作
load: lw register_destination, RAM_source # copy word (4 bytes) at source RAM location to destination register. # 從內存中 復制 RAM_source 的內容到 對應的寄存器中(lw中的'w'意為'word',即該數據大小為4個字節) lb register_destination, RAM_source # copy byte at source RAM location to low-order byte of destination register,and sign-extend to higher-order bytes # 同上, lb 意為 load bytestore word: sw register_source, RAM_destination # store word in source register into RAM destination # 將指定寄存器中的數據 寫入到指定的內存中 sb register_source, RAM_destination # store byte (low-order) in source register into RAM destinationload immediate: li register_destination, value # load immediate value into destination register
舉例
.data var1: .word 23 # declare storage for var1; initial value is 23 .text __start: lw $t0, var1 # load contents of RAM location into register $t0: $t0 = var1 li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, var1 # store contents of register $t1 into RAM: var1 = $t1 done
6 立即與間接尋址
load address: la $t0, var1 # 將var1的RAM地址復制到寄存器$t0中indirect addressing: lw $t2, ($t0) #將$t0中包含的RAM地址的值加載到$t2中 sw $t2, ($t0) # 將寄存器$ t2中的值存儲到$ t0中包含的地址的RAM中based or indexed addressing: lw $t2, 4($t0) # 將RAM地址($t0+4)的值存到$t2寄存器 sw $t2, -12($t0) # 將$t2寄存器的值存到地址($t0-12)中
不必多說,要用到偏移量的尋址,基本上使用最多的場景無非兩種:數組,棧。
.data array1: .space 12 # declare 12 bytes of storage to hold array of 3 integers # 定義一個 12字節 長度的數組 array1, 容納 3個整型 .text __start: la $t0, array1 # load base address of array into register $t0 # 讓 $t0 = 數組首地址 li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, ($t0) # first array element set to 5; indirect addressing # 對于 數組第一個元素賦值 array[0] = $1 = 5 li $t1, 13 # $t1 = 13 sw $t1, 4($t0) # second array element set to 13 # 對于 數組第二個元素賦值 array[1] = $1 = 13 # (該數組中每個元素地址相距長度就是自身數據類型長度,即4字節, 所以對于array+4就是array[1]) li $t1, -7 # $t1 = -7 sw $t1, 8($t0) # third array element set to -7 # 同上, array+8 = (address[array[0])+4)+ 4 = address(array[1]) + 4 = address(array[2])
7 算術指令集
1. 最多3個操作數
2. 在這里,操作數只能是寄存器,絕對不允許出現地址
3. 所有的指令統一是32位 = 4 * 8 bit = 4 bytes = 1 word
add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as signed (2's complement) integers sub $t2,$t3,$t4 # $t2 = $t3, $t4 addi $t2,$t3, 5 # $t2 = $t3 + 5; "add immediate" (no sub immediate) addu $t1,$t6,$t7 # $t1 = $t6 + $t7; add as unsigned integers subu $t1,$t6,$t7 # $t1 = $t6 + $t7; subtract as unsigned integers mult $t3,$t4 # multiply 32-bit quantities in $t3 and $t4, and store 64-bit # result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4 p $t5,$t6 # Lo = $t5 / $t6 (integer quotient) # Hi = $t5 mod $t6 (remainder) mfhi $t0 # move quantity in special register Hi to $t0: $t0 = Hi mflo $t1 # move quantity in special register Lo to $t1: $t1 = Lo # used to get at result of product or quotient move $t2,$t3 # $t2 = $t3
8 控制流
branches分支(if else系列)
指令內置了分支條件的比較:
b target # unconditional branch to program label targetbeq $t0,$t1,target # branch to target if $t0 = $t1blt $t0,$t1,target # branch to target if $t0 < $t1ble $t0,$t1,target # branch to target if $t0 <= $t1bgt $t0,$t1,target # branch to target if $t0 > $t1bge $t0,$t1,target # branch to target if $t0 >= $t1bne $t0,$t1,target # branch to target if $t0 <> $t1
Jumps跳轉(while,for,goto系列)
j target # unconditional jump to program label target # 看到就跳轉,不用考慮任何條件 jr $t3 # jump to address contained in $t3 ("jump register") # 類似相對尋址,跳到該寄存器給出的地址處
子程序調用 subroutine return: "jump register" instruction
jr $ra # "jump register"
跳轉到寄存器$ra中保存的返回地址(由jal指令存儲)
如果說調用的子程序中有調用了其他子程序,如此往復, 則返回地址的標記就用棧(stack)來存儲, 畢竟 $ra 只有一個,(哥哥我分身乏術啊)
9 系統調用和輸入/輸出
1. 通過系統調用實現終端的輸入輸出,以及聲明程序結束
2. 學會使用 syscall
3. 參數所使用的寄存器:v0, a0,
4. 返回值使用:$v0
大概意思是要打印的字符串應該有一個終止符,估計類似C中的'', 在這里我們只要聲明字符串為 .asciiz 類型即可。下面給個我用Mars4.4的提示:
1. 對于讀取整型, 浮點型,雙精度的數據操作, 系統會讀取一整行,(也就是說以換行符為標志 'n')
2. read_string和fgets類似
舉例 打印一個存儲在寄存器$2里的整型:
Print out integer value contained in register $t2li $v0, 1 # load appropriate system call code into register $v0; # 聲明需要調用的操作代碼為 1 (print_int) 并賦值給 $v0 # code for printing integer is 1move $a0, $t2 # move integer to be printed into $a0: $a0 = $t2 # 將要打印的整型賦值給 $a0syscall # call operating system to perform operation
舉例 讀取一個數,并且存儲到內存中的 int_value 變量中:
Read integer value, store in RAM location with label int_value (presumably declared in data section)li $v0, 5 # load appropriate system call code into register $v0; # code for reading integer is 5syscall # call operating system to perform operationsw $v0, int_value # value read from keyboard returned in register $v0; # store this in desired location
舉例 打印一個字符串(這是完整的,其實上面栗子都可以直接替換main: 部分,都能直接運行
.datastring1 .asciiz "Print this.n"# declaration for string variable, # .asciiz directive makes string null terminated .text main: li $v0, 4 # load appropriate system call code into register $v0;# code for printing string is 4 la $a0, string1# load address of string to be printed into $a0 syscall # call operating system to perform print operation
舉例 執行到這里,程序結束,立馬走人,管他后邊洪水滔天~~
li $v0, 10 # system call code for exit = 10syscall # call operating sys
參考引用:
本文屬于轉載非原創,略有修改: https://www.cnblogs.com/thoupin/p/4018455.html這篇文章的英文原版地址在這里: https://minnie.tuhs.org/CompArch/Resources/mips_quick_tutorial.html
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
石家莊新百廣場是什么區?是橋西區。石家莊新百廣場在橋西區。地址:河北省石家莊市中山西路139號(近中華街)。穿過新百廣場(東)(西三室內繪畫館)的線路:1號、1號、6號、11號、30號、34號、61號、68號、108號、118號、317號、326號、368號..途經新百廣場(西)的線路:1路、快車1路、游客1路、游客5路、6路、11路、30路、34路、61路、93路、快車107路、192路、317...
阿里巴巴為什么要重發?因為阿里巴巴的產品排名是按照你發布的先后順序排列的,而且也和你的產品信息的新鮮度有關。一般3天再發一次。如果想短時間內再發,可以修改產品名稱。阿里巴巴每次刷新都會重新排列產品的排名。一般阿里巴巴早上8: 00第一次刷新,之后每1小時刷新不超過5分鐘,下午5: 45最后一次刷新,決定了你的產品在下一個晚上的排名!阿里巴巴重發有什么用?讓阿里巴巴誠信通會員的商品信息獲得更好的排名...
如何使用freemind制作思維導圖教程?打開FreeMind,雙擊屏幕中間的“新思維導圖”節點,編輯節點中的內容。也可以選擇節點并按F2鍵進行編輯。點擊“插入”菜單中的某一項,插入新的節點,包括子節點、父節點、平行節點以及鏈接、圖片、線條等,對于節點位置的操作,如節點左移、上移、折疊、展開等,可以使用菜單欄中的“導航”命令??梢酝ㄟ^菜單欄的“編輯”命令進行復制、剪切、粘貼等操作。最后,編輯完成后...