gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:45:08 25 4
gpt4 key购买 nike

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

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

最佳答案

弹出将与推相反。所以如果你用它来推送 $t2 :

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

你会弹出它:
lw $t2,($sp)
addiu $sp,$sp,4

计算堆栈上否定词的数量将是一个循环的问题,该循环从堆栈中弹出一个词,使用 BGEZ如果弹出的值 >=0 或以其他方式增加计数器并重复,则退出循环。

关于stack - 如何从 MIPS 程序集中的堆栈中弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com