2013-04-11 33 views
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" 

我把它推吧,據我所知道的,但我想不通的推動和計數的權利。我必須從這裏做什麼?

回答

3

流行將是推動的對立面。所以,如果你用它來推動$t2

lw $t2,($sp) 
addiu $sp,$sp,4 

計數的堆棧上否定詞的數量將有一個循環彈出一個字的問題:

sub $sp,$sp,4 
sw $t2,($sp) 

你會用彈出它如果彈出的值大於等於0,則使用BGEZ退出循環,否則增加計數器並重復。

+1

極端挑剔,我知道,但是具有immediate的'sub'不存在,它應該是'addi $ sp,$ sp,-4'。 – 2016-09-25 10:25:54

+0

它最有可能被彙編程序識別爲僞指令並翻譯成addiu。 – Michael 2016-09-25 11:02:02