200 likes | 478 Views
Programming Steps:. Assembly Code:. ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop EndLoop: ; do something else. Initialize J Compare J to 10
E N D
Programming Steps: Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop EndLoop: ; do something else • Initialize J • Compare J to 10 • If Not Less than 10, • End Loop • Else • do something • Increment J • Repeat Loop (Step 2) ECE 372 – Microcontroller DesignBasic Assembly Programming For Loop Example: for(j=0; j<10; j++) { // do something }
How do we determine these values? ECE 372 – Microcontroller DesignBasic Assembly Programming Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop EndLoop: ; do something else
ECE 372 – Microcontroller DesignHCS12 Instruction Set Summary Overview … … … …
ECE 372 – Microcontroller DesignBasic Assembly Programming Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop EndLoop: ; do something else
Loop ECE 372 – Microcontroller DesignExecution Time Analysis How long does this loop take to execute? Cycles Loop Cycles = (1+1+1+3)*10 + (1+3) Loop Cycles = 64 cycles Core Clock = 4 MHz Execution Time = 64 * 250 ns = 16000 ns Execution Time = 16 us
ECE 372 – Microcontroller DesignHCS12 Registers - Accumulators • Accumulators • Source and destination of arithmetic operations • A, B are 8-bit registers • D is the combination of A and B • Forms a 16-bit register • A is the MSB and B is the LSB • Used for 16-bit operations ldaa #$5
Is there any difference between the following assembly code examples? ldaa #$50 ldab #$01 vs. vs. ldd #$0150 ldd #$5001 ldaa #$00 ldab #$0A clra ldab #$0A vs. vs. ldd #10 ECE 372 – Microcontroller DesignHCS12 Registers - Accumulators
ECE 372 – Microcontroller DesignHCS12 Registers - CCR • Condition Code Register (CCR) • C – Carry/Borrow • Set when a carry occurs during addition or a borrow occurs during subtraction • O – Overflow • Set in the event of an overflow during an arithmetic operation • Z – Zero • Set when all the bits of the result are 0s • N – Negative • Shows the state of the MSB of the result • N is most commonly used in two’s complement arithmetic (more on this later) • H – Half Carry • Indicates a carry from accumulator A bit 3 during an addition operation • DAA instruction uses the value of the H bit
ECE 372 – Microcontroller DesignHCS12 Registers - CCR • Condition Code Register (CCR) • S – Enable/Disable STOP instruction • Clearing the S bit enables the STOP instruction • Setting the S bit will treat a STOP instruction like a NOP • I, X – Mask IRQ/XIRQ Interrupts • More on these later
ECE 372 – Microcontroller DesignTime for Fun (or maybe not?) 1 2 3 4 5 4 5 1 2 3
Internal System Bus ECE 372 – Microcontroller DesignMC9S12C Block Diagram
ECLK R/W ADDR15:0 DATA15:0 Read initial PC address Read ldaa instruction and immediate value Triggered by Reset ECE 372 – Microcontroller DesignInstruction Execution Timing - Reset $FFFE $4000 $4002 $4000 $8600 $810A
ECLK R/W ADDR15:0 DATA15:0 A = 1 A = 0 Branch not taken, requires only 1 cycles ECE 372 – Microcontroller DesignInstruction Execution Timing – Initial Loop Execution $FFFE $4000 $4002 $4004 $4006 $4000 $8600 $810A $2C04 $8B01
ECLK R/W ADDR15:0 DATA15:0 PC = $4008 + 2 + $F8(-8) = $4002 A = 1 Branch always (BRA) requires 3 cycles ECE 372 – Microcontroller DesignInstruction Execution Timing – BRA Execution $4006 $4008 $4002 $4004 $2C04 $8B01 $20F8 $810A
ECLK R/W ADDR15:0 DATA15:0 PC = $4004 + 2 + $04 = $4010 A>=10, Branch taken, requires 3 cycles ECE 372 – Microcontroller DesignInstruction Execution Timing – Final Loop Execution $4002 $4004 $4010 $810A $2C04 $???? A = 10