340 likes | 592 Views
The 68K Stack. Subroutines (Part 1). A stack-based machine. Problem: Compute (A+B) (C-D) in a stack-based machine. D. B. C. C. C-D. (A+B)(C-D). A. A. A+B. A+B. A+B. A+B. Push A. Push B. Add. Push C. Push D. Add. Multiply. Clements, pp. 264. A memory stack-based machine.
E N D
The 68K Stack Subroutines (Part 1) CMPUT 229
CMPUT 229 A stack-based machine Problem: Compute (A+B)(C-D) in a stack-based machine. D B C C C-D (A+B)(C-D) A A A+B A+B A+B A+B Push A Push B Add Push C Push D Add Multiply Clements, pp. 264
CMPUT 229 A memory stack-based machine Problem: Compute (A+B)(C-D) in a stack-based machine. Clements, pp. 264
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
CMPUT 229 MOVEM • MOVEM saves and restores group of registers. Clements, pp. 265
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?
CMPUT 229 Register-Saving Convention for CMPUT 229 • After Apple Computer’s C convention:
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
CMPUT 229 Passing Parameter On The Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 State of the Stack Clements, pp. 273
CMPUT 229 PEA - Push Effective Address • PEA pushes the address specified into the stack. PEA X Is equivalent to MOVE.L #X, -(A7)
CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG $400 LEA $1000, A7 Set up stack pointer PEA X Push address of variable X PEA Y Push address of variable Y PEA Z Push address of variable Z (the result) BSR AddUp Call adder routine MOVE.W Z, D2 Read result (a dummy operation) LEA 12(A7),A7 Clean up stack STOP #$2700 * AddUp MOVEA.L 12(A7), A0 Get address of parameter X MOVEA.L 8(A7), A1 Get address of parameter Y MOVE.W (A0),D2 Get value of X MOVE.W (A1),D3 Get value of Y ADD D2, D3 Add them MOVEA.L 4(A7),A3 Get address of parameter Z MOVE.W D3,(A3) Put result in variable Z RTS * ORG $500 X DC.W 1 Y DC.W 2 Z DC.W 1
CMPUT 229 Example: Add Two Numbers (Parameter Passing) MK68K assembly: ORG $400 LEA $1000, A7 Set up stack pointer PEA X Push address of variable X PEA Y Push address of variable Y PEA Z Push address of variable Z (the result) BSR AddUp Call adder routine MOVE.W Z, D2 Read result (a dummy operation) LEA 12(A7),A7 Clean up stack STOP #$2700 * AddUp MOVEA.L 12(A7), A0 Get address of parameter X MOVEA.L 8(A7), A1 Get address of parameter Y MOVE.W (A0),D2 Get value of X MOVE.W (A1),D3 Get value of Y ADD D2, D3 Add them MOVEA.L 4(A7),A3 Get address of parameter Z MOVE.W D3,(A3) Put result in variable Z RTS * ORG $500 X DC.W 1 Y DC.W 2 Z DC.W 1
CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG $400 LEA $1000, A7 Set up stack pointer PEA X Push address of variable X PEA Y Push address of variable Y PEA Z Push address of variable Z (the result) BSR AddUp Call adder routine MOVE.W Z, D2 Read result (a dummy operation) LEA 12(A7),A7 Clean up stack STOP #$2700 * MOVEA.L 12(A7), A0 Get address of parameter X MOVEA.L 8(A7), A1 Get address of parameter Y MOVE.W (A0),D2 Get value of X MOVE.W (A1),D3 Get value of Y ADD D2, D3 Add them MOVEA.L 4(A7),A3 Get address of parameter Z MOVE.W D3,(A3) Put result in variable Z RTS * ORG $500 X DC.W 1 Y DC.W 2 Z 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
CMPUT 229 Procedure Call • Place parameters in a place where the procedure can access them. • Transfer control to procedure. • Acquire the storage resources needed for the procedure. • Perform the procedure’s task. • Place the result value in a place where the calling program can access it. • Return control to the point of origin. Pat.-Hen. pp. 132
CMPUT 229 AFTER A5 $7FFC A7(SP) $7FF0 Memory $7FF8 $7FFC SP $7FF0 $7FF4 $7FF8 $7FFC $ABCC A5 $8000 $1234 $8004 The LINK instruction BEFORE A5 LINK A5, #-12 $ABCC A7(SP) $8000 Memory $7FF8 $7FFC $7FF0 Link creates a Local “workspace” in the stack to be used by a subroutine. $7FF4 $7FF8 $7FFC SP $8000 $1234 $8004 Clements, pp. 625
CMPUT 229 BEFORE AFTER A5 $7FFC A5 $ABCC A7(SP) $7FF0 A7(SP) $8000 Memory Memory $7FF8 $7FF8 $7FFC $7FFC SP $7FF0 $7FF0 $7FF4 $7FF4 $7FF8 $7FF8 $7FFC $ABCC A5 $7FFC $8000 $1234 SP $8000 $1234 $8004 $8004 The UNLK instruction UNLK A5 Unlink collapses the stack to release workspace previously allocated by LINK. Clements, pp. 639
CMPUT 229 A Procedure that Doesn’t Call Another Procedure Before subroutine starts Memory A6 $1234 int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } $7FFC $8000 A7(SP) $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS
CMPUT 229 A Procedure that Doesn’t Call Another Procedure After the LINK instruction Memory A6 $8000 int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } A7 $7FFC A6 $8000 $1234 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS
CMPUT 229 A Procedure that Doesn’t Call Another Procedure Before UNLK instruction Memory A6 $8000 int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } A7 $7FFC f A6 $8000 $1234 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS
CMPUT 229 A Procedure that Doesn’t Call Another Procedure After UNLK instruction Memory A6 $1234 int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } $7FFC f $8000 $1234 A7 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS
CMPUT 229 A Procedure that Doesn’t Call Another Procedure After RTS instruction Memory A6 $1234 int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } $7FFC f $8000 $1234 $8004 <ret adr> A7 $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS
CMPUT 229 A Procedure that Doesn’t Call Another Procedure Memory int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } $7FFC f $8000 $1234 A7 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 8(A6) , D0 Load g from stack into D0 ADD.L 12(A6), D0 D0 g+h MOVE.L 16(A6), D1 Load i into D1 ADD.L 20(A6), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS Optimization
CMPUT 229 A Procedure that Doesn’t Call Another Procedure Memory int leaf_example ( intg,int h, int i, int j) { intf; f = (g + h) - (i + j); return f; } $7FFC $8000 A7 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L 4(A7) , D0 Load g from stack into D0 ADD.L 8(A7), D0 D0 g+h MOVE.L 12(A7), D1 Load i into D1 ADD.L 16(A7), D1 D1 i+j SUB.L D1, D0 D0 D0-D1 MOVE.L D0, -4(A6) Write f into stack MOVE.L -4(A6), D0 Read f from stack into D0 UNLK A6 RTS Further Optimization