ECE 372 Microcontroller Design Basic Assembly Programming For
ECE 372 – Microcontroller Design Basic Assembly Programming For Loop Example: for(j=0; j<10; j++) { PORTT = j; } Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop staa PTT adda #1 bra Loop End. Loop: ; ; ; ; Initialize j Compare j to 10 Else !(j<10) Write J to PORT T Increment j Repeat Loop do something else 1
ECE 372 – Microcontroller Design Basic Assembly Programming Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge End. Loop ; Else !(j<10) staa PTT ; Write J to PORT T adda #1 ; Increment j bra Loop ; Repeat Loop End. Loop: ; do something else $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C $4005 $4006 $4007 $4008 $4009 … $04 $8 B $01 $20 $F 8 Insert staa instruction LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) PC=PC+2+Rel ADDA immediate addressing Value to add to A Branch Always PC=PC+2+Rel 2
ECE 372 – Microcontroller Design Basic Assembly Programming Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop staa PTT adda #1 bra Loop End. Loop: $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $4008 $4009 $400 A $400 B $400 C … $86 $00 $81 $0 A $2 C $04 $7 A $ $ $8 B $01 $20 $F 8 ; ; ; ; Initialize j Compare j to 10 Else !(j<10) Write J to PORT T Increment j Repeat Loop do something else How do we determine this value? LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) PC=PC+2+Rel STAA extended addressing Memory Location ADDA immediate addressing Value to add to A Branch Always PC=PC+2+Rel 3
ECE 372 – Microcontroller Design Basic Assembly Programming Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop staa PTT adda #1 bra Loop End. Loop: $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $4008 $4009 $400 A $400 B $400 C … $86 $00 $81 $0 A $2 C $04 $7 A $02 $40 $8 B $01 $20 $F 8 ; ; ; ; Initialize j Compare j to 10 Else !(j<10) Write J to PORT T Increment j Repeat Loop do something else LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) PC=PC+2+Rel STAA extended addressing Memory Location or PORTT ($240) Do the relative addresses for the BGE and BRA instructions change? Why or why not? ADDA immediate addressing Value to add to A Branch Always PC=PC+2+Rel 4
ECE 372 – Microcontroller Design Basic Assembly Programming Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop staa PTT adda #1 bra Loop End. Loop: $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $4008 $4009 $400 A $400 B $400 C … $86 $00 $81 $0 A $2 C $07 $7 A $02 $40 $8 B $01 $20 $F 5 ; ; ; ; Initialize j Compare j to 10 Else !(j<10) Write J to PORT T Increment j Repeat Loop do something else LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) PC=PC+2+Rel STAA extended addressing Memory Location or PORTT ($240) ADDA immediate addressing Value to add to A Branch Always PC=PC+2+Rel (-11) 5
ECE 372 – Microcontroller Design STAA Instruction Execution Timing $4000 $86 LDAA immediate addressing $4001 $00 Value to be stored in A $4002 $81 CMPA immediate addressing $4003 $0 A Compare Value 10 $4004 $2 C BGE (Branch if greater then or equal to zero) $4005 $07 PC=PC+2+Rel $4006 $7 A STAA extended addressing $4007 $02 Memory Location or PORTT ($240) $4008 $4009 $8 B ADDA immediate addressing $400 A $01 Value to add to A $400 B $20 Branch Always $400 C $F 5 PC=PC+2+Rel (-11) ECLK R/W ADDR 15: 0 DATA 15: 0 $4002 $8600 $810 A $4004 $2 C 06 $4006 $7 A 02 $4007 $0242 $0240 $0000 A = 0 $4009 $8 B 01 A = 1 Store A to PORT T Requires 1 Write Cycle 6
ECE 372 – Microcontroller Design Basic Assembly Programming – Accumulator D For Loop Example: for(j=0; j<1000; j++) { Do we need to use division here? PORTT = j%256; } Assembly Code: ldd #0 Loop: cpd #1000 bge End. Loop stab PTT addd #1 bra Loop End. Loop: ; ; ; ; Initialize j Compare j to 1000 Else !(j<1000) Write J%256 to PORT T Increment j Repeat Loop do something else 7
ECE 372 – Microcontroller Design Basic Assembly Programming – Accumulator D Assembly Code: ldd #0 Loop: cpd #1000 bge End. Loop stab PTT addd #1 bra Loop End. Loop: $4000 $4001 $4002 $4003 $4004 $4005 … $CC $00 $8 C $03 $E 8 ; ; ; ; Initialize j Compare j to 1000 Else !(j<1000) Write J to PORT T Increment j Repeat Loop do something else LDD immediate addressing MSB of value to be stored in D LSB of value to be stored in D CPD immediate addressing Compare to Value 1000 8
ECE 372 – Microcontroller Design Instruction Execution Timing – Accumulator D $4000 $4001 $4002 $4003 $4004 $4005 $CC $00 $8 C $03 $E 8 LDD immediate addressing MSB of value to be stored in D LSB of value to be stored in D CPD immediate addressing Compare to Value 1000 ECLK R/W ADDR 15: 0 $4000 DATA 15: 0 $4001 $CC 00 $4003 $0000 $4004 $8 C 03 $4006 $03 E 8 D = 0 Detect LDD instruction Read immediate value Detect CPD instruction Read immediate value 9
ECE 372 – Microcontroller Design Time for Fun (or maybe not? ) 10 Gallon 7 Gallon 3 Gallon 10
ECE 372 – Microcontroller Design STAA Instruction Execution Timing $4000 $4001 $4002 $4003 $4004 $4005 $CC $00 $8 C $03 $E 8 LDD immediate addressing MSB of value to be stored in D LSB of value to be stored in D CPD immediate addressing Compare to Value 1000 ECLK R/W ADDR 15: 0 DATA 15: 0 $4000 $CC 00 $4001 $0000 How does the CPU execute this internally? 11
ECE 372 – Microcontroller Design CPU Internals Arithmetic Logic Unit PC ALU B A Program Counter IR Instruction Register T Temporary Register D MDR MAR 16 16 ADDR 15: 0 DATA 15: 0 Memory Address Register Memory Data Register 12
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC ALU B A IR D T MAR 16 ADDR 15: 0 MDR 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge End. Loop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop End. Loop: ; do something else 13
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU B: A: $4000 IR: D T: MAR: $FFFE MDR: $4000 16 ADDR 15: 0 $FFFE 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $FFFE $4000 $8600 $4002 $810 A 14
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU A: $00 B: IR: $86 D T: MAR: $4000 $FFFE $4000 $00 MDR: $8600 $4000 16 ADDR 15: 0 $4002 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $FFFE $4000 $8600 $4002 $810 A 15
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU $4004 $4002 Update CCR A: $00 B: IR: $81 $86 D T: MAR: $4002 $4000 MDR: $810 A $8600 16 ADDR 15: 0 $4002 $00 $0 A 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $FFFE $4000 $8600 $4002 $810 A 16
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU $4006 $4004 BGE Not Taken A: $00 B: IR: $2 C $86 D T: MAR: $4004 $4002 MDR: $2 C 04 $810 A 16 ADDR 15: 0 $4004 $00 $04 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $FFFE $4000 $8600 $4002 $810 A $4004 $2 C 04 $4006 $8 B 01 17
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU A: $01 $00 B: IR: $8 B $86 $2 C D T: MAR: $4006 $4002 $4006 $00 $04 $01 MDR: $8 B 01 $2 C 04 $810 A 16 ADDR 15: 0 $4006 $4004 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $FFFE $4000 $8600 $4002 $810 A $4004 $2 C 04 $4006 $8 B 01 18
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU A: $0 A B: IR: $20 $86 $8 B D T: MAR: $4008 $4006 $4008 $00 $01 $F 8 MDR: $20 F 8 $8 B 01 $810 A 16 ADDR 15: 0 $400 A $4008 $4002 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $4006 $8 B 01 $4008 $20 F 8 $4002 $810 A $4004 $2 C 04 19
ECE 372 – Microcontroller Design CPU Internal Execution Timing PC: ALU A: $0 A B: IR: $20 $86 D T: MAR: $4002 $4006 $4002 $00 $F 8 MDR: $20 F 8 $810 A 16 ADDR 15: 0 $4002 16 $4000 $4001 $4002 $4003 $4004 $86 $00 $81 $0 A $2 C LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater then or equal to zero) $4005 $4006 $04 $8 B PC=PC+2+Rel ADDA immediate addressing $4007 $4008 $4009 $01 $20 $F 8 Value to add to A Branch Always PC=PC+2+Rel DATA 15: 0 ECLK R/W ADDR 15: 0 DATA 15: 0 $4006 $8 B 01 $4008 $20 F 8 $4002 $810 A $4004 $2 C 04 20
ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) 21
- Slides: 21