Reputation: 229
I am trying to write a MIPS assembly program to get two integers from the user, save it to the memory in an array and print it. This is what i have so far. My program prints some big number that I didn't put in. I am very new to this game. Somebody help please!
Here's my code:
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $t0, 0 #count for the loop to get two integers
getnum:
li $v0, 5 #read integer
syscall
sw $v0, num($s0) #save the integer from user input into num and $s0 has address for num, I'm not sure if i did this right
addi $s0, $s0, 4 # increment $s0 by 4 to save another integer
addi $t0, $t0, 1 #increment the counter
ble $t0, 1, getnum #if counter $t0, is less then or equal to 1, it will go through the loop again
printnum:
la $a0, num($s0) #load address of num to print
li $v0, 1 #print int
syscall
addi $s0, $s0, 4
addi $t1, $t1, 1
ble $t1, 1, printnum #does it twice
li $v0, 10
syscall
.data
num:
.word 0, 0 # i want to store my two numbers here
prompt:
.asciiz "Enter 2 positive integers: "
Upvotes: 1
Views: 3661
Reputation: 4961
Your problems are two fold.
First, you are loading the address of the integer instead of the actual integer. To fix this change la
to lw
.
Secondly, because you increment $s0
twice in the getnum
loop and immediately use it in the printnum
loop it is too far ahead, you will need to add move $s0, $zero
to solve this problem.
In addition, your code seems to rely on the fact that $s0
starts the program with a value of 0, which isn't perhaps a great assumption to make. It would be better to explicitly set it to zero.
Upvotes: 2