Skip to content

Instantly share code, notes, and snippets.

@zikosw
Created October 5, 2014 11:45
Show Gist options
  • Select an option

  • Save zikosw/ef35815d7a7f248aef61 to your computer and use it in GitHub Desktop.

Select an option

Save zikosw/ef35815d7a7f248aef61 to your computer and use it in GitHub Desktop.
wow.c
int fact(int n){
if (n<1) return 1;
else
return n*fact(n-1);
}
fact:
SUB sp,sp,#8 ;Adjust stack for 2 times
STR lr,[sp,#4] ;Save return address
STR r0,[sp,#0] ;Save argument n
CMP r0,#1 ;compare n to 1
BGE L1
MOV r0,#1 ;if so,return is 1
ADD sp,sp,#8 ;Pop 2 items form stack
MOV pc,lr ;Return to caller
L1:SUB r0,r0,#1 ;else decrement n
BL fact ;Recursive call
MOV r12,r0 ;Restore original n
LDR r0,[sp,#0] ;and return address
LDR lr,[sp,#4] ;pop 2 items from stack
ADD sp,sp,#8 ;
MUL r0,r0,r12 ;Multiply to get result
MOV pc,lr ;and return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment