200 likes | 375 Views
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.
E N D
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 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
* 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.
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
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
* 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
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
* 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
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
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
* 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
* 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
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
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 ...
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
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.
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
*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
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!