1 / 20

Subroutines and Stacks

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

ulani
Download Presentation

Subroutines and Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subroutines and Stacks Lecture L3.1

  2. Subroutine and Stacks • The System Stack • Subroutines • A Data Stack

  3. The System Stack

  4. Pushing D on the stack

  5. 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?

  6. Subroutine and Stacks • The System Stack • Subroutines • A Data Stack

  7. 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

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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. Subroutine and Stacks • The System Stack • Subroutines • A Data Stack

  18. Using X as a stack pointerfor a data stack LDY 2,X+will pop $1234 into Y

  19. Subroutine to add two numberson the data stack

  20. Subroutine to subtract two numberson the data stack

More Related