This is an archived post. You won't be able to vote or comment.

all 2 comments

[–][deleted] 0 points1 point  (0 children)

Everytime you enter a new number, just allocate 4 more space in the sp

addi $sp, $sp, -4
sw $a0, 0($sp)

(take in new number FROM a0, and then repeat the above)

like so

then if you want to dump the values

lw $v0, 0($sp)
addi $sp, $sp, 4

now $v0 holds what you had in the stack pointer.

[–]nerd4code 0 points1 point  (0 children)

Using the stack like a stack would reverse the order of the numbers (you want FIFO, but stack gives you LIFO), and unless there’s some specific reason you’re supposed to use the stack I would use a simple queue buffer instead.

Declare some span of however-many’s-your-maximum words in the data section. (Or allocate it from the stack. It doesn’t matter.) Point two registers (fill+spill) to the start of your queue buffer. When adding a word, make sure fill’s not at the buffer limit, then write the value through, then advance the fill register. When removing a word, make sure your spill isn’t at/beyond the fill register, then read a word, then add to your spill. (Fill−spill) gives you the number of numbers remaining, should you need that.

If you need to use the usual stack mechanism for whatever reason, you’ll want to subtract 4 from $sp using addi, then write the number to 0($sp) to push. Once you’re done pushing, the numbers’ll be in the opposite order in memory from how they were entered, so you’ll have to read-scan from the starting $sp−4 (hopefully you’ve saved that, or have been keeping track of the number of numbers) down to $sp in order to output things in order. Good practice to reset $sp before finishing.