In some assembly code, I find that this register is often used with sp register or alone,and sometimes causing problems.
In fact, I'm not very familiar about its special usage in ARM architecture.
Can someone tell me about it and what I should pay attention to when using it ?
Hi jayzhao,
the fp stands for the frame pointer which is used as a base pointer to local variables on the stack. Although sp (stack pointer) varies according to function calls from a function, fp holds a fixed value. The relationship between the sp and the fp is a just difference of offsets for the stack. For a wise compiler, the fp might not be needed because it could be realized by adjusting offsets for the sp. Therefore it would be no problems unless the fp would be used. As for the gcc, you can see the behavior of the fp by using -fno-omit-frame-pointer switch. For example the results of the compilation of the main.c as the following.
main(){ volatile int a,b,c; c = a+b;}
(1) with -fno-omit-frame-pointermain: mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #12 ldr r2, [fp, #-16] ldr r3, [fp, #-20] add r3, r3, r2 str r3, [fp, #-24] sub sp, fp, #12 ldmfd sp, {fp, sp, pc}
(2) witch -fomit-frame-pointermain: sub sp, sp, #12 ldr r2, [sp, #8] @@ fp can be replaced for sp ldr r3, [sp, #4] @@ fp can be replaced for sp add r3, r3, r2 str r3, [sp, #0] @@ fp can be replaced for sp add sp, sp, #12 bx lr
Best regards,Yasuhiko Koumoto.
Thanks very much~