5

我正在尝试学习 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"

据我所知,我让它向右推动,但我无法弄清楚推动和计数的权利。我必须从这里做什么?

4

1 回答 1

7

弹出将与推送相反。因此,如果您使用它来推送$t2

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

你会弹出它:

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

计算堆栈上的负字数将是一个循环,该循环将一个字从堆栈中弹出,BGEZ如果弹出的值> = 0,则用于退出循环,否则会增加计数器并重复。

于 2013-04-12T09:06:57.740 回答