next up previous contents

[ENGN3213 Home]

Local Variables and Stack Frames

Stack frames permit the dynamic creation of local storage space for use within subroutines.

Load the file clab64s.s:

*       clab64s.s
*       parameter passing via stack
*       stack frame for local variable
	
        ORG     $0 
        DC.L    $8000           Stack pointer value after a reset
        DC.L    START           Program counter value after a reset


        ORG     $2000           Start at location 2000 Hex

START   CLR.L   D0              clear D0
        MOVE.L  #$1234,D0       non-zero value for illustration
        MOVEA.L #$2468,A0       non-zero value for illustration
        MOVEA.L #$22223333,A6   non-zero value for illustration
        MOVE.W  #4,-(A7)        put integer on stack
        MOVE.L  #NUMBER,-(A7)   put address of number in stack
        BSR     ADD_IT3         execute subroutine ADD_IT3
        LEA     6(A7),A7        clean up stack
        BREAK                   done

*	subroutine ADD_IT3
*	D0 is return value (16-bit number)
*	16-bit integer parameter on stack
*	address of a number (16 bit) in memory on stack
*	local variable stack frame used
ADD_IT3 LINK    A6,#-2          create stack frame for local variable
        MOVEM.L A0,-(A7)        save working registers on stack
        CLR.L   D0
        MOVE.W  18(A7),-2(A6)   get 16-bit integer from stack, 
                                             store in local variable
        MOVE.L  14(A7),A0       get address of number in memory from stack
        MOVE.W  (A0),D0	        get number from memory
        ADD.W   D0,-2(A6)       add the numbers, store in local variable
        MOVE.W  -2(A6),D0       save return value
        MOVEM.L (A7)+,A0        restore working registers
        UNLK    A6              collapse stack frame
        RTS                     return to caller


NUMBER  DC.W    13              number 13 stored in memory

Exercise.

1.
Explain what the subroutine and main program do.

2.
What is the value of the BSR offset?

3.
List the registers (including SP, FP=A6 and PC) and stack locations which are used. Then single step through the program and record the values in a table as done above for the previous example. Carefully list all items on the stack and stack frame.

4.
How many bytes are used to store each parameter on the stack and stack frame? Carefully explain the offsets to SP=A7 and FP=A6 used to retrieve the parameters from the stack and access the local variable in the stack frame.

5.
Is the value of NUMBER changed during execution of the subroutine?

6.
State whether each parameter is passed by value or reference.

7.
State the addressing modes used in each instruction.


next up previous contents

[ENGN3213 Home]

ANU Engineering - ENGN3213