MARS MIPS 模擬器是 MIPS 處理器的組合語言編輯器,彙編器,模擬器和偵錯程式,由密蘇里州立大學( src )的 Pete Sanderson 和 Kenneth Vollmar 開發。
在組裝之前,這個模擬器的環境可以簡單地分成三個部分: 左上角的編輯器,正在編寫所有程式碼,編輯器/輸出正下方的編輯器和代表 CPU 的暫存器列表對於我們的計劃。
組裝後(通過簡單地按 F3)環境發生變化,兩個新的段獲得編輯器的位置: 文字段在哪裡
i)每行彙編程式碼在基本列中清除偽指令(我們將在一秒內討論)
ii)程式碼列中每條指令的機器程式碼,
以及資料段,我們可以看一下具有 little-endian 順序的處理器的記憶體表示。
組裝完成後,我們可以一次性(F5)或逐步(F7)執行我們的程式碼,並將執行的幾個步驟倒退到後面(F8)。
現在,讓我們看一下上面的示例程式碼並解釋每一行:
.text
.globl main
main: #main function
li $v0, 11 #11=system code for printing a character, $v0=register that gets the system code for printing as value
la $a0, 'a' #'a'=our example character, $a0=register that accepts the character for printing
syscall #Call to the System to execute our instructions and print the character at the a0 register
li $v0, 10 #11=system code for terminating, $v0=register that gets the system code for terminating (optional, but desirable)
syscall #Call to the System to terminate the execution
MARS 接受並匯出 .asm 檔案型別的檔案
但是上面的程式碼只列印了一個字元,那麼好的 Hello World 呢?怎麼樣,不知道,新增一些數字還是什麼?好吧,我們可以改變我們的一點點:
.data #data section
str: .asciiz "Hello world\n"
number: .word 256
.text #code section
.globl main
main:
li $v0, 4 #system call for printing strings
la $a0, str #loading our string from data section to the $a0 register
syscall
la $t0, number #loading our number from data section to the $t0 register
lw $s1, 0($t0) #loading our number as a word to another register, $s1
addi $t2, $s1, 8 #adding our number ($s1) with 8 and leaving the sum to register $t2
sw $t2, 0($t0) #storing the sum of register $t2 as a word at the first place of $t0
li $v0, 10 # system call for terminating the execution
syscall
在通過 MARS 說明結果之前,需要對這些命令進行更多解釋:
系統呼叫是作業系統提供的一組服務。要使用系統呼叫,需要將呼叫程式碼放入$ v0 暫存器以進行所需操作。如果系統呼叫有引數,則將它們放在$ a0- $ a2 暫存器中。這是所有系統呼叫。
li(load immediate)是一個偽指令(稍後我們將討論),它立即載入一個帶有值的暫存器。la(載入地址)也是一個偽指令,用於將地址載入到暫存器。使用 li $v0, 4,$ v0 暫存器現在已經將 4 作為值,而 la $a0, str 將 str 的字串載入到 $a0 暫存器。
一個字 (就我們所說的 MIPS 而言)是一個 32 位序列,其中第 31 位是最高有效位,第 0 位是最低有效位。
lw(載入字)從儲存器傳輸到暫存器,而 sw(儲存字)從暫存器傳輸到儲存器。使用 lw $s1, 0($t0) 命令,我們載入到 $s1 暫存器,該暫存器位於 $t0 暫存器的 LSB 處(這就是 0 在這裡的符號,這個字的偏移量),也就是 256。$t0 這裡有地址,而 $s1 有值。sw $t2, 0($t0) 做了相反的工作。
MARS 使用 Little Endian ,意味著一個字的 LSB 儲存到儲存器的最小位元組地址。
MIPS 使用位元組地址,因此地址與前一個地址和下一個地址相差 4。
通過彙編之前的程式碼,我們可以進一步瞭解記憶體和暫存器的交換方式,從資料段中禁用十六進位制值:
或從資料段啟用 ASCII:
像這樣開始吧
$ java -jar Mars4_5.jar
建立此檔案並儲存。
.text
main:
li $s0,0x30
loop:
move $a0,$s0 # copy from s0 to a0
li $v0,11 # syscall with v0 = 11 will print out
syscall # one byte from a0 to the Run I/O window
addi $s0,$s0,3 # what happens if the constant is changed?
li $t0,0x5d
bne $s0,$t0,loop
nop # delay slot filler (just in case)
stop: j stop # loop forever here
nop # delay slot filler (just in case)
按 F3 組裝它,然後按執行。現在你開始編譯並執行 MIPS 程式碼。