Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

3 3 Simple Subroutine * 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 Note: the parameter to the subroutine is passed, and the result returned through the register A itself. No local variables are used.

4 4 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 Subroutine Call Issues

5 5 Local variables SP Saved registers Return address Incoming parameters Y (or X) Example of 68HC11 stack frame 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

6 6 Swap Subroutine * 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

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

8 8 Modified Swap Subroutine * 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

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

10 10 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 Local Variables

11 11 Do Activity #1 Now

12 12 Complex Swap Subroutine * 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...

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

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

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

16 16 Buffalo Utilities 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 Input - Output Subroutines are in Buffalo Monitor ROM ROM rev. n ROM rev. n I-O Utility ROM rev. n+m ROM rev. n+m I-O Utility The Jump Table is on next slide Jump Table Jump Table fixed location Use a Jump Table that has a fixed location in ROM Solution Jumps to Subr code I/O Subr Entry Pt

17 17 Jump Table for I/O Subroutines ROMROM Memory AddrContentsComments ffa0JMP UPCASElower  UPPER ffa3JMPWCHEKwhitespace ffa6JMPDCHEKdelimiter ffacJMPINPUTRead Input ffafJMP OUTPUTWrite Output ffb2JMP OUTLHLF ffb5JMP OUTRHLF ffb8JMPOUTA ffbbJMP OUT1BYT ffbeJMP OUT1BSP ffc1JMP OUT2BSP ffc4JMP OUTCRLF ffc7JMP OUTSTRG ffcaJMP OUTSTRG0 ffcdJMP INCHAR More complete descriptions are given in the handout.

18 18 Example Use of Utilities These program fragments illustrate how to do I/O *EQUATES INPUTEQU$FFAC INCHAREQU$FFCD OUTPUTEQU$FFAF OUTSTRGEQU$FFC7 ORG$6000 JMPSTART *DATA SECTION TEXTFCC"Text" FCB$04 PROMPTFCC'> ASCIIRMB1 BUFFERRMB80

19 19 Example Use of Utilities *MAIN START STARTLDX #TEXT JSROUTSTRG... LDAAPROMPT JSROUTPUT... JSRINPUT STAAASCII... LDY#BUFFER LOOPJSRINCHAR STAA0,Y...

20 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!


Download ppt "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."

Similar presentations


Ads by Google