I'm confused on what happens during POP. When POP LR
happens does that change the PC
to the location of LR
, which would be sumi
. If it does take it back to sumi
wouldn't that just take the recursion even further?
C++:
int sumi(int n) {
if(n == 0)
return 0;
else
return n+sumi(n-1);
}
assembly:
sumi:
CMP R2,#0 ; n is R2
BNE else
then:
MOV R0,#0 ; result in R0
BX LR
else:
PUSH LR
PUSH R2
SUB R2,R2,#1
BL sumi
POP R2
ADD R0,R2,R0
POP LR
BX LR