Download Subroutine and Stacks

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Subroutines and Stacks
Lecture L3.1
Subroutine and Stacks
• The System Stack
• Subroutines
• A Data Stack
The System Stack
Table 2.1 Push and Pull Instructions
Mnemonic
Function
PSHA
Push A
PSHB
Push B
PSHC
Push CCR
PSHD
Push D
PSHX
Push X
PSHY
Push Y
PULA
Pull A
PULB
Pull B
PULC
Pull C
PULD
Pull D
PULX
Pull X
PULY
Pull Y
Pushing D on the stack
A
B
D 12 34
09FE
SP = 09FE
12
09FF
09FF
34
SP = 0A00
0A00
Before PSHD
After PSHD
68HC11 Note: When values are pushed on the stack in the 68HC12 the stack pointer is
first decremented and then the data are stored at the new location of SP. However, in the
68HC11 the data are first stored at the current SP location and then the stack pointer is
decremented. Thus, in the 68HC12 the stack pointer points to the last full location on the
stack, while in the 68HC11 it points to the next free location on the stack.
1:
stack
2:
;
=00004000
System
ORG $4000
3:
4000 CF 08FF
LDS
#$08FF
4:
4003 CC 1234
LDD
#$1234
5:
4006 CD 5678
LDY
#$5678
6:
4009 3B
PSHD
7:
400A 35
PSHY
8:
400B 30
PULX
9:
400C 31
PULY
10:
400D 3F
SWI
What values end up in X and Y?
Subroutine and Stacks
• The System Stack
• Subroutines
• A Data Stack
1:
example
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
#$1234
SUB1
SUB2
SWI
ADDD
JSR
RTS
#$5678
SUB2
ADDD
RTS
#$1111
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
SUB2
ORG
EX2
CC 1234
16 400A
16 4011
3F
400A
400A C3 5678
400D 16 4011
LDD
JSR
JSR
SWI
Stack
SUB1
4006
ADDD
JSR
4010 3D
4011
4011 C3 1111
4014 3D
#$1234
SUB1
SUB2
D
1234
#$5678
RTS
SUB2
ADDD
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
Stack
4010
4006
ADDD
JSR
ADDD
#$1234
SUB1
SUB2
SWID
68AC
#$5678
SUB2
RTS
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
Stack
4010
4006
ADDD
JSR
ADDD
#$1234
SUB1
SUB2
SWID
79BD
#$5678
SUB2
RTS
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
Stack
4006
#$1234
SUB1
SUB2
SWID
79BD
ADDD
JSR
#$5678
SUB2
RTS
ADDD
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
Stack
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
#$1234
SUB1
SUB2
SWID
79BD
ADDD
JSR
#$5678
SUB2
RTS
ADDD
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
Stack
4009
#$1234
SUB1
SUB2
SWID
79BD
ADDD
JSR
#$5678
SUB2
RTS
ADDD
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
Stack
4009
#$1234
SUB1
SUB2
SWID
8ACE
ADDD
JSR
#$5678
SUB2
RTS
ADDD
#$1111
RTS
1:
2:
3:
4:
$4000
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
; Subroutine example
=00004000
4000
4000
4003
4006
4009
ORG
EX2
CC 1234
16 400A
16 4011
3F
LDD
JSR
JSR
Stack
400A
400A C3 5678
400D 16 4011
4010 3D
SUB1
4011
4011 C3 1111
4014 3D
SUB2
#$1234
SUB1
SUB2
SWID
8ACE
ADDD
JSR
#$5678
SUB2
RTS
ADDD
#$1111
RTS
Saving and restoring registers
in a subroutine
;
subroutine example
subname
pshb
;save B
pshx
;save X
---------;instructions that
---;change B and X
---Note that you MUST
pulx
;restore X
pull ALL registers
pulb
;restore B
that you pushed on
rts
the stack
Subroutine and Stacks
• The System Stack
• Subroutines
• A Data Stack
Using X as a stack pointer
for a data stack
A
B
D 12 34
096E
X=
096E
12
096F
096F
34
0970
0970
X=
Before STD 2,-X
LDY 2,X+
After STD 2,-X
will pop $1234 into Y
Subroutine to add two numbers
on the data stack
;
PLUS
+ ( n1 n2 -- n1+n2 )
LDD
ADDD
STD
RTS
X ->
2,X+
0,X
0,X
Before
After
04D2
04D2
LDD
2,X+
162E
04D2
X ->
ADDD
04D2
162E
04D2
0,X
162E
X ->
STD
162E
162E
04D2
0,X
X ->
1B00
D
042D
D
1B00
D
1B00
Subroutine to subtract two numbers
on the data stack
;
MINUS
- ( n1 n2 -- n1-n2 )
LDD
SUBD
STD
RTS
2,X
2,X+
0,X
Related documents