Beez
Beez

Reputation: 532

MIPS Assembly - Storing user input into a stack

I'm trying to store user input into the stack, and when the user enters in 0 the program will stop receiving input and print out all of the numbers in order that the user added, not including the zero. I'm not sure how to go about this correctly. Here is my code thus far.

.data
prompt: .ascii "Please enter numbers, enter (0) to exit: "

.text

main:
li $v0, 4
la $a0, prompt
syscall


do:
li $v0, 5
syscall
addi $sp, $sp, -4 # Move stack four bytes down -- MAYBE MAKE SOME OF COUNTER TO KEEP TRACK OF HOW MUCH DATA NEEDS TO BE STORED IN STACK??
sw $v0, 0($sp) # Places contents of $t0 into first spot into the stack -- THINK I NEED TO CHANGE THIS TO ACCOMADATE CHANGE IN MEMORY??
#while
bne $t0, 0, do # Branch to print section if input = 0

print:
lw $t0, 0($sp)
addi $sp, $sp, 4
# Do printing instructions here from stack

end:
li $v0, 10
syscall

Any recommendations or help is greatly appreciated thanks!

Upvotes: 0

Views: 3090

Answers (1)

Ped7g
Ped7g

Reputation: 16596

This should be probably closed as "unclear what you're asking" (your question is not a perfect fit for stackoverflow kind of questions ... to put it nicely), but I personally don't have problem with these broad style biased questions, just the answer will be not very useful for others, so better put some effort to exploit it for yourself at least.

...
li $v0, 5

do:

What is in v0 here (i.e. which service is asked from MARS)? Five? And upon second loop iteration?

syscall
move $t0, $v0 # Places input in $t0

Why? (*)

addi $sp, $sp, -4 # Move stack four bytes down MAYBE MAKE SOME OF COUNTER TO KEEP TRACK OF HOW MUCH DATA NEEDS TO BE STORED IN STACK??

Your newly edited comment addresses the biggest deficiency. Yes, you are blindly subtracting from sp without having any way to recover it later. Keeping counter is one of possible ways, other is to store original sp somewhere (the $sX registers are handy), then stored_bytes = original_sp - sp; (and count = stored_bytes>>2;, but you may even not need the count directly, if you want just to print the values back, using original_sp value should be enough then).

sw $t0, 0($sp) # Places contents of $t0 into first spot into the stack -- THINK I NEED TO CHANGE THIS TO ACCOMADATE CHANGE IN MEMORY??

Yes, it stores value in t0 to memory at address sp. I'm not sure what your edited comment means, what is worrying you.

while:

Unused label, rather turn it into comment, as you don't need it.

beq $t0, $zero, print # Branch to print section if input = 0
j do

This can be probably turned into branch-when-not-zero (bne is MIPS instruction IIRC?), then you don't need j anywhere, when print: code will follow.

(lot more advanced tip/info, feel free to skip) Also make sure your MARS simulator has delayed branching OFF in options (as is by default, making the simulation of MIPS unreal, but easier to understand and learn to code). On real MIPS CPU you must put branching code 1 instruction ahead of where the branch will actually be effective (or avoid that by using nop after each branch, which is valid solution in some rare cases, and lame in others).

print:

# Do printing instructions here from stack

So, try something.

You should also try your code in debugger, using single-step over instruction, watching register and memory content, to get better grasp how CPU works.

* - my biggest personal mental "break-through" when I was learning programming as kid (basically I learned programming in assembly, did some BASIC before for a short while, but switched to assembly very early, as I wanted all the performance of that 3.5MHz Z80 CPU (that speed is not a typo)) was, when I finally realized that once you have theoretical formula of the calculation clean enough, you can simply write it 1:1 into computer instructions. There's no need to dance around and add additional cruft of code, to make it look similar to something else, etc. All you need is very clear and deep understanding, what is it you want to actually calculate.

In your case you want to store inputted value into stack memory. The inputted value was/is in v0. Why do you involve additional t0 register? Just store it sw $v0, 0($sp) (after the sp is adjusted by addi of course).

The same goes probably for the rest of your code and effort. Take a paper and pencil, and try first to put down what you are actually trying to achieve. The missing counter/sp-restoration would become obvious, once you would try to create the print code (you would notice there is no value to base end of printing on).

Remember, that weeks of coding can save hours of planning.

Upvotes: 2

Related Questions