如何从MIPS程序集中的堆栈弹出?

use*_*164 5 stack 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"
Run Code Online (Sandbox Code Playgroud)

就我所知,我把它推向正确,但我无法弄清楚正在推动和计数.我从这里要做什么?

Mic*_*ael 6

一个流行音乐将与推动相反.因此,如果您使用此推送$t2:

sub $sp,$sp,4
sw $t2,($sp)
Run Code Online (Sandbox Code Playgroud)

你会弹出它:

lw $t2,($sp)
addiu $sp,$sp,4
Run Code Online (Sandbox Code Playgroud)

计算堆栈上的否定字的数量将是具有从堆栈弹出单词BGEZ的循环的问题,如果弹出的值> = 0则用于退出循环或以其他方式增加计数器并重复.

  • 极端吹毛求疵,我知道,但是不存在带有立即数的 `sub`,它应该是 `addi $sp, $sp, -4`。 (2认同)