Stack

LIFO

Temporary storage facility in memory

Advantage: Processor automatically allocates space for stack and Stack Pointer (SP)

Stack-related instructions

  • LDS, STS: Load, store SP (LDS used to initialize the stack)

  • INS, DES: increment/decrement stack

  • PSHA, PSHB: Push A/B onto stack

  • PSHX, PSHY: Push X/Y onto stack

  • PULA, PULB: pull A/B off the stack

  • PULX, PULY: pull X/Y off the stack

  • TSX, TSY: (SP) + 1 → X/Y (SP not changed)

  • TXS, TYS: (X/Y) - 1 → SP (X/Y not changed)


Motorola Stack Pointer points to the open space on top of the stack

Intel Stack Pointer points to the object on top of the stack


Stack grows in memory with decreasing addresses


PSHA:

STAA 0,SP

DES


PULA:

INS

LDAA 0,SP


PSHX:

Since data is stored in big endian, It will store the High Byte in $9FFF and Low Byte in $A000

STXL 0,SP

DES

STXL 0,SP

DES


PULX:

INS

LDXH 0,SP

INS

LDXL 0,SP


Subroutines (funcitons)

  • Reusable program module

  • Main program can call the subroutine from any location

  • Subroutine instructions

    • JSR (jump to subroutine; extended addressing mode) - get to the function from the main program

    • RTS (return from subroutine) - get from the function to the main program

The RTS needs information to go back, the JSR produces that information

The JSR will store the address of where the RTS needs to go to in a register

If a subroutine calls another subroutine, the return address will be overridden, thus, you can not implement multiple subroutines

This is why the RTS can be stored in the stack.


Push the return address onto stack during execution of JSR. The program counter during the execution of the JSR has the address of the RTS because its the next instruction. Push contents of PC onto stack during execution of JSR


JSR: PSHPC (This is during the execution of the JSR so the PC is pointing to the next instruction (return address))

        JMP            SUB


RTS: PULPC



Every time you push onto the stack, you have to pull everything before you execute the RTS (the return address is sitting on top).


How to pass parameters to/from subroutines

call by value: inc(i)

call by reference: inc(&i) works only for variables, not constants - change of data item in subroutine seen by main program.


How to pass data/address? Parameter Passing

  1. call-by-value in register

    1. LDAA #5

    2. JSR    SUB

    3. SUB    …

    4. RTS

  2. Call-by-value over the stack

    1. LDAA #5

    2. PSHA

    3. JSR    SUB

    4. SUB    PULX        get return address off of stack

    5. PULA

    6. PUSHX

    7. RTS


Since we only have a few registers in a 32 bit processor, all parameters are passed over the stack.

When we use the stack you can pass however many parameters you want in and back up, C can only send one back up (return i;)



  1. A: call-by-reference in register(X/Y, not D)


                ORG        B000</p><p>DATA        FCB        5</p><p>    </p><p>                ORG        B000</p><p>DATA        FCB        5</p><p>    </p><p>                ORG        C000

                …

                LDX        #DATA

                JSR        SUB

                …


SUB        LDAA        0,X        get data

                ….

                RTS


  1. B: call-by-reference over the stack


                ORG        B000</p><p>DATA        FCB        5</p><p></p><p>                ORG        B000</p><p>DATA        FCB        5</p><p></p><p>                ORG        C000

                …

                LDX        #DATA

                PSHX

                JSR        SUB

                …


SUB        PULY                    get return address(you could also PULX)

                PULX                   get data address

                LDAA        0,X      get data

                PSHY                    push return address back onto stack

                ….

                RTS


If its a global variable, you can access it anywhere (accessing it by calling it directly)

To keep it a local variable, pass it down by value or reference


  1. B: call-by-reference over the stack (without pulling return address off of stack)


                ORG        B000</p><p>DATA        FCB        5</p><p></p><p>                ORG        B000</p><p>DATA        FCB        5</p><p></p><p>                ORG        C000

                …

                LDX        #DATA

                PSHX

                JSR        SUB

                INS                        take 2 bytes off the stack (take return address off)

                INS

                …


SUB        TSX                     get stack address

                LDX          2,X      get data address

                LDAA        0,X      get data

                ….

                RTS



LOCAL VARIABLES IN SUBROUTINES


static(in memory)

will not be destroyed when exiting subroutine


Void sub(){

    static int var1, var2;

    var1++;

    var2++;

}


                ORG            D000</p><p>VAR1       RMB            1</p><p>VAR2       RMB            1</p><p>sub        INC               VAR1</p><p>               DEC            VAR2</p><p>                RTS</p><p></p><p></p><p></p><p>Dynamic(onstack)</p><p>Willbedestroyedwhenexitingsubroutine</p><p></p><p>Voidsub()</p><p>    intvar1,var2;</p><p>    var1++;</p><p>    var2++;</p><p></p><p></p><p>                ORG            D000</p><p>VAR1       RMB            1</p><p>VAR2       RMB            1</p><p>sub         INC               VAR1</p><p>                DEC             VAR2</p><p>                RTS</p><p></p><p></p><p></p><p>Dynamic(on stack)</p><p>Will be destroyed when exiting subroutine</p><p></p><p>Void sub(){</p><p>    int var1, var2;</p><p>    var1++;</p><p>    var2++;</p><p>}</p><p></p><p>                ORG            D000

SUB         DES                                        open hole for var2

                DES                                        open hole for var1

                TSX                                        X points to var1

                INC               0,X

                DEC             1,X

                INS                                        close var1 hole

                INS                                        close var2 hole

                RTS





Passing from subroutine with dynamic local variable

psuedo code:


int result;


main(){

    result = sub();

}


int sub(){

    int temp;

    temp = 5;

    temp++;


    return(temp)

}


ASSEMBLY:


main():


                ORG                B000</p><p>RESULTRMB                1</p><p></p><p>                ORG                B000</p><p>RESULT RMB                1</p><p></p><p>                ORG                C000

                LDS                #$00FF

                JSR                SUB

                PULA            

                STAA                RESULT
DONE     STOP


sub():

Stack after JSR

SP→




RetH


$00FF

RetL



Stack before temp init

sp→




TEMP




00FF</p></td><tdcolspan="1"rowspan="1"><p>RetH</p><p>RetL</p><p>RetVAL</p></td><tdcolspan="1"rowspan="1"><p></p></td></tr></tbody></table><p></p><ol><li><p>Pullreturnaddress</p></li><li><p>openholeforreturnvalue</p></li><li><p>pushreturnaddress</p></li><li><p>openholefortempvariable</p></li><li><p>temp=5,temp++;</p></li><li><p>copttemptobottomofthestack</p></li><li><p>closevariablehole</p></li><li><p>RTS</p></li></ol><p></p><p></p><p>                ORG            00FF</p></td><td colspan="1" rowspan="1"><p>RetH</p><p>RetL</p><p>Ret VAL</p></td><td colspan="1" rowspan="1"><p></p></td></tr></tbody></table><p></p><ol><li><p>Pull return address</p></li><li><p>open hole for return value</p></li><li><p>push return address</p></li><li><p>open hole for temp variable</p></li><li><p>temp=5, temp++;</p></li><li><p>copt temp to bottom of the stack</p></li><li><p>close variable hole</p></li><li><p>RTS</p></li></ol><p></p><p></p><p>                ORG            D000

SUB        PULX                            pull return address

                DES                             open hole for return parameter

                PSHX                            push return address

                DES                              open hole for temp

                TSX                             get variable address

                LDAA             #5

                STAA            0,X            TEMP=5

                INC               0,X            TEMP++

                LDAA            0,X            get temp variable

                STAA            3,X            Store in stack

                INS                                close variable hole

                RTS