Faculty of Computer Science CMPUT 229 © 2006 Subroutines (Part 1) The 68K Stack
Jan 07, 2016
Faculty of Computer Science
CMPUT 229 © 2006
Subroutines (Part 1)
The 68K Stack
© 2006
Department of Computing Science
CMPUT 229
A stack-based machine
Problem: Compute (A+B)(C-D) in a stack-based
machine.
A
Push A
A
B
Push B
A+B
Add
A+B
C
Push C
A+B
C
D
Push D
A+B
C-D
Add
(A+B)(C-D)
Multiply
Clements, pp. 264
© 2006
Department of Computing Science
CMPUT 229
A memory stack-based machine
Problem: Compute (A+B)(C-D) in a stack-based
machine.
Clements, pp. 264
© 2006
Department of Computing Science
CMPUT 229
Pushing D0 into the stack
Stack pointer (A7) always points to element at the top of the stack.
- Decrement A7 before a push
- Increment A7 after a pull
Clements, pp. 265
© 2006
Department of Computing Science
CMPUT 229
MOVEM
MOVEM saves and restores group of registers.
Clements, pp. 265
© 2006
Department of Computing Science
CMPUT 229
Subroutine Calling Conventions
Parameter Passing
– Where?
• On registers
• On the stack frame
– How?
• By value
• By reference
Register Preservation Conventions
– Which registers are preserved by a function call?
© 2006
Department of Computing Science
CMPUT 229
Register-Saving Convention for CMPUT 229
After Apple Computer’s C convention:
Register Preserved by Function Call?
D0-D2 No
D3-D7 Yes
A0 No
A1 No
A2-A6 Yes
A7 Stack Pointer
© 2006
Department of Computing Science
CMPUT 229
BSR and JSR
There are two instructions to call a subroutine:
– BSR (Branch to Subroutine)
• Is relative to the current address
– Range of - 32 kbytes to + 32 kbytes
– Allows position-independent code
– JSR (Jump to Subroutine)
• The address is absolute
– Range is not limited
– Code is position dependent
© 2006
Department of Computing Science
CMPUT 229
Passing Parameter On The Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
State of the Stack
Clements, pp. 273
© 2006
Department of Computing Science
CMPUT 229
PEA - Push Effective Address
PEA pushes the address specified into the stack.
PEA X
Is equivalent to
MOVE.L #X, -(A7)
© 2006
Department of Computing Science
CMPUT 229
Example: Add Two NumbersMK68K assembly:
ORG $400LEA $1000, A7 Set up stack pointerPEA X Push address of variable XPEA Y Push address of variable YPEA Z Push address of variable Z (the result)BSR AddUp Call adder routineMOVE.W Z, D2 Read result (a dummy operation)LEA 12(A7),A7 Clean up stackSTOP #$2700
*AddUp MOVEA.L 12(A7), A0 Get address of parameter X
MOVEA.L 8(A7), A1 Get address of parameter YMOVE.W (A0),D2 Get value of XMOVE.W (A1),D3 Get value of YADD D2, D3 Add themMOVEA.L 4(A7),A3 Get address of parameter ZMOVE.W D3,(A3) Put result in variable ZRTS
*ORG $500
X DC.W 1Y DC.W 2Z DC.W 1
© 2006
Department of Computing Science
CMPUT 229
Example: Add Two Numbers (Parameter Passing) MK68K assembly:
ORG $400LEA $1000, A7 Set up stack pointerPEA X Push address of variable XPEA Y Push address of variable YPEA Z Push address of variable Z (the result)BSR AddUp Call adder routineMOVE.W Z, D2 Read result (a dummy operation)LEA 12(A7),A7 Clean up stackSTOP #$2700
*AddUp MOVEA.L 12(A7), A0 Get address of parameter X
MOVEA.L 8(A7), A1 Get address of parameter YMOVE.W (A0),D2 Get value of XMOVE.W (A1),D3 Get value of YADD D2, D3 Add themMOVEA.L 4(A7),A3 Get address of parameter ZMOVE.W D3,(A3) Put result in variable ZRTS
*ORG $500
X DC.W 1Y DC.W 2Z DC.W 1
© 2006
Department of Computing Science
CMPUT 229
Example: Add Two NumbersMK68K assembly:ORG $400LEA $1000, A7 Set up stack pointerPEA X Push address of variable XPEA Y Push address of variable YPEA Z Push address of variable Z (the result)BSR AddUp Call adder routineMOVE.W Z, D2 Read result (a dummy operation)LEA 12(A7),A7 Clean up stackSTOP #$2700*MOVEA.L 12(A7), A0 Get address of parameter XMOVEA.L 8(A7), A1 Get address of parameter YMOVE.W (A0),D2 Get value of XMOVE.W (A1),D3 Get value of YADD D2, D3 Add themMOVEA.L 4(A7),A3 Get address of parameter ZMOVE.W D3,(A3) Put result in variable ZRTS*
ORG $500X DC.W 1Y DC.W 2Z DC.W 1
Parameter Passing By Reference
Clements, pp. 278
Parameter Passing By Reference
Clements, pp. 278
Parameter Passing By Reference
Clements, pp. 278
Parameter Passing By Reference
Clements, pp. 278
Parameter Passing By Reference
Clements, pp. 278
© 2006
Department of Computing Science
CMPUT 229
Procedure Call
1 Place parameters in a place where the procedure can access
them.
2 Transfer control to procedure.
3 Acquire the storage resources needed for the procedure.
4 Perform the procedure’s task.
5 Place the result value in a place where the calling program can
access it.
6 Return control to the point of origin.
Pat.-Hen. pp. 132
© 2006
Department of Computing Science
CMPUT 229
The LINK instruction
Clements, pp. 625
$1234$8000
$8004
$7FFC
$7FF8
$7FF4
$7FF0
$7FFC
$7FF8
Memory
SP
$8000A7(SP)
$ABCCA5
BEFORE
LINK A5, #-12
$ABCC
$1234$8000
$8004
$7FFC
$7FF8
$7FF4
$7FF0
$7FFC
$7FF8
Memory
SP
$7FF0A7(SP)
$7FFCA5
AFTER
A5
Link creates a Local
“workspace” in the
stack to be used by a
subroutine.
© 2006
Department of Computing Science
CMPUT 229
The UNLK instruction
Clements, pp. 639
$1234$8000
$8004
$7FFC
$7FF8
$7FF4
$7FF0
$7FFC
$7FF8
Memory
SP
$8000A7(SP)
$ABCCA5
AFTER
UNLK A5
$ABCC
$1234$8000
$8004
$7FFC
$7FF8
$7FF4
$7FF0
$7FFC
$7FF8
Memory
SP
$7FF0A7(SP)
$7FFCA5
BEFORE
A5
Unlink collapses the
stack to release workspace
previously allocated by LINK.
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6RTS
i
<ret adr>
h
g
j$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
Memory
A7(SP)
$1234A6
Before subroutine starts
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
$1234
i
<ret adr>
h
g
j
Memory
A7
$8000A6
After the LINK instruction
A6
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
f
$1234
i
<ret adr>
h
g
j
Memory
A7
$8000A6
Before UNLK instruction
A6
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
f
$1234
i
<ret adr>
h
g
j
Memory
A7
$1234A6
After UNLK instruction
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
f
$1234
i
<ret adr>
h
g
j
Memory
A7
$1234A6
After RTS instruction
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 8(A6) , D0 Load g from stack into D0ADD.L 12(A6), D0 D0 g+hMOVE.L 16(A6), D1 Load i into D1ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
f
$1234
i
<ret adr>
h
g
j
Memory
A7
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
Optimization
© 2006
Department of Computing Science
CMPUT 229
A Procedure that Doesn’t Call Another Procedureint leaf_example ( int g, int h, int i, int j) { int f;
f = (g + h) - (i + j); return f; }
MK68K assembly:LINK.w A6,#-4 Make room in stack for 1 more itemMOVE.L 4(A7) , D0 Load g from stack into D0ADD.L 8(A7), D0 D0 g+hMOVE.L 12(A7), D1 Load i into D1ADD.L 16(A7), D1 D1 i+j SUB.L D1, D0 D0 D0-D1MOVE.L D0, -4(A6) Write f into stackMOVE.L -4(A6), D0 Read f from stack into D0UNLK A6RTS
i
<ret adr>
h
g
j
Memory
A7
$8014
$8018
$8010
$800C
$8008
$8004
$8000
$7FFC
Further
Optimization