Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Faculty of Computer Science Subroutines (Part 1) The 68K Stack CMPUT 229 © 2006 Department of Computing Science A stack-based machine Problem: Compute (A+B)(C-D) in a stack-based machine. D B C C C-D A A A+B A+B A+B A+B (A+B)(C-D) Push A Push B Add Push C Push D Add Multiply CMPUT 229 Clements, pp. 264 © 2006 Department of Computing Science A memory stack-based machine Problem: Compute (A+B)(C-D) in a stack-based machine. CMPUT 229 Clements, pp. 264 © 2006 Department of Computing Science 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 CMPUT 229 Clements, pp. 265 © 2006 Department of Computing Science MOVEM MOVEM saves and restores group of registers. CMPUT 229 Clements, pp. 265 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science 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 CMPUT 229 © 2006 Department of Computing Science 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 © 2006 Department of Computing Science Passing Parameter On The Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science State of the Stack CMPUT 229 Clements, pp. 273 © 2006 Department of Computing Science PEA - Push Effective Address PEA pushes the address specified into the stack. PEA X Is equivalent to MOVE.L #X, -(A7) CMPUT 229 © 2006 Department of Computing Science Example: Add Two Numbers MK68K assembly: ORG LEA PEA PEA PEA BSR MOVE.W LEA STOP * AddUp MOVEA.L MOVEA.L MOVE.W MOVE.W ADD MOVEA.L MOVE.W RTS * ORG X DC.W Y DC.W Z DC.W CMPUT 229 $400 $1000, A7 X Y Z AddUp Z, D2 12(A7),A7 #$2700 Set up stack pointer Push address of variable X Push address of variable Y Push address of variable Z (the result) Call adder routine Read result (a dummy operation) Clean up stack 12(A7), A0 8(A7), A1 (A0),D2 (A1),D3 D2, D3 4(A7),A3 D3,(A3) Get address of parameter X Get address of parameter Y Get value of X Get value of Y Add them Get address of parameter Z Put result in variable Z $500 1 2 1 © 2006 Department of Computing Science Example: Add Two Numbers (Parameter Passing) MK68K assembly: * AddUp ORG LEA PEA PEA PEA BSR MOVE.W LEA STOP $400 $1000, A7 X Y Z AddUp Z, D2 12(A7),A7 #$2700 Set up stack pointer Push address of variable X Push address of variable Y Push address of variable Z (the result) Call adder routine Read result (a dummy operation) Clean up stack MOVEA.L MOVEA.L MOVE.W MOVE.W ADD MOVEA.L MOVE.W RTS 12(A7), A0 8(A7), A1 (A0),D2 (A1),D3 D2, D3 4(A7),A3 D3,(A3) Get address of parameter X Get address of parameter Y Get value of X Get value of Y Add them Get address of parameter Z Put result in variable Z ORG DC.W DC.W DC.W $500 1 2 1 * X Y Z CMPUT 229 © 2006 Department of Computing Science Example: Add Two Numbers MK68K assembly: ORG $400 LEA $1000, A7 PEA X PEA Y PEA Z BSR AddUp MOVE.W Z, D2 LEA 12(A7),A7 STOP #$2700 * MOVEA.L 12(A7), A0 MOVEA.L 8(A7), A1 MOVE.W (A0),D2 MOVE.W (A1),D3 ADD D2, D3 MOVEA.L 4(A7),A3 MOVE.W D3,(A3) RTS * ORG X DC.W Y DC.W Z DC.W CMPUT 229 Set up stack pointer Push address of variable X Push address of variable Y Push address of variable Z (the result) Call adder routine Read result (a dummy operation) Clean up stack Get address of parameter X Get address of parameter Y Get value of X Get value of Y Add them Get address of parameter Z Put result in variable Z $500 1 2 1 © 2006 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 Department of Computing Science 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. CMPUT 229 © 2006 Pat.-Hen. pp. 132 Department of Computing Science The LINK instruction AFTER BEFORE A5 A7(SP) LINK $ABCC A5 $7FFC A7(SP) $7FF0 A5, #-12 $8000 Memory Memory $7FF8 $7FF8 $7FFC $7FFC SP $7FF0 $7FF4 $7FF8 $7FFC SP $8000 $1234 Link creates a Local “workspace” in the stack to be used by a subroutine. $7FF0 $7FF4 $7FF8 A5 $7FFC $ABCC $8000 $1234 $8004 $8004 CMPUT 229 Clements, pp. 625 © 2006 Department of Computing Science The UNLK instruction AFTER BEFORE A5 UNLK $7FFC A7(SP) A5 $ABCC A7(SP) $8000 A5 $7FF0 Memory Memory $7FF8 $7FF8 $7FFC $7FFC SP Unlink collapses the stack to release workspace previously allocated by LINK. $7FF0 $7FF4 $7FF8 A5 $7FFC $ABCC $8000 $1234 $7FF0 $7FF4 $7FF8 $7FFC SP $8000 $1234 $8004 $8004 CMPUT 229 Clements, pp. 639 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another Before subroutine starts Procedure Memory A6 $1234 int leaf_example ( int g, int h, int i, int j) { int f; $7FFC $8000 A7(SP) f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another After the LINK instruction Procedure Memory A6 int leaf_example ( int g, int h, int i, int j) { int f; $8000 A7 $7FFC A6 $8000 $1234 $8004 <ret adr> f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another Before UNLK instruction Procedure Memory A6 int leaf_example ( int g, int h, int i, int j) { int f; $8000 A7 $7FFC f A6 $8000 $1234 $8004 <ret adr> f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another After UNLK instruction Procedure Memory A6 $1234 int leaf_example ( int g, int h, int i, int j) { int f; A7 f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $7FFC f $8000 $1234 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another After RTS instruction Procedure Memory A6 $1234 int leaf_example ( int g, int h, int i, int j) { int f; $7FFC f $8000 $1234 $8004 <ret adr> f = (g + h) - (i + j); return f; A7 } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another Procedure Memory int leaf_example ( int g, int h, int i, int j) { int f; A7 f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 8(A6) , D0 ADD.L 12(A6), D0 MOVE.L 16(A6), D1 ADD.L 20(A6), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $7FFC f $8000 $1234 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j D0 D0-D1 Write f into stack Optimization Read f from stack into D0 © 2006 Department of Computing Science A Procedure that Doesn’t Call Another Procedure Memory int leaf_example ( int g, int h, int i, int j) { int f; $7FFC $8000 A7 f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 MOVE.L 4(A7) , D0 ADD.L 8(A7), D0 MOVE.L 12(A7), D1 ADD.L 16(A7), D1 SUB.L D1, D0 MOVE.L D0, -4(A6) MOVE.L -4(A6), D0 UNLK A6 RTS CMPUT 229 $8004 <ret adr> $8008 g $800C h $8010 i $8014 j $8018 Make room in stack for 1 more item Load g from stack into D0 D0 g+h Load i into D1 D1 i+j Further D0 D0-D1 Write f into stack Read f from stack into D0 Optimization © 2006