ECE 3430 Introduction to Microcomputer Systems University of

  • Slides: 12
Download presentation
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture

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 1

Subroutines - Sub-sections of code that performs specific task. - Modular design flow. -

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 $E 000 : : : Lecture #12 Main Subroutine 1 Subroutine 2 ECE 3430 – Intro to Microcomputer Systems Fall 2009 2

Subroutines Subroutine Call - The HC 11 provides instructions for subroutine calls: BSR JSR

Subroutines Subroutine Call - The HC 11 provides instructions for subroutine calls: BSR JSR <label> Relative addressing. 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). $00 FD XX Return HIGH $00 FE Return LOW $00 FF Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009 SP after JSR/BSR completes 3

Subroutines Subroutine Return - The HC 11 provides the following instruction to return from

Subroutines Subroutine Return - The HC 11 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. $00 FD XX Return HIGH $00 FE Return LOW $00 FF Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009 SP after RTS completes 4

Subroutines Subroutine example MAIN: ADDEM: Label ORG $E 000 LDS LDAA LDAB BSR BRA

Subroutines Subroutine example MAIN: ADDEM: Label ORG $E 000 LDS LDAA LDAB BSR BRA #$00 FF $40 $41 ADDEM MAIN ABA RTS END MAIN: ADDEM: Addr Data INST $E 000 $E 001 $E 002 $E 003 $E 004 $E 005 $E 006 $E 007 $E 008 $E 009 $E 00 A $E 00 B $E 00 C $8 E $00 $FF $96 $40 $D 6 $41 $8 D $REL $20 $REL $1 B $39 LDS #$00 FF LDAA $40 LDAB $41 BSR ADDEM BRA MAIN ABA RTS What address is pushed on the stack when BSR is called? Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009 5

Subroutines Subroutine example MAIN: ADDEM: Label ORG $E 000 LDS LDAA LDAB BSR BRA

Subroutines Subroutine example MAIN: ADDEM: Label ORG $E 000 LDS LDAA LDAB BSR BRA #$00 FF $40 $41 ADDEM MAIN ABA RTS END MAIN: ADDEM: Addr Data INST $E 000 $E 001 $E 002 $E 003 $E 004 $E 005 $E 006 $E 007 $E 008 $E 009 $E 00 A $E 00 B $E 00 C $8 E $00 $FF $96 $40 $D 6 $41 $8 D $02 $20 $F 8 $1 B $39 LDS #$00 FF LDAA $40 LDAB $41 BSR ADDEM BRA MAIN ABA RTS What address is pushed on the stack when BSR is called? $00 FD $00 FE $00 FF Lecture #12 XX $E 0 $09 $E 009 SP after BSR instruction completes ECE 3430 – Intro to Microcomputer Systems Fall 2009 6

Subroutines – Parameter Passing Parameters to Subroutines Parameters can provide input info to a

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

Subroutines – Do No Damage Preserving the Programming Model (Do No Damage Paradigm) If

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. SUB 1: PSHA PSHB ; do no damage: push everything this subroutine uses LDAA LDAB ABA STAA PORTD PULB PULA ; damage no do: pull everything that was previously pushed $0040 RTS This becomes very important when subroutines are calling other subroutines. Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2009 8

Subroutines – Register/Accumulator Parameter Passing Ex 1) MAIN: DONE: Using registers: Output value to

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

Subroutines – Stack Parameter Passing Ex 2) MAIN: DONE: Using the stack: Output value

Subroutines – Stack Parameter Passing Ex 2) MAIN: DONE: Using the stack: Output value to port D: ORG LDAA PSHA JSR PULA BRA $E 000 #10 OUTD ; remove input parameter from stack 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 Lecture #12 Stack ; place input parameter on stack ECE 3430 – Intro to Microcomputer Systems Fall 2009 $00 FA X(high) $00 FB X(low) $00 FC A $00 FD Ret(high) $00 FE Ret(low) $00 FF Input val top bottom 10

Subroutines – Global Memory Parameter Passing Ex 3) OUTVAL: MAIN: DONE: Using global memory:

Subroutines – Global Memory Parameter Passing Ex 3) OUTVAL: MAIN: DONE: Using global memory: Output value to port D: ORG RMB $0040 1 ORG LDAA STAA JSR BRA $E 000 #10 OUTVAL OUTD 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 11

Debugging/Utility Library Subroutine Parameters No Parameters: INIT PAUSE DMPREG OUTCRLF initializes the library pause

Debugging/Utility Library Subroutine Parameters No Parameters: INIT PAUSE DMPREG OUTCRLF initializes the library pause execution until key press dump programming model 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 12