2

I am learning ARM assembly and compiled a code which simply ran a loop 5 times but when ran it goes to infinite loop

.global main
main:
push {ip.lr}
sub r1,r1,r1
well:
ldr r0,=message
bl printf
add r1,r1,#1
cmp r1,#6
bne well
pop {ip,pc}
message:
       .ascii "Amit Singh Tomar\n"

Why this code runs in to infinite Loop?

Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 2
    There is a point in learning curves where you should study more before asking questions. – auselen Mar 21 '14 at 07:42
  • 1
    @auselen: +1 - StackOverflow should not be a substitute for mental exertion. – Paul R Mar 21 '14 at 07:44
  • Ok @auselen but I tried different things before asking and kind of got frustrated.So put my question here.I will take care of your comments. – Amit Singh Tomar Mar 21 '14 at 07:44
  • 1
    @AmitSinghTomar Be sure that it is not an insult. Sometimes to learn more you need to push yourself. It is a great skill to learn without asking, it helps professionally as well. – auselen Mar 21 '14 at 07:49
  • Ok @auselen ,I do take your suggestion and work on it. – Amit Singh Tomar Mar 21 '14 at 07:53
  • For instance, [heyrick's assembler intro](http://www.heyrick.co.uk/assembler/apcsintro.html) and other ARM assembler tutorials would be good reading. Use [google ARM assembler tutorial](https://www.google.ca/#q=arm+assembler+tutorial) to find many good links. – artless noise Mar 21 '14 at 16:09
  • again possible duplicate of [ARM to C calling convention, registers to save](http://stackoverflow.com/questions/261419/arm-to-c-calling-convention-registers-to-save) – artless noise Mar 21 '14 at 16:10

2 Answers2

4

printf call destroys your r1. Save your loop invariant in non-scratch register.

auselen
  • 27,577
  • 7
  • 73
  • 114
4

R1 gets trashed by the call to printf - use a non-volatile register instead or save/restore R1 around the call to printf.

Paul R
  • 208,748
  • 37
  • 389
  • 560