2013-04-11 60 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