Download - ECE 447: Lecture 15
ECE 447: Lecture 15
Stack Operations
ECE 447: Stack after a function call
RTNL
RTNH
SP before
a function call
SP after
a function call
0
MAX_ADDRESS
ECE 447: Stack Operations
Subroutine call and return from subroutine
BSR
JSR
REL
N Z V C
– – – –
– – – –DIR, EXT, IND
RTS INH – – – –
ECE 447: Stack after an interrupt call
RTNL
RTNH
IYL
IYH
IXL
IXH
ABCC
SP before
an interrupt
SP after
an interrupt 0
MAX_ADDRESS
ECE 447: Stack Operations
Interrupt related instructionsand return from an interrupt
SWI
WAI
N Z V C
– – – –
– – – –
RTI INH – – – –
INH
INH
ECE 447: Stack Operations
INH
N Z V C
– – – –
PSH [A, B]PSH [X, Y]PUL [A, B]PUL [X, Y]
IMM, DIR, EXT, IND
N Z V C
0 –
Initialize and store stack pointer
DIR, EXT, IND 0 –
LDS
STS
Push and pull
ECE 447: Stack OperationsJump
Stack pointer manipulation
JMP – – – –DIR, EXT, IND
DES
TS [X, Y]
T [X, Y] S
INH
INH
INH
– – – –
– – – –
– – – –
INS
INH – – – –
Using the stack - Entering a function with a fixednumber of arguments and local variables
arg_2
arg_N
arg_2
arg_N...
arg_2
arg_N......
arg_2
arg_N...
RTNH
RTNL
RTNH
RTNL
RTNH
RTNL
Savedregisters
var_1
var_M...
callerfunction
calledfunction
SP
SP
IXarg_1
D
arg_1L
D
arg_1H
IXor
PSH
JSR orBSR
PSH
DES orPSHX
TSX
Localvariables
Returnaddress
Functionarguments
reg_1
reg_L...
reg_1
reg_L...
ECE447: Exiting a function: “called-cleanup” method
arg_2
arg_N
arg_2
arg_N......
arg_2
arg_N...
RTNH
RTNL
RTNH
RTNL
RTNH
RTNL
Savedregisters
var_1
var_M...
callerfunction
calledfunction
SP
SP
IXret
D
retL
D
retH
IXor
JMP 0,Y
PULY
INS
PUL
INS orPUL
Localvariables
Returnaddress
Functionarguments
reg_1
reg_L...
reg_1
reg_L...
Using the stack - Entering a function with a variablenumber of arguments
arg_1
arg_N
arg_1
arg_N...
arg_1
arg_N......
arg_1
arg_N...
RTNH
RTNL
RTNH
RTNL
RTNH
RTNL
Savedregisters
var_1
var_M...
callerfunction
calledfunction
SP
SP
IX
PSH
JSR orBSR
PSH
DES orPSHX
TSX
Localvariables
Returnaddress
Functionarguments
reg_1
reg_L...
reg_1
reg_L...
Using the stack - Exiting a function“caller-cleanup” convention
arg_1
arg_N
arg_1
arg_N...
arg_1
arg_N......
arg_1
arg_N...
RTNH
RTNL
RTNH
RTNL
RTNH
RTNL
Savedregisters
var_1
var_M...
callerfunction
calledfunction
SP
SP
IXret
D
retL
D
retH
IXor
PULz
RTS
PUL
INS orPULX
TXSLocal
variables
Returnaddress
Functionarguments
reg_1
reg_L...
reg_1
reg_L...
/* C version of main function */
#include <stdio.h>
extern long multadd(unsigned int a, unsigned char b, long c, long *pm);
long product, sum;
void main(void){ sum = multadd(258, 17, 32L, &product);}
main.c
In this example, long = 32bits
/* C version of the multadd function */
long multadd(unsigned int a, unsigned char b, long c, long *pm)/* this function returns a*b + c and writes a*b to the location of memory pointed out by pm*/{ long tmul; long tadd; tmul = a*b; *pm = tmul; tadd = tmul + c;
return tadd;}
multadd.c
* assembly language version of the main function
.global multadd
.global printf
.mri 1
section .bssproduct rmb 4sum rmb 4
amain.s (1)
section .textmain:; pushing arguments arg4 thru arg2 to the stack; arg4: pm = &product ldx #product pshx; arg3: c = 32L = 0x00000020 ldx #$0020 pshx ldx #$0000 pshx; arg2: b = 17 = 0x11 ldaa #$11 psha ; storing argument arg1 in the register D; arg1: a = 258 = 0x102 ldd #$102 jsr multadd
amain.s (2)
amultadd.s (1)
; assembly language version of the multadd function
tmul_offset EQU 0tadd_offset EQU 4a_offset EQU 8b_offset EQU 12c_offset EQU 13pm_offset EQU 17
0 - tmul
1 - tmul
2 - tmul
3 - tmul
4 - tadd
5 - tadd
6 - tadd
7 - tadd
10 - RTNH
11 - RTNL
12 - b
13 - c
14 - c
15 - c
16 - c17 - &product
18 - &product
8 - a
9 - a
SP
IX
amultadd.s (2)
section .text
multadd:;---------------------------------------------------------; Initialization sequence, similar for different functions;---------------------------------------------------------; storing D register
pshbpsha
; reserving space for two local variables; long tadd pshx pshx; long tmul pshx pshx
; IX set to point to the top of the stack tsx
amultadd.s (3a)
;---------------------------------------------------------; Part specific for the given function;---------------------------------------------------------; Multiplication;---------------------------------------------------------; tmul = a*b = (a_high * 2^8 + a_low) * b; ACCB = b
ldab b_offset,X; ACCA = LSB of a = a_low
ldaa a_offset+1,X; ACCD = b*a_low
mul; tmul = b*a_low
std tmul_offset+2,Xldd #0std tmul_offset,X
; ACCB = bldab b_offset,X
; ACCA = MSB of a = a_high ldaa a_offset,X
; ACCD = b*a_high mul; tmul= b*a_low + (2^8)*b*a_high
addd tmul_offset+1,Xstd tmul_offset+1,X
; *pm = tmulldy pm_offset,Xldd tmul_offset,Xstd 0,Yldd tmul_offset+2,Xstd 2,Y
amultadd.s (3b)
amultadd.s (4a)
;---------------------------------------------------------; Addition;---------------------------------------------------------; tadd = c
ldd c_offset,Xstd tadd_offset,Xldd c_offset+2,X
std tadd_offset+2,X; tadd = tadd + tmul
pshXclc
ldab #4loop ldaa tadd_offset+3,X
adca tmul_offset+3,Xstaa tadd_offset+3,Xdexdecbbne looppulx
; End of part specific for the given function
;---------------------------------------------------------; Exit sequence;---------------------------------------------------------
; restore value of the stack pointertxs
; return long result in registers IX and D; IX:D = tadd
ldx tadd_offset,Xldd tadd_offset+2,X
; deallocate space for local variables; repeat this 8 times
insinsinsinsinsins
amultadd.s (5)
insins
; remove register D from the stack puly; store return address in register IY puly ; repeat 7 times ins
insinsinsinsinsins
jmp 0,Y
end
amultadd.s (6)
RTNH
RTNL
main multadd
SP
SP
IX
$0102
D
LDXPSHX JSR
PSHD
PSHXPSHXPSHXPSHXTSX
&product
$20
LDD
$00$00$00$11
&product
$20$00$00$00$11
RTNH
RTNL
&product
$20$00$00$00$11
$02$01
RTNH
RTNL
&product
$20$00$00$00$11
$02$01
xxxx
xx xx xx
xxxx
xx
tmul
tadd
D=a
b
c
pm
0
4
8
12
13
17