4
我試圖去學習MIPS程序集,因爲我有一些空閒時間,而且我正在嘗試編寫一個程序,將數字壓入堆棧,然後將它們彈出。我希望它能夠計算在達到負數之前彈出的數字的數量,然後每次得到負數時都會記錄下彈出多少個負數和正數。如何在MIPS程序集中從堆棧中彈出?
到目前爲止,我得到這個:
#count the number of negative words on the stock by poping the stack until a non- negative word is found
#and print out the number of words found
.text
.globl main
#this code reads the numbers from the data area and stores in them in a stack
#the numbers are located in the test area and the number of numbers in the num area
main: la $t0, test
lw $t1, num
loop: lw $t2,($t0)
sub $sp, $sp, 4
sw $t2($sp)
add $t0, $t0, 4
add $t1, $t1, -1
bnez $t1, loop
#pop from the stack and print the number of numbers in the stack before a nonnegative number is reached
#then keep count of how many negative and positive ones there are total
#code I cannot come up with would go here
.data
test: .word
2, 0xfffabfff,2,-4,-9,0x99999999,0x90000000,-2147479536,0x80000000
num: .word 10
ans: .asciiz "Number is = "
endl: .asciiz "\n"
我把它推吧,據我所知道的,但我想不通的推動和計數的權利。我必須從這裏做什麼?
極端挑剔,我知道,但是具有immediate的'sub'不存在,它應該是'addi $ sp,$ sp,-4'。 – 2016-09-25 10:25:54
它最有可能被彙編程序識別爲僞指令並翻譯成addiu。 – Michael 2016-09-25 11:02:02