1 / 20

Stacks, Subroutines, I/O Routines

Stacks, Subroutines, I/O Routines. Today: First Hour : Stacks, Subroutines Section 3.9,3.10 of Huang’s Textbook In-class Activity #1 Second Hour : I/O Routines Section 3.11 of Huang’s Textbook In-class Activity #2. Subroutines:Recap. Program Structure. Subroutine Processing.

xylia
Download Presentation

Stacks, Subroutines, I/O Routines

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. Stacks, Subroutines, I/O Routines • Today: • First Hour: Stacks, Subroutines • Section 3.9,3.10 of Huang’s Textbook • In-class Activity #1 • Second Hour: I/O Routines • Section 3.11 of Huang’s Textbook • In-class Activity #2

  2. Subroutines:Recap Program Structure Subroutine Processing Main program Main program <call> sub_x . . Subroutine 1 Subroutine 2 Subroutine 3 . sub_x: . Subroutine 1.1 Subroutine 2.1 Subroutine 3.1 . . Subroutine <return> Subroutine 2.1.1 Subroutine 2.1.2 Subroutine call: JSR, BSR Subroutine return: RTS

  3. * Goal: to increment the number in the accumulator A using a subroutine. ORG $C000 START LDS #INITSP LDAA #$32 ;the number to increment JSR INCREMENT_A ;call subroutine SWI ; software interrupt ************************************************************************** * subroutine INCREMENT_A INCREMENT_A INCA RTS Simple Subroutine Note: the parameter to the subroutine is passed, and the result returned through the register A itself. No local variables are used.

  4. Subroutine Call Issues • 1. Parameter passing options: • - use registers • - use the stack • - use global memory • 2. Returning results • - use registers • - use the stack • (caller creates a hole in the stack; • callee places the result in this hole) • - use global memory • 3. Protect registers: • - Any registers that may be “corrupted” by the subroutine must be pushed on the stack first • 4. Local variables allocation • - allocated by the callee • - need to be deallocated before return

  5. Stack Frame Stack used a lot for passing parameters, returning results and storing registers! The stack is therefore organized as a bunch of stack frames, one per subroutine SP Y (or X) Local variables Saved registers Return address Incoming parameters Example of 68HC11 stack frame

  6. * Goal: swap accumulators A and B through a subroutine ORG $C000 START LDS #INITSP JSR SWAP ;call subroutine ... * subroutine SWAP SWAP PSHX ; save register IX DES ; create space for a local var on stack TSX ; copy SP to IX STAA 1,IX ; Save A in the local var LDAA #$0 ; Reset A to 0 ABA ; A+B  A, I.e. move B to A LDAB 1,IX ; Load local var to B INS ; Deallocate local var PULX ; restore register IX RTS ; Return Swap Subroutine

  7. SP 1 byte for local var Upper byte of IX X_H lower byte of IX X_L Stack Frame ret_addr_H upper byte of return address ret_addr_L lower byte of return address Stack frame for Swap Example

  8. * Goal: swap 1-byte passed as params, leave result in A, B ORG $C000 START LDS #INITSP PSHB ; parameter B PSHA ; parameter A JSR NEW_SWAP ;call subroutine ... * subroutine NEW_SWAP NEW_SWAP PSHX ; save register IX DES ; create space for a local var on stack TSX ; copy SP to IX LDAA 6,IX ; load A LDAB 7,IX ; load B STAA 1,IX ; Save A in the local var LDAA #$0 ; Reset A to 0 ABA ; A+B  A, I.e. move B to A LDAB 1,IX ; Load local var to B INS ; Deallocate local var PULX ; restore register IX RTS ; Return Modified Swap Subroutine

  9. SP 1 byte for local var Upper byte of IX X_H lower byte of IX X_L Stack Frame ret_addr_H upper byte of return address ret_addr_L lower byte of return address A Stack positions used to only pass values of A , B as parameters. B Stack frame for New_Swap Example

  10. Local Variables • Allocation: • If less than 5 bytes needed for local variables, use DES instructions • If more than 5 bytes needed for local variables, use: • TSX • XGDX • SUBD #N ; allocate N bytes • XGDX • TXS ; move the stack pointer up by N bytes • De-allocation: • If less than 5 bytes to be deallocated, use INS instructions • If more than 5 bytes to be deallocated, use: • TSX • XGDX • ADDD #N ; deallocate N bytes • XGDX • TXS ; move down the stack pointer by N bytes

  11. Do Activity #1 Now

  12. * Goal: Swap 1-byte passed as params, Return result via stack thru same locations ORG $C000 START LDS #INITSP PSHB ; parameter B PSHA ; parameter A JSR COMPLEX_SWAP ;call subroutine PULA PULB ... Complex Swap Subroutine

  13. * subroutine COMPLEX_SWAP COMPLEX_SWAP PSHX ; save register IX DES ; create space for a local var on stack TSX ; copy SP to IX LDAA 6,IX ; load A LDAB 7,IX ; load B STAA 1,IX ; Save A in the local var LDAA #$0 ; Reset A to 0 ABA ; A+B  A, I.e. move B to A LDAB 1,IX ; Load local var to B STAA 6,IX ; Return swapped value of A thru stack STAB 7,IX ; Return swapped value of B thru stack INS ; Deallocate local var PULX ; restore register IX RTS ; Return Complex Swap Subroutine

  14. SP 1 byte for local var Upper byte of IX X_H lower byte of IX X_L Stack Frame ret_addr_H upper byte of return address ret_addr_L lower byte of return address A Stack positions used to both pass and return A , B B Stack frame for New_Swap Example

  15. SP 6 bytes for local variables Y_H upper byte of Y Y_L lower byte of Y X_H upper byte of X X_L lower byte of X A B ret_addr_H upper byte of return address ret_addr_L lower byte of return address MAT_H upper byte of MAT MAT_L lower byte of MAT N Complex Stack Frame ldaa #N psha ldx #mat pshx bsr subx ... Subx: (1) pshb (2) psha (3) pshx (4) pshy (5) tsx (6) xgdx (7) subd #6 (8) xgdx (9) txs ...

  16. ROM rev. n I-O Utility I/O Subr Entry Pt ROM rev. n+m I-O Utility Jumps to Subr code Use a Jump Table that has a fixed location in ROM Jump Table Buffalo Utilities Input - Output Subroutines are in Buffalo Monitor ROM Problem The actual location in ROM of the Buffalo utility input-output subroutines may be changed with revisions and updates of the Buffalo monitor program Solution The Jump Table is on next slide

  17. Jump Table for I/O Subroutines ROM ROM Memory Addr Contents Comments ffa0 JMP UPCASE lower  UPPER ffa3 JMP WCHEK whitespace  ffa6 JMP DCHEK delimiter  ffac JMP INPUT Read Input ffaf JMP OUTPUT Write Output ffb2 JMP OUTLHLF ffb5 JMP OUTRHLF ffb8 JMP OUTA ffbb JMP OUT1BYT ffbe JMP OUT1BSP ffc1 JMP OUT2BSP ffc4 JMP OUTCRLF ffc7 JMP OUTSTRG ffca JMP OUTSTRG0 ffcd JMP INCHAR More complete descriptions are given in the handout.

  18. Example Use of Utilities These program fragments illustrate how to do I/O *EQUATES INPUT EQU $FFAC INCHAR EQU $FFCD OUTPUT EQU $FFAF OUTSTRG EQU $FFC7 ORG $6000 JMPSTART *DATA SECTION TEXT FCC "Text" FCB $04 PROMPT FCC '> ASCII RMB 1 BUFFER RMB 80

  19. *MAIN START LDX #TEXT JSR OUTSTRG ... LDAA PROMPT JSR OUTPUT ... JSR INPUT STAA ASCII ... LDY #BUFFER LOOP JSR INCHAR STAA 0,Y ... Example Use of Utilities

  20. Do Activity #2 Now • Due: End of Class Today. • RETAIN THE LAST PAGE(S) (#3 onwards)!! • For Next Class: • Bring Huang Textbook, & HC11 PRG • Required Reading: • Sec 4.1-4.7 of Huang • This reading is necessary for getting points in the Studio Activity!

More Related