2013-11-01 70 views
0

我在装配MIPS中堆栈推送和弹出。我只能通过更改指针的索引手动弹出堆栈$sp中的数据,但如何通过循环来实现这一点?装配:从堆栈中弹出循环

例子:

lw $t1, 0($sp) ## pops the first data at index 0 
lw $t1, 4($sp) ## pops the second data at index 4 
lw $t1, 8($sp) ## pops the third data at index 8 
addui $sp, $sp, 12 ## Lets free our stack 

我现在的问题是,如何做到这一点在一个循环?如果我使用以下内容

addui $sp, $sp, 4这意味着我们的堆栈中会有一个空闲空间。这并不意味着将堆栈指针递增到下一个索引。

我希望你们能在这里得到我想说的话。

我不使用$ T2想到这里允许lw $t1, $t2($sp)

+1

我不明白这个问题。首先,这些'lw's不会“弹出”堆栈,它们从堆栈中加载 –

回答

1

addiu $sp, $sp, 4意味着由4递增堆栈指针(这是一个字的大小)。如果你想要一个循环,你可以这样做:

li $t0, 3   # loop counter 
loop: 
    lw $t1, 0($sp)  # load top of stack 
    addiu $sp, $sp, 4 # free top of stack 
    # ...    # do something with $t1 
    addiu $t0, $t0, -1 # decrement loop counter 
    bgtz $t0, loop  # repeat if not 0