Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself
© 2006 Department of Computing Science CMPUT 229 Calling Itself int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }
© 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
© 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 $ Memory SP$8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 3 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 Assuming that call to subroutine fact is at address $100024FC $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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)); } $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ 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 $ ….
© 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 $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $00007FFC SP A6 $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF0 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP/A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF8 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $00007FFC SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP SP/A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP SP/A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 A6
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP/A6
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP/A6
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP
© 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 $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP
© 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
© 2006 Department of Computing Science CMPUT 229 ASCII Code Patt.-Hen., pp 142
© 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
© 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
© 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
© 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
© 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
© 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
© 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