Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)

Similar presentations


Presentation on theme: "Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)"— Presentation transcript:

1 Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine) return values (from subroutine to calling program) local variables (local to subroutine)

2 Stack Frame Structure local area old frame pointer return address
input area output area

3 Link and allocate LINK An,#displacement usually 1st line of subroutine
operation: (SP) ← (SP) - 4 point to next empty stack location ((SP))← (An) save old frame pointer (An) ← (SP) position frame pointer (SP) ← (SP) + d reserve local area e.g. LINK A6,#-8 . SP-> return address SP->

4 Unlink (and deallocate)
UNLK An usually last line of subroutine before RTS operation: (SP) ← (An) deallocate local area (An) ← ((SP)) restore old frame pointer (SP) ← (SP) + 4 point to return address e.g. UNLK A6 >> demonstrate on previous example

5 Stack Frame e.g. input parameters - buffer length (W) = 256
- addresses of BUFA (L), BUFB (L) output parameter - error code (W) local variables - buffer temp space = 25 longwords

6 JSR BUF_CMP ;compare buffers . ;clean up stack . ;get error code
the calling program * input parameters: buffer length (W) = 256; BUFA, BUFB * output parameter: error code (W) . ;reserve output area . ;push buffer length . ; and buffer addresses . ; on stack JSR BUF_CMP ;compare buffers . ;clean up stack . ;get error code

7 * local variables - buffer temp space = 25 longwords
the subroutine * local variables - buffer temp space = 25 longwords BUF_CMP LINK ;create tmp buff MOVEM.L D0/D1/A0-A2,-(SP) ;save regs ;get buff length ;get buff 1 addr ;get buff 2 addr ;get tmp buff addr ;… variation 1 ;… variation 2 MOVE.W #$FF,D1 ;set error code ;save error code MOVEM.L (SP)+,D0/D1/A0-A2 ;restore regs UNLK ;deallocate local area RTS

8 * Function: Write character to device screen
* Input: D0.B - Input character. * Internal: D0.B - Initially holds input character. * A Holds the screen address for character insertion. * WriteChar LINK A6, #-2 MOVEM.L A0-A1/A5/D0-D2, -(SP) MOVE.B D0, -2(A6) JSR GetConsole ; returns screen addr in A0 ; trashes D0 MOVE.W #1, -(SP) ; # bytes to insert PEA -2(A6) ; text to be inserted (ptr) MOVE.L A0, -(SP) ; field pointer for insertion JSR FldInsert ADDA.L #10, SP MOVEM.L (SP)+, A0-A1/A5/D0-D2 UNLK A6 RTS

9 * Function: Write number to device screen
* Input: D0.L - Input number. * Internal: D0.L - Input number, initially. * D3.L - Input number, copy. * A0.L - Holds buffer pointer between StrIToA and WriteString. * Buffer EQU 16 WriteNum LINK A6, #-buffer MOVEM.L A0-A1/A5/D0-D3, -(SP) MOVE.L D0, D3 MOVE.L D3, -(SP) PEA -buffer(A6) JSR StrIToA ; returns ptr in A0 ADDQ.L #8, SP JSR WriteString MOVEM.L (SP)+, A0-A1/A5/D0-D3 UNLK A6 RTS

10 output area is reserved by the calling program
SUMMARY To construct the stack frame (in order): output area is reserved by the calling program parameters are placed in the input area by the calling program return address is placed in the stack by the call to the subroutine at the start of the called program use LINK to place the old frame pointer on the stack and allocate the local area After the stack frame is constructed, the SP points to the local area (top of frame; movable) the stack frame pointer points to the old frame pointer (stationary, i.e. never moves!) retrieve parameters and access return area by displacements relative to the stack frame To deconstruct the stack frame (in order): at the end of the called program use UNLK to collapse the local area and restore the old frame pointer the return to the calling program removes the return address from the stack the calling program collapses the input area the calling program removes the output parameters from the stack

11 Reading/Expectations:
M68000 Assembly Language [Handout - pdf, 92p; N. Znotinas] review operation of LINK and UNLK instructions review the additional sample programs Expectations: you should be able to write a program using a stack frame you should be able to analyze and explain the stack contents for any program using stack frames


Download ppt "Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)"

Similar presentations


Ads by Google