0
votes

I am trying to create a MIPS program where the user sets how many input values they want to enter(with a maximum of up to 5 values), and then input those values.

To do this I want to use a loop which would prompt the user to enter the value into the console, and then move that value from $v0 into the stack. But I am unsure of how to increment the stack pointer inside of a loop. I originally tried something like:

# Initialize t6 to zero, this is stack pointer value
add $t6, $zero, $zero

# Initialize stack to hold 5 register values
addi $sp, $sp, -20

# Load register value into stack
sw $vo, $t6($sp)

And then increment my stack pointer value by 4 each iteration. But I am getting a '"$t6": operand is of incorrect type' error. Is there a way to increment the stack pointer value inside of a loop?

Yes I do know that I can just do this with an array but I am trying to practice so that I can learn to properly use the stack. Thank you in advance for your help!

2

2 Answers

0
votes

Erik explained the problem and a couple possible solutions. Here is an alternative solution.

Instead of initializing $t6 to 0, initialize it to $sp.

addi $sp, $sp, -20
add $t6, $sp, $zero

Then you can write to the address in $t6.

sw $vo, ($t6)

Increment $t6 by 4 each iteration, just as you are doing now.

If you need a way to terminate the loop after 5 values, either use another register as a counter, or

add $t7, $t6, 20

and then exit the loop when $t6 equal $t7.

0
votes

MIPS does not have a register + register addressing mode — it only has immediate + register.  If you want to compute register + register (or register + register * scaleFactor), you have to use multiple instructions.


You can dynamically push one value onto the stack with the following code sequence:

add $sp, $sp, -4
sw $xx, 0($sp)

this allocates a word of stack space and then initializes it with xx (whatever register xx is).  This code sequence can be repeated as needed for further items.

Whenever you push something onto the stack, things already on the stack become further away, so a value that was at 8($sp), before the above code sequence is now at 12($sp).

Only if you know statically how many items are going onto the stack (or at least know a maximum count of items going on the stack), can you do a bulk allocation (e.g. add $sp, $sp, -20 and then use the slots you've allocated — this is like allocating a fixed sized array (where the fixed size is known at compile time) on the stack and then using that (or some of that).

If you don't know statically the count of items, then you can instead dynamically push one at a time as per the above.


(Of course if the item you're pushing onto the stack is actually 20 bytes large, then you can do that repeatedly.)


We can also index into a fixed sized array that is on the stack.  That requires computing the base pointer to the array, and increment/decrement the pointer by element size, or indexing into the array by scaling the index by elements size and adding to the base pointer to the array.  Either approach requires a few instructions on MIPS rather than a single complex addressing mode.