简体   繁体   中英

For Loop in MIPS using .space

I am trying to make a loop that will add user inputted integers into an array until it fills the array. Every time I typed in a value, QTSPIM spits out 268501016 which I assume to be some random value stored in an register.

To test if my program was going through the whole loop, I added a call to an ascii line when the program reached the branch portion of my beq. The program seemed to be branching even if the values were not (at least to my understanding) equal.

.data
array1: .space 24
str1: .ascii "Type in numbers:"
str2: .ascii "Reached Terminate"

.text

main:

li $t2, 5
li $t3, 0 

loop1:
beq $t3, $t2, terminate #branch if equal 
la $a0, str1
syscall

ori $v0, $0, 5 #instruction to store user input in v0
syscall      #get user input and store it in v0

la $t4, array1 #load the address of the array
addu $t0, $0, $v0   #add v0 (our user input) to $t0
sw 0($t4), t0  #stores the value in $t4 to our array
addi $t3, $t3, 1  #add 1 to t3 (incrementing the counter)
addi $t4, $t4, 4  $add 4 to increment the array 4 bits to the next array slot
jal loop1 

terminate: 

la $a2, str2 #load the string to check when the program reaches terminate
syscall
ori $v0, $0, 10 # end the program
syscall

The only thing I can think is that my jump call is not going back to loop1, but if this is the case I am unaware how to fix that.

This is 32 bit MIPS code.

You're not setting up the registers properly before the syscalls .

Here there should be an li $v0, 4 before syscall :

la $a0, str1
syscall

If we assume that you're trying to print str2 here, there should be an li $v0, 4 before the syscall , and $a0 should be used instead of $a2 :

la $a2, str2 #load the string to check when the program reaches terminate
syscall

This should be sw $t0, 0($t4) , not the other way around:

sw 0($t4), t0

This should be j , not jal ( jal is used for function calls):

jal loop1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM