Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3430 – Intro to Microcomputer Systems

Similar presentations


Presentation on theme: "ECE 3430 – Intro to Microcomputer Systems"— Presentation transcript:

1 ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #12 Agenda Today 1) Subroutines - Parameter Passing Techniques - “Do No Damage” Paradigm Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

2 ECE 3430 – Intro to Microcomputer Systems
Subroutines Subroutines - Sub-sections of code that performs specific task. - Modular design flow. - The main program loop contains logical structure of the program. - The subroutines perform the details. - Program design can be divided between multiple engineers. - Subroutines can exist anywhere in memory—but typically they are defined after the main program loop. Memory $E000 : : : Main Subroutine 1 Subroutine 2 Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

3 Subroutines Subroutine Call - The HC11 provides instructions for subroutine calls: BSR <label> Relative addressing. JSR <label> Direct, extended, and indexed addressing. - When these instructions are executed, the following happens: 1) The return address is pushed onto the stack (low byte first, high byte second) The “return address” is the address of the instruction immediately following the BSR or JSR instruction. 2) The program counter (PC) is loaded with the address of the beginning of the subroutine (the address for the subroutine label). $00FD $00FE $00FF SP after JSR/BSR completes XX Return HIGH Return LOW Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

4 ECE 3430 – Intro to Microcomputer Systems
Subroutines Subroutine Return - The HC11 provides the following instruction to return from a subroutine: RTS ; return from subroutine - When this instruction is executed, the following happens: 1) The return address is pulled off of the stack (high byte first, low byte second). 2) The program counter (PC) is loaded with the address that was pulled off of the stack. $00FD $00FE $00FF XX Return HIGH SP after RTS completes Return LOW Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

5 ECE 3430 – Intro to Microcomputer Systems
Subroutines Subroutine example ORG $E000 LDS #$00FF MAIN: LDAA $40 LDAB $41 BSR ADDEM BRA MAIN ADDEM: ABA RTS END What address is pushed on the stack when BSR is called? Label Addr Data INST $E000 $8E LDS #$00FF $E001 $ $E002 $FF - MAIN: $E003 $96 LDAA $40 $E004 $ $E005 $D6 LDAB $41 $E006 $ $E007 $8D BSR ADDEM $E008 $REL - $E009 $20 BRA MAIN $E00A $REL - ADDEM: $E00B $1B ABA $E00C $39 RTS Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

6 Subroutines Subroutine example ORG $E000
LDS #$00FF MAIN: LDAA $40 LDAB $41 BSR ADDEM BRA MAIN ADDEM: ABA RTS END What address is pushed on the stack when BSR is called? $E009 Label Addr Data INST $E000 $8E LDS #$00FF $E001 $ $E002 $FF - MAIN: $E003 $96 LDAA $40 $E004 $ $E005 $D6 LDAB $41 $E006 $ $E007 $8D BSR ADDEM $E008 $ $E009 $20 BRA MAIN $E00A $F8 - ADDEM: $E00B $1B ABA $E00C $39 RTS $00FD $00FE $00FF SP after BSR instruction completes XX $E0 $09 Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

7 Subroutines – Parameter Passing
Passing Parameters to Subroutines Parameters can provide input info to a subroutine or receive output info from a subroutine. 1) Use registers/accumulators - Registers and/or accumulators provide additional info to the subroutine. 2) Use the stack - Values pushed on the stack provide additional info to the 3) Use global memory - Reserved memory locations provide additional info to the Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

8 Subroutines – Do No Damage
Preserving the Programming Model (Do No Damage Paradigm) If not using registers to pass parameters, it is imperative that subroutines do not inadvertently alter the programming model. In other words, the contents of A, B, X, Y, etc should be the same before and after calling a subroutine. SUB1: PSHA ; do no damage: push everything this subroutine uses PSHB LDAA PORTA LDAB PORTD ABA STAA $0040 PULB ; damage no do: pull everything that was previously pushed PULA RTS This becomes very important when subroutines are calling other subroutines. Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

9 Subroutines – Register/Accumulator Parameter Passing
Ex 1) Using registers: Output value to port D: ORG $E000 MAIN: LDAA #10 JSR OUTD DONE: BRA DONE * * Subroutine: Output value to port D OUTD: STAA PORTD RTS Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

10 Subroutines – Stack Parameter Passing
Ex 2) Using the stack: Output value to port D: ORG $E000 MAIN: LDAA #10 PSHA ; place input parameter on stack JSR OUTD PULA ; remove input parameter from stack DONE: BRA DONE * * Subroutine: Output value to port D OUTD: PSHA ; do no damage PSHX TSX ; X now points to top stack element LDAA 5,X STAA PORTD PULX ; damage no do PULA RTS Stack $00FA X(high) top $00FB X(low) $00FC A $00FD Ret(high) $00FE Ret(low) $00FF Input val bottom Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

11 Subroutines – Global Memory Parameter Passing
Ex 3) Using global memory: Output value to port D: ORG $0040 OUTVAL: RMB 1 ORG $E000 MAIN: LDAA #10 STAA OUTVAL JSR OUTD DONE: BRA DONE * * Subroutine: Output value to port D OUTD: PSHA LDAA OUTVAL STAA PORTD PULA RTS Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009

12 Debugging/Utility Library Subroutine Parameters
No Parameters: INIT initializes the library PAUSE pause execution until key press DMPREG dump programming model OUTCRLF move cursor to a new line Global Memory Parameter Passing: DMPMEM dump a range of memory (MEMADDR and MEMSIZE) DMPRM DMPREG + DMPMEM Register/Accumulator Parameter Passing: OUTA outputs hexadecimal representation of ACCA SNDBYTE outputs single ASCII character Stack Parameter Passing: INCHAR returns ASCII for key when key pressed (returns ASCII through stack) OUTSTRG outputs a terminated string (needs parameter telling it where string begins) OUTSTRGN outputs a non-terminated string (needs parameters telling it where string begins and how many characters to print) Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google