-
Converting C to MIPS?
Hi,
I've just started looking at MIPS assembly and one of the questions in my book asks to convert C to MIPS:
Code:
for (i = 1; i <= 25; i += 2) {
A[i] = A[i-1] - B[i-1];
}
Where A and B are both arrays of integers, an addressed at $s2 and $s4 respectively.
I'm not sure how to approach this as I've never really coded in C and very new to MIPS. Would I store i to a temporary register like say $t0? Also would I need to rewrite this as a while loop for MIPS? I'm not really sure how to write it as a For loop in MIPS. I would use a "Break" if it were an if/then loop, but like I said I'm not sure how to handle the for loop.
Any info or starting tips would be greatly appreciated.
-
Yes, you'll need to store 'i' in some temporary register. As you said $t0 is a good place.
Just keep looping, but branch when 'i' ($t0) is greater than 25. Your loop may look something like:
Code:
addi $t0, $0, 1
addi $t1, $0, 25
loop:
# i <= 25
bgt $t0, $t1, end
# ... stuff in the loop
# i += 2
addi $t0, $t0, 2
# go to the next iteration
j loop
end:
# ... stuff after the loop ...
Don't get caught up in the while/do-while/for loops, they all can be written as for loops, while loops or do-while loops.