I need help with this last part of this problem. I'm basically trying to translate C++ code into MIPS Assembly language.
Assume a is in $s0, b in $s1, c in $s2, x in $s4, y in $s5, and z in $s6.
I finished almost all of them but I am stuck on these two, I know some parts of it but I'm having trouble putting it together as a whole. The parts I know will be followed by hashtags with the assembly code. Thanks for any help.
1.
for(x = 0; x <= z; x++) # x = 0; is: addi $s4, $0, 0
y = y + 1; # addi $s5, $s5, 1
y = 0; # addi $s5, $0, 0
2.
if(y > z)
x = 0; # addi $s4, $0, 0
else x = 1; # else: addi $s4, $0, 1
Here are the oringinal problems without the hashtags incase I am wrong:
1.
for(x = 0; x <= z; x++)
y = y + 1;
y = 0;
2.
if(y > z)
x = 0;
else x = 1;
Thanks again.
Attempt at 2, not sure if right.
ifLoop:
add $s5, ? , $s6
addi $s4, $0, 0
ifLoop
else:
addi $s4, $0, 1
else
Practice: (Assume array p is in $s7)
p[0] = 0;
int a = 2;
p[1] = a;
p[a] = a;
My attempt:
sw $0, 0($s7)
addiu $s0, $0, 2
sw $s0, 4($s7)
sll $t0, $s0, 2
addu $t1, $t0, $s7
sw $s0, 0($t1)
forloop and anif/elsewould both require branch instructions (ok, that particularif/elsecould probably be done branchless, but I suspect that you're supposed to use branching anyway), but I see none in your translations. Look up conditional branching in your course material or an online tutorial or a MIPS instruction set reference. - Michael