I'm beginning in ARM assembly and I've been trying to write a simple integer division subroutine. So far, I have the following:
.text
start:
mov r0, #25
mov r1, #5
bl divide
b stop
divide:
cmp r0, r1
it lo
mov pc, lr
sub r0, r0, r1
add r2, r2, #1
b divide
stop:
b stop
I wrote it based on the pseudocode I came up with for the algorithm:
Is the Divisor (bottom) larger than the Dividend (top)?
Yes:
-Return the remainder and the counter(quotient)
No:
-Subtract the Divisor from the Dividend
-Increment the counter by 1
-Repeat the method
r0 contains the numerator and r1 contains the denominator. When the algorithm finishes, r0 should contain the remainder and r2 should contain the quotient. However, upon running, r0 contains 19 and r2 contains 0.
Are there any fallacies in my logic that I'm just missing?
asked Feb 6, 2015 at 23:59
watswat5
6911 gold badge9 silver badges19 bronze badges
1 Answer 1
I removed it lo and changed mov to movlo and it worked fine.
answered Feb 7, 2015 at 0:38
watswat5
6911 gold badge9 silver badges19 bronze badges
Sign up to request clarification or add additional context in comments.
itinstruction don't also have the corresponding condition codes (even ifititself doesn't assemble to anything in ARM state). Think about what happens when themov pc, lris executed unconditionally (my psychic powers tell me your simulator/debugger displays registers in hex).