絕對是我吸收不良OuO
題目:1+2+3+…+25 = 325 (十六進位:0x145)
為啥我覺得突然之間一言難盡 (要講的東西好多@@)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: | |
addi $t0, $s0, 0 | |
addi $t1, $s0, 1 | |
j loop | |
loop: | |
add $t0, $t0, $t1 | |
addi $t1, $t1, 1 | |
bne $t1, 26, loop | |
j exit | |
exit: | |
jr $ra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: | |
addi $t1, $t1, 1 | |
add $t0, $t0, $t1 | |
exit: | |
bne $t1, 25, main | |
jr $ra |
先從程式碼講起好了,其他看要不要再PO一篇(三)QuQ
兩個都是正解(上面幾乎是自己寫的,下面則是某人給我的OuO)
不過長度差頗多
上面
main:一定要寫,算是程式進入點,後面接 ":" 的都有一種標籤的意思
addi $t0, $s0, 0:是把 $s0 (恆為0) + 0 的值放到 $t0,有一種初始化為0的感覺,第3行同義
j loop:j是無條件跳到後面接的地方,這行可忽略,因為本來就會執行 loop,只是這樣比較清楚
add $t0, $t0, $t1:與 addi 不同,add 第三個參數是接 register 而非常數
bne $t1, 26, loop:bne 表示第一、二個參數若不同則跳到第三的參數的地方
jr $ra:相等於 return,主要是返回呼叫堆疊的地方
下面
其實上面有提到會用到的指令
推敲一下就會發現答案一樣
只是下面的並沒有初始化的動作
按下執行後就會出現如下
答案(145)會以16進位存到使用的register (除非轉成使用10進位)
記錄一下bne在跳地方時的address要怎麼算好了OuO
下圖顯示 -12 [loop - 0x0040003c]
-12 就是 loop 的 address 減掉 bne 的address
loop的address可以由上面 j loop 判斷
所以是 0x00400030 - 0x0040003c = -(0xc) = -12(10進位)
參考資料:(我把所有東西都放在這裡了,去找吧OuO,有空再來整理第三篇QuQ)