200 likes | 350 Views
Subroutines and Stacks. Lecture L3.1. Subroutine and Stacks. The System Stack Subroutines A Data Stack. The System Stack. Pushing D on the stack. 1: ; System stack 2: =00004000 ORG $4000
E N D
Subroutines and Stacks Lecture L3.1
Subroutine and Stacks • The System Stack • Subroutines • A Data Stack
1: ; System stack 2: =00004000 ORG $4000 3: 4000 CF 08FF LDS #$08FF 4: 4003 CC 1234 LDD #$1234 5: 4006 CD 5678 LDY #$5678 6: 4009 3B PSHD 7: 400A 35 PSHY 8: 400B 30 PULX 9: 400C 31 PULY 10: 400D 3F SWI What values end up in X and Y?
Subroutine and Stacks • The System Stack • Subroutines • A Data Stack
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4006 D 1234
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4010 4006 D 68AC
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4010 4006 D 79BD
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4006 D 79BD
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack D 79BD
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4009 D 79BD
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack 4009 D 8ACE
1: ; Subroutine example 2: 3: 4: =00004000 ORG $4000 5: 4000 EX2 6: 4000 CC 1234 LDD #$1234 7: 4003 16 400A JSR SUB1 8: 4006 16 4011 JSR SUB2 9: 4009 3F SWI 10: 11: 400A SUB1 12: 400A C3 5678 ADDD #$5678 13: 400D 16 4011 JSR SUB2 14: 4010 3D RTS 15: 16: 4011 SUB2 17: 4011 C3 1111 ADDD #$1111 18: 4014 3D RTS Stack D 8ACE
Note that you MUST pull ALL registers that you pushed on the stack Saving and restoring registersin a subroutine ; subroutine example subname pshb ;save B pshx ;save X ---- ---- ---- ;instructions that ---- ;change B and X ---- pulx ;restore X pulb ;restore B rts
Subroutine and Stacks • The System Stack • Subroutines • A Data Stack
Using X as a stack pointerfor a data stack LDY 2,X+will pop $1234 into Y