2

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
2
  • 1
    Is this being assembled to ARM or Thumb? Either way I'm surprised it even gets that far - the assembler should error out if the instructions following an it instruction don't also have the corresponding condition codes (even if it itself doesn't assemble to anything in ARM state). Think about what happens when the mov pc, lr is executed unconditionally (my psychic powers tell me your simulator/debugger displays registers in hex). Commented Feb 7, 2015 at 0:23
  • Thanks. I replaced mov with movlo and it worked. Commented Feb 7, 2015 at 0:36

1 Answer 1

1

I removed it lo and changed mov to movlo and it worked fine.

answered Feb 7, 2015 at 0:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.