Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structure Applications

Similar presentations


Presentation on theme: "Control Structure Applications"— Presentation transcript:

1 Control Structure Applications
Module 1-D Control Structure Applications Tim Rogers 2017

2 Learning Outcome #1 Architecture and Programming Model
“An ability to program a microcontroller to perform various tasks” Architecture and Programming Model Instruction Set Overview Assembly Control Structures Control Structure Applications Parameter Passing Table Lookup Macros and Structured Programming How?

3 Whole “apps” are better…
Objective Whole “apps” are better… N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop 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 ?,?,? “Control Structure Applications” Why? Loops are nice unsigned int I; for(I=1;I<=10;I++) { <statements>; }

4 3 Simple apps Software Delay Extended-precision binary add/subtract
Extended-precision decimal add/subtract

5 Software Delay Input: (A) = <Delay Time in ms> Output: nothing
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> delay_func Passing arguments via registers Output: nothing

6 How many times do we need to go around the inner loop get to 1ms?
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?

7 Reminder Can find the exact cycle count for each insn in reference manual

8 [X] = cycles delay_func pshx [2] psha [2] pshc [2] loopo
Note: Need to know clock speed of CPU– here, assume it is 8 MHz, i.e., each cycle is 125 ns [X] = cycles delay_func pshx [2] psha [2] pshc [2] Total Cycles = (A)  [ (X)3 + 5] + 20 loopo Set (A)=1ms ldx #_______ [2] 2661 loopi dbne x,loopi [3] 8000 = (1)  [ (X)3 + 5] + 20 Here, X ~ Set (A)=100ms dbne a,loopo [3] pulc [3] pula [3] pulx [3] rts [5] 800,000 = (100)  [ (X)3 + 5] + 20 Here, X ~ z

9 Some Timing Analysis N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop 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 ?,?,? If N=0, the total number of cycles consumed by “macme” is: A: 55 B: 235 C: 5135 D: 5155 E: None of these D = * = 5155

10 Some Timing Analysis N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop 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 ?,?,? If N=1, the total number of cycles consumed by “macme” is: A: 55 B: 235 C: 5135 D: 5155 E: None of these A = = 55

11 Some Timing Analysis N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop 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 ?,?,? If N=10, the total number of cycles consumed by “macme” is: A: 55 B: 235 C: 5135 D: 5155 E: None of these B = 21+10*20+14 = 235

12 Some Timing Analysis N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop 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 ?,?,? If N=255, the total number of cycles consumed by “macme” is: A: 55 B: 235 C: 5135 D: 5155 E: None of these C = * = 5135

13 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

14 EP Binary Add Addr Value X Registers ldaa 0,x Name Value Y A adca 1,y+
Memory Addr Value (augend)  (X) + (addend)  (Y) (result)  (X) (augend)  (augend) + (addend) Storing result over one of the inputs X 1t 1t 50t Registers 51t 2t ldaa 0,x Name Value 1 Y 10t A adca 1,y+ 100t 11t 11t 0t 2 101t 20t X staa 1,x+ 51t 50t 3 Y 100t 101t Repeat for size of numbers 14

15 Code EP Binary Add

16 In effect, there really is no argument passing to this function
EP Binary Add 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

17 Code EP Binary Subtract
sbca

18 Code EP Decimal Add daa

19 EP Decimal Subtract DAA Q: Is this just as easy? So how?
No: There is no das (decimal adjust subtraction) Q: Is this just as easy? Use daa, take the 10’s compliment of subhend first So how? (result)BCD  (minuend)BCD + [ 99BCD – (subhend)BCD + 1] radix (10’s) complement of subtrahend DAA

20 EP Decimal Subtract Add 1 to get radix complement and propagate carry


Download ppt "Control Structure Applications"

Similar presentations


Ads by Google