Download presentation
Presentation is loading. Please wait.
1
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself
2
© 2006 Department of Computing Science CMPUT 229 Calling Itself int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }
3
© 2006 Department of Computing Science CMPUT 229 Linking a Recursive Procedure int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 A6 SP
4
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10004000 3 Memory SP$8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $12345678 A6 $00008014 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
5
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $00008010 A6 $0000800C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
6
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $00008010 A6 $0000800C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
7
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 3 D0 $00008010 A6 $0000800C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
8
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $0000800C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
9
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 2 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $0000800C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
10
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008008 SP A6 Assuming that call to subroutine fact is at address $100024FC $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
11
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008004 A6 $00008000 SP A6 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
12
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008004 A6 $00008000 SP A6 $1000 2500 M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
13
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008004 A6 $00008000 SP A6 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
14
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00008004 A6 $00008000 SP A6 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
15
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00008004 A6 $00008000 SP A6 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
16
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00008004 A6 $00007FFC SP A6 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
17
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
18
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
19
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
20
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
21
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 0 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
22
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF0 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
23
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
24
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
25
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
26
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
27
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
28
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
29
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
30
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP/A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF8 SP $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
31
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $00008004 0 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
32
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
33
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00008004 A6 $00007FFC SP A6 $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
34
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00008004 A6 $00008000 SP A6 $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
35
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008004 A6 $00008004 SP SP/A6 $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
36
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 2 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008004 A6 $00008004 SP SP/A6 $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
37
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 2 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008008 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
38
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 2 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008008 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
39
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 2 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
40
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 2 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $00008010 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
41
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 6 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $00008010 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
42
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 6 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008014 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
43
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 6 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008018 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
44
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 6 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008018 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $1000 4000….
45
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008008 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 A6
46
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 3 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $00008010 A6 $00008014 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8
47
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $00008010 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 SP/A6
48
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $00008010 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 SP/A6
49
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 SP
50
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 SP
51
© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n 1 SUBQ.L #1,D0D0 n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $10002500 1 $12345678 $0008010 2 $10002500 $10004000 6 Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $12345678 A6 $00008010 SP $00008004 1 $10002500 $7FF8 $7FF4 $7FF0 $1000 2500 $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $1000 4000…. $00007FF8 $7FEC 1 $7FE8 SP
52
© 2006 Department of Computing Science CMPUT 229 Other Data Stored in the Stack $sp High Address Low Address $fp Before procedure call $sp High Address Low Address $fp After procedure call $sp High Address Low Address $fp During procedure call Saved argument registers Saved return reg. Saved registers Local arrays and structures Patt.-Hen. pp 139
53
© 2006 Department of Computing Science CMPUT 229 ASCII Code Patt.-Hen., pp 142
54
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0 M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.B(A0,D0), D5D5 y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return
55
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save D0 in stack void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.B(A0,D0), D5D5 y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
56
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack i 0 void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.B(A0,D0), D5D5 y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
57
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack i 0 x[i] y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.BD4, (A0,D0)x[i] y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
58
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack y[i] = 0? i 0 x[i] y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.BD4, (A0,D0)x[i] y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
59
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure i i + 1 save $s0 in stack no y[i] = 0? i 0 x[i] y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.BD4, (A0,D0)x[i] y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
60
© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure i i + 1 save $s0 in stack no y[i] = 0? yes restore $s0 return i 0 x[i] y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i 0 L1:MOVE.B(A1,D0), D4D4 y[i] MOVE.BD4, (A0,D0)x[i] y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ] A0 base of array y[ ] A1 Assumption i D0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.