Analysis of DSP2407's frame pointer (FP) and stack pointer (SP)
[Copy link]
This article explains the FP and SP pointers by analyzing the protection and recovery of a simple function.
There are several pointers in DSP that have special uses. AR0 is the FP pointer, AR1 is the SP pointer, and AR2, AR3, AR4, and AR5 can be used freely, that is, they can be modified during function execution and do not need to be restored. AR6 and AR7 are used as register variables. If they are modified in a function, they must be protected and restored.
int add(int a, int b, int c)
{
int nSum = 0;
nSum = a + b + c;
return nSum;
}
The assembly code is as follows:
add: ; Enter the function ARP is 1
POPD *+ ; Save the function return address
SAR AR0, *+ ; Save the FP pointer
SAR AR1, * ; Save the SP pointer
LAR AR0,#2h ; Load the local frame length, that is, the number of local variables + 1
LAR AR0,*0+,AR2 ; Old FP = old SP, new SP = old SP + 2, ARP = 2
LACL #0h
LAR AR2,#1h ;AR2 = 1
MAR *0+ ;AR2 = AR2 + AR0
SACL *,0 ; Set the content of AR2 to 0
SETC SXM
SBRK #4h ;AR2 = AR2 - 4
; The memory is arranged as follows:
; Parameter C, return address, old FP, old SP, AR2. At this time, AR2 points to parameter C
LACC *-,0 ;Load c, AR2 = AR2 - 1
ADD *-,0 ;ACC += b, AR2 = AR2 - 1
ADD *,0 ;ACC += a
ADRK #6h ;Memory is arranged as follows:
;Parameters A(AR2), B, C, return address, old FP, old SP, (before calculation).
;AR2 += 6, return to the address before AR2 starts calculation
SACL *,0 ;Assign value to nSum
LACC *,0 ;Load to ACC, return value
MAR *,AR1 ;Switch to AR1
SBRK #3h ;Cancel local frame, AR1 returns to the old FP position
LAR AR0,*- ;Restore FP
PSHD * ;Return address is pushed to the stack
RET
|