Module 1-D Control Structure Applications Tim Rogers 2017 [1.D]-1
Learning Outcome #1 An ability to program a microcontroller to perform various tasks How? A. Architecture and Programming Model B. Instruction Set Overview C. Assembly Control Structures D. Control Structure Applications E. Parameter Passing F. Table Lookup G. Macros and Structured Programming [1.D]-2
Objective Control Structure Applications Why? unsigned int I; for(i=1;i<=10;i++) { <statements>; } Loops are nice Whole N apps equ are? better macme loop org $800 pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) ACM rmb 4 XA fdb?,?,? YA fdb?,?,? [1.D]-3
3 Simple apps 1. Software Delay 2. Extended-precision binary add/subtract 3. Extended-precision decimal add/subtract [1.D]-4
Software Delay Purpose: Do nothing, but for a specific amount of time Fine if you need an imprecise delay : key de-bouncing for instance Why does this end up being imprecise? Parameter-dependent overhead Interrupts Input: (A) = <Delay Time in ms> Passing arguments via registers delay_func Output: nothing [1.D]-5
Software Delay How (and this is just one way to do this): doubly-nested loop Inner loop takes 1ms (will time it based on number of cycles/instruction) Outer loop controls # times we go through inner loop One question: How many times do we need to go around the inner loop get to 1ms? [1.D]-6
Reminder Can find the exact cycle count for each insn in reference manual [1.D]-7
delay_func loopo loopi [X] = cycles pshx [2] psha [2] pshc [2] ldx # 2661 [2] Note: Need to know clock speed of CPU here, assume it is 8 MHz, i.e., each cycle is 125 ns Total Cycles = (A) * [ (X)*3 + 5] + 20 Set (A)=1ms dbne x,loopi [3] 8000 = (1) * [ (X)*3 + 5] + 20 Here, X ~ 2658 10 Set (A)=100ms dbne a,loopo [3] pulc [3] pula [3] pulx [3] rts [5] 800,000 = (100) * [ (X)*3 + 5] + 20 Here, X ~ 2664 10z [1.D]-8
N equ? Some Timing Analysis macme loop org $800 pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) 1. If N=0, the total number of cycles consumed by macme is: A: 55 B: 235 C: 5135 D: 5155 P E: None of these ACM rmb 4 XA fdb?,?,? YA fdb?,?,? [1.D]-9
N equ? Some Timing Analysis macme loop org $800 pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) 1. 2. If N=1, the total number of cycles consumed by macme is: A: 55 P B: 235 C: 5135 D: 5155 E: None of these ACM rmb 4 XA fdb?,?,? YA fdb?,?,? [1.D]-10
N equ? Some Timing Analysis macme loop org $800 pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) 3. 1. If N=10, the total number of cycles consumed by macme is: A: 55 B: 235 P C: 5135 D: 5155 E: None of these ACM rmb 4 XA fdb?,?,? YA fdb?,?,? [1.D]-11
N equ? Some Timing Analysis macme loop org $800 pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) 4. If N=255, the total number of cycles consumed by macme is: A: 55 B: 235 C: 5135 P D: 5155 E: None of these ACM rmb 4 XA fdb?,?,? YA fdb?,?,? [1.D]-12
Extended-Precision (EP) Binary Add Motivation: Want to add numbers that are bigger than 16-bits How? Assume 2 large values are in memory Use CF to propagate the carry of 8-bit adds Sort of like building large adders in HW from small adders Store pointers to the 2 numbers augend (top number), addend (bottom number) Use auto increment/decrement pointers to move addition along [1.D]-13
EP Binary Add (augend) (augend) + (addend) Storing result over one of the inputs Registers Name Value A X Y 11t 0t 51t 50t 100t 101t (augend) (X) + (addend) (Y) --------------- (result) (X) 1 ldaa 0,x 2 adca 1,y+ 3 staa 1,x+ Repeat for size of numbers X Y Memory Addr Value 50t 51t 1t 2t 100t 10t 101t 20t [1.D]-1414
Code EP Binary Add [1.D]-15
Our EP Examples Input: Assumes memory labels point to inputs addn Output: Nothing explicitly passed out, updates memory at label In effect, there really is no argument passing to this function [1.D]-16
Code EP Binary Subtract sbca [1.D]-17
Code EP Decimal Add daa [1.D]-18
EP Decimal Subtract Q: Is this just as easy? No: There is no das (decimal adjust subtraction) So how? Use daa, take the 10 s compliment of subtrahend first (flipping its sign). Then do normal adca + daa. (result) BCD (minuend) BCD + [ 99 BCD (subtrahend) BCD + 1] radix (10 s) complement of subtrahend DAA [1.D]-19
EP Decimal Subtract Add 1 to get radix complement and propagate carry [1.D]-20