Reputation: 15
I am writing a program that will add together 5 integers from stdin in ARM assembly, but I am reaching an infinite loop and have no idea why.
i start out by defining basic things
/*defines functions*/
.section .rodata
promptString:
.ascii "Enter numbers: \000"
readString:
.ascii "%d\000"
printSum:
.ascii "sum=%d\n\000"
/*global varibles*/
.section .data
.align 2
.comm string,4,4
.text
/*sets addresses*/
addrString: .word string
addrPromptString: .word promptString
addrReadString: .word readString
addrPrintSum: .word printSum
I then start my main and have a loop that should last 5 iterations, but instead i reach an infinite loop
main:
stmfd sp!, {fp, lr}
mov r3,#0
mov r2,#0
mov r4,#5
loop:
cmp r3,r4
beq end
ldr r0, addrPromptString
bl printf
ldr r0, addrReadString
ldr r1, addrString
bl scanf
add r2,r2,r1
add r3,r3,#1
bl loop
end:
ldr r2, addrPrintSum
bl printf
ldmfd sp!, {fp, pc}
By what i think is sound logic, it should jump to end when r3 reaches 5 and becomes equal to r4 which is 5.
But obviously it doesn't.
Thanks!
Upvotes: 0
Views: 3827
Reputation: 91
Assuming printf and scanf are C functions, your r2 and r3 register values might be getting clobbered. The ARM ABI uses these registers as temporary registers when not being used as function parameters, so they are not guaranteed to be saved by the functions you call.
However, the ABI expects r4 through r11 to be saved before they are used. So your code might work if you save, say, r4 through r6 in the stack, use r5 and r6 in place of r2 and r3, and restore r4 through r6 when returning. That way, you follow the ABI and printf and scanf won't interfere with your variables.
Also, where it says "bl loop", you need to replace that with "b loop", so it isn't always setting the link register to the location of "end".
Upvotes: 1