Implementing a Stack


Now to describe, how to emulate a stack with assembler macros:

pha     MACRO
        dec&  SP  ;SP--
        sta@  SP  ;*SP=ACC
        ENDM

pla     MACRO
        lda@  SP  ;ACC=*SP
        inc&  SP  ;SP++    //WARNING: take care of the Flags.
        ENDM


It works well, but it's obvious that the inc& command in pla better
should not modify the status register.
Better define another inc& macro with OpCode Bit 6 = 0.

Note: when a word is pushed on top of the stack, it could be used
as a "scratchpad" by lda@/sta@ etc.
It is possible to write an ALU result to that location without
modifying ACC.


Now for some more stacked mayhem: subroutines.

To keep the instruction decoder simple, MT15 does not feature things
like jsr/rts (also known as call/return).

So we have to implement this with some software macros.

WARNING: PC points to the argument (d16) of an instruction during
data calculation, so we have to set ALU C_in to 1 for a compensation.

Example:

jsr#    MACRO  addr
        ;
        dec&   SP       ;SP--
        set1@  SP       ;write $0001 on Stack                       
        slm@   SP       ;set word on Stack to $0002 by shifting it left
        add1@  PC to SP ;add PC+1 to word on Stack
        jmp#   addr
        ;
        ;<-return address
        ;
        inc&   SP       ;SP--, take return address from Stack
        ;
        ENDM

Or, to be more specific:
jsr#    MACRO  addr
        ;
        dc.w   %0001100110000110 ; $1986 //SP--
        dc.w   SP
        ;
        dc.w   %0001110110111000 ; $1db8 //write $0001 on Stack
        dc.w   SP
        ;
        dc.w   %0001110110001111 ; $1d8f //shift left word on stack
        dc.w   SP
        ;
        dc.w   %0001111110111001 ; $1fb9 //ADD PC to word on Stack
        dc.w   SP
        ;
        dc.w   %0000000100000101 ; $0105 //write next word immediate into PC
        dc.w   addr
        ;
        ;<- POINT OF RE_ENTRY
        ;
        dc.w   %0001100110110101 ; $19b5 //SP++
        dc.w   SP
        ;
        ENDM

Returning from a subroutine can simply be done with jmp@ SP.
Nevertheless, take care not to modify the Flags in the status register.

I know, that this isn't a good/efficient solution.
In fact, MT15 wasn't designed to have subroutines.
It just works.

By the way, it's possible to build conditional returns.


[HOME] [UP]/ [BACK] [1] [2] [3] [4] [5] [6] [7] [8] [NEXT]

(c) Dieter Mueller 2005