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
call-by-value in register
LDAA #5
JSR SUB
SUB …
RTS
Call-by-value over the stack
LDAA #5
PSHA
JSR SUB
SUB PULX get return address off of stack
PULA
PUSHX
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;)
A: call-by-reference in register(X/Y, not D)
ORG C000
…
LDX #DATA
JSR SUB
…
SUB LDAA 0,X get data
….
RTS
B: call-by-reference over the stack
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
B: call-by-reference over the stack (without pulling return address off of stack)
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
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 C000
LDS #$00FF
JSR SUB
PULA
STAA RESULT
DONE STOP
sub():
Stack after JSR
SP→ | ||
RetH | ||
$00FF | RetL |
Stack before temp init
sp→ | ||
TEMP | ||
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 |