除非堆棧指針做任何事情,否則沒有真正的理由我們想保留一些東西。然而,在這樣一個簡單的程序中,僅僅使用寄存器更容易。 (僅使用添加,子,多重峯,阿迪,與,或,ANDI,ORI,LW,SW,BEQ,BNE,SLT,的SLTⅠ,MFLO。)
.text
.global main
main:
addi $t0, $zero, 10 # (counter) we will start with 10 and go down to zero
add $t1, $zero, $zero # (sum) our sum, 0
count:
add $t1, $t1, $t0 # sum += counter
addi $t0, $t0, -1 # counter -= 1
bne $t0, $zero, count # if (counter) goto count
add $v0, $zero, $t1 # return value, our sum
#jr $ra # return (jr not allowed?)
如果你真的想利用堆棧存儲局部變量(count和sum),你可以做這樣的事情。但是,正如你所看到的,這是一個相當廣泛和不必要的。
.text
.global main
main:
addi $sp, $sp, -12 # Make room on the stack ($ra, sum, counter)
sw $ra, 0($sp) # save the return address (not really needed)
sw $zero, 4($sp) # sum variable, set to 0
addi $t0, $zero, 10
sw $t0, 8($sp) # counter variable, set to 10
count:
lw $t0, 4($sp) # load sum
lw $t1, 8($sp) # load counter
add $t0, $t0, $t1 # sum += counter
addi $t1, $t1, -1 # counter -= 1
sw $t0, 4($sp) # save the sum value
sw $t1, 8($sp) # save the counter
bne $t1, $zero, count # if (counter) goto count
lw $v0, 4($sp) # return value, the sum
lw $ra, 0($sp) # restore caller's address
addi $sp, $sp, 12 # pop the stack
#jr $ra # return to caller (jr not allowed?)
請把你在這裏的東西放在這裏,所以我們知道從哪裏開始。順便說一句,這聽起來完全像家庭作業。 – 2012-02-28 19:26:50
意識到,但它不是。發誓對我母親的生活。它是我們明天在測試中使用的「備忘單」。當我回到我的房間時,我可以提出我的確切時間,它在我的電腦上 – zipzapzoop45 2012-02-28 19:38:20
第二天測試的「備忘單」屬於我書中的「作業」範疇 – hirschhornsalz 2012-02-28 19:46:00