I am stuck with an exercise of ARM. The following program should calculate the result of 2((x-1)^2 + 1) but there is a mistake in the program that leads it into an infinite loop. I think that I still don't understand completely subroutines and for this reason I am not seeing where the mistake is.
_start:
mov r0, #4
bl g
mov r7, #1
swi #0
f:
mul r1, r0, r0
add r0, r1, #1
mov pc, lr
g:
sub r0, r0, #1
bl f
add r0, r0, r0
mov pc, lr
The infinite loop starts in subroutine g:
in the line of mov pc, lr
and instead of returning to _start
it goes to the previous line add r0, r0, r0
and then again to the last line of subroutine g:
.
So I guess that the problem is the last line of subroutine g:
but I can't find the way to return to _start
without using mov pc, lr
. I mean, this should be the command used when we have a branch with link.
Also, in this case r0 = 4, so the result of the program should be 20.