2
I want to write 7 assembly instructions (28 bytes) that runs a logical equivalent of this C snippet:
c = a + b + 6;
while (c > 5) {
c = c - a;
b = b + 1;
}
However, there are restrictions to the allowed instructions:
add, addi, sub, subi, slt, slti, bne
a, b, and c are located in registers $t0, $t1, and $s0 respectively. Assume they are declared as signed 32 bit integers. You may use other registers as necessary but you may not assume an initial value for any registers except $0 which is wired to ground (always 0, even if assigned to).
The best attempt to date is thanks to @Smac89 with the following assembly:
addi $s1, $t0, 6 # d = a + 6
add $s0, $s1, $t1 # c = d + b
loop: slti $t2, $s0, 6 # f = (c < 6)
bne $t2, $0, exit # (c < 6) ? exit
sub $s0, $s0, $t0 # c = c - a;
addi $t1, $t1, 1 # b = b + 1;
bne $s1, $t0, loop # (d != a) ? loop; always true
exit:
3I don't understand, your goal is to do it in 7 instructions, and you post an example that solves it in 7 instructions. What is missing? – orlp – 2016-05-11T21:25:42.350
The label is on the 8th instruction, which feels kind of "cheaty." I'd like a more definitive way of doing it in exactly 7 lines (or less if you're feeling Godly). – Patrick Roberts – 2016-05-12T04:20:58.277
1Labels are not instructions. – orlp – 2016-05-12T09:30:45.960