Reputation: 33
I have problems writing for loops in MIPS: I have the following for loop to write but I cant figure it out. To be honest I don't understand MIPS at all.
How would this be written in MIPS
for ( int i = 0; i < 4; i++ )
Upvotes: 0
Views: 2934
Reputation: 91
Here is a brief example of one approach of using "for-loop" in MIPS.
We will use $2 to store i, $1 to store 4, $5 to store 1 and $6 for temporary storage.
Note: everything after # are comments
#initialize
addi $1, $0, 4 #store 4 to $1
add $2, $0, $0 #set i to 0
addi $5, $0, 1 #set 1 to $5
#for loop
loop:
slt $6, $2, $1 #check if $2<4 and store result in $6
beq $6, $5, endloop #if $6 contains a 1, exit loop
#rest of code in loop
add $2, $2, $5 #increment i
beq $0, $0, loop #go back to loop label
endloop:
#rest of mips code outside loop
Upvotes: 3