Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.

Similar presentations


Presentation on theme: "Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack."— Presentation transcript:

1 Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack

2 © 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

3 © 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

4 © 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

5 © 2006 Department of Computing Science CMPUT 229 MOVEM  MOVEM saves and restores group of registers. Clements, pp. 265

6 © 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?

7 © 2006 Department of Computing Science CMPUT 229 Register-Saving Convention for CMPUT 229  After Apple Computer’s C convention: RegisterPreserved by Function Call? D0-D2No D3-D7Yes A0No A1No A2-A6Yes A7Stack Pointer

8 © 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

9 © 2006 Department of Computing Science CMPUT 229 Passing Parameter On The Stack Clements, pp. 273

10 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

11 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

12 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

13 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

14 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

15 © 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

16 © 2006 Department of Computing Science CMPUT 229 PEA - Push Effective Address  PEA pushes the address specified into the stack. PEAX Is equivalent to MOVE.L#X, -(A7)

17 © 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * AddUpMOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

18 © 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers (Parameter Passing) MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * AddUpMOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

19 © 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * MOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

20 Parameter Passing By Reference Clements, pp. 278

21 Parameter Passing By Reference Clements, pp. 278

22 Parameter Passing By Reference Clements, pp. 278

23 Parameter Passing By Reference Clements, pp. 278

24 Parameter Passing By Reference Clements, pp. 278

25 © 2006 Department of Computing Science CMPUT 229 Procedure Call 1Place parameters in a place where the procedure can access them. 2Transfer control to procedure. 3 Acquire the storage resources needed for the procedure. 4Perform the procedure’s task. 5Place the result value in a place where the calling program can access it. 6Return control to the point of origin. Pat.-Hen. pp. 132

26 © 2006 Department of Computing Science CMPUT 229 The LINK instruction Clements, pp. 625 $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $8000 A7(SP) $ABCC A5 BEFORE LINK A5, #-12 $ABCC $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $7FF0 A7(SP) $7FFC A5 AFTER A5 Link creates a Local “workspace” in the stack to be used by a subroutine.

27 © 2006 Department of Computing Science CMPUT 229 The UNLK instruction Clements, pp. 639 $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $8000 A7(SP) $ABCC A5 AFTER UNLK A5 $ABCC $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $7FF0 A7(SP) $7FFC A5 BEFORE A5 Unlink collapses the stack to release workspace previously allocated by LINK.

28 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS i h g j $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Memory A7(SP) $1234 A6 Before subroutine starts

29 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS $1234 i h g j Memory A7 $8000 A6 After the LINK instruction A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

30 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $8000 A6 Before UNLK instruction A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

31 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $1234 A6 After UNLK instruction $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

32 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $1234 A6 After RTS instruction $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

33 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Optimization

34 © 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int 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 item MOVE.L4(A7), D0Load g from stack into D0 ADD.L8(A7), D0 D0  g+h MOVE.L12(A7), D1Load i into D1 ADD.L16(A7), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS i h g j Memory A7 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Further Optimization


Download ppt "Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack."

Similar presentations


Ads by Google