ece 372 – microcontroller design basic assembly programming

19
1 ECE 372 – Microcontroller Design Basic Assembly Programming for(j=0; j<10; j++) { // do something } For Loop Example: 1. Initialize J 2. Compare J to 10 3. If Not Less than 10, 1. End Loop 4. Else 1. do something 2. Increment J 3. Repeat Loop (Step 2) 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

Upload: lot

Post on 02-Feb-2016

73 views

Category:

Documents


0 download

DESCRIPTION

Programming Steps:. Assembly Code:. ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j

TRANSCRIPT

Page 1: ECE 372 – Microcontroller Design Basic Assembly Programming

1

ECE 372 – Microcontroller Design Basic Assembly Programming

for(j=0; j<10; j++){ // do something}

For Loop Example:

1. Initialize J2. Compare J to 103. If Not Less than 10,

1. End Loop4. Else

1. do something 2. Increment J3. Repeat Loop (Step 2)

Programming Steps: Assembly Code: ldaa #0 ; Initialize jLoop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat LoopEndLoop: ; do something else

Page 2: ECE 372 – Microcontroller Design Basic Assembly Programming

2

ECE 372 – Microcontroller Design Basic Assembly Programming

$4000

$86 LDAA immediate addressing

$4001

$00 Value to be stored in A

$4002

$ CMPA immediate addressing

$4003

$0A Compare Value 10

$4004

$ BGE (Branch if greater then or equal to zero)

$4005

$04 PC=PC+2+Rel

$4006

$ ADDA immediate addressing

$4007

$01 Value to add to A

$4008

$ Branch Always

$4009

$F8 PC=PC+2+Rel

Assembly Code: ldaa #0 ; Initialize jLoop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat LoopEndLoop: ; do something else

How do we determine these values?

Page 3: ECE 372 – Microcontroller Design Basic Assembly Programming

3

ECE 372 – Microcontroller Design HCS12 Instruction Set Summary Overview

Page 4: ECE 372 – Microcontroller Design Basic Assembly Programming

4

ECE 372 – Microcontroller Design HCS12 Instruction Glossary

Page 5: ECE 372 – Microcontroller Design Basic Assembly Programming

5

ECE 372 – Microcontroller Design HCS12 Opcode Table

Page 6: ECE 372 – Microcontroller Design Basic Assembly Programming

6

ECE 372 – Microcontroller Design Basic Assembly Programming

$4000

$86 LDAA immediate addressing

$4001

$00 Value to be stored in A

$4002

$81 CMPA immediate addressing

$4003

$0A Compare Value 10

$4004

$2C BGE (Branch if greater then or equal to zero)

$4005

$04 PC=PC+2+Rel

$4006

$8B ADDA immediate addressing

$4007

$01 Value to add to A

$4008

$20 Branch Always

$4009

$F8 PC=PC+2+Rel

Assembly Code: ldaa #0 ; Initialize jLoop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat LoopEndLoop: ; do something else

Page 7: ECE 372 – Microcontroller Design Basic Assembly Programming

7

ECE 372 – Microcontroller Design Execution Time Analysis

$4000

$86 LDAA immediate addressing 1

$4001

$00 Value to be stored in A

$4002

$81 CMPA immediate addressing 1

$4003

$0A Compare Value 10

$4004

$2C BGE (Branch if greater then or equal to zero)

3/1

$4005

$04 PC=PC+2+Rel

$4006

$8B ADDA immediate addressing 1

$4007

$01 Value to add to A

$4008

$20 Branch Always 3

$4009

$F8 PC=PC+2+Rel

How long does this loop take to execute? Cycles

Loop Cycles = (1+1+1+3)*10 + (1+3)Loop Cycles = 64 cyclesCore Clock = 4 MHzExecution Time = 64 * 250 ns = 16000 ns Execution Time = 16 us

Loop

Page 8: ECE 372 – Microcontroller Design Basic Assembly Programming

8

ECE 372 – Microcontroller Design HCS12 Registers

Page 9: ECE 372 – Microcontroller Design Basic Assembly Programming

9

ECE 372 – Microcontroller Design HCS12 Registers - Accumulators

ldaa #$5

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

Page 10: ECE 372 – Microcontroller Design Basic Assembly Programming

10

ECE 372 – Microcontroller Design HCS12 Registers - Accumulators

ldaa #$50ldab #$01

ldd #$0150

Is there any difference between the following assembly code examples?

vs. ldd #$5001vs.

ldaa #$00ldab #$0A

ldd #10vs. clra ldab #$0Avs.

Page 11: ECE 372 – Microcontroller Design Basic Assembly Programming

11

ECE 372 – Microcontroller Design HCS12 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

Page 12: ECE 372 – Microcontroller Design Basic Assembly Programming

12

ECE 372 – Microcontroller Design HCS12 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

Page 13: ECE 372 – Microcontroller Design Basic Assembly Programming

13

ECE 372 – Microcontroller Design Time for Fun (or maybe not?)

1

1

2

2 3

3

4 45 5

Page 14: ECE 372 – Microcontroller Design Basic Assembly Programming

14

ECE 372 – Microcontroller Design MC9S12C Block Diagram

Page 15: ECE 372 – Microcontroller Design Basic Assembly Programming

15

ECE 372 – Microcontroller Design MC9S12C Block Diagram

Internal System Bus

Page 16: ECE 372 – Microcontroller Design Basic Assembly Programming

16

ECE 372 – Microcontroller Design Instruction Execution Timing - Reset

ECLK

ADDR15:0

R/W

DATA15:0

$FFFE

$4000

$4000

$8600

$4000 $86 LDAA immediate addressing

$4001 $00 Value to be stored in A

$4002 $81 CMPA immediate addressing

$4003 $0A Compare Value 10

$4004 $2C BGE (Branch if greater then or equal to zero)

$4005 $04 PC=PC+2+Rel

$4006 $8B ADDA immediate addressing

$4007 $01 Value to add to A

$4008 $20 Branch Always

$4009 $F8 PC=PC+2+Rel

$810A

$4002

Triggered by ResetRead initial PC address

Read ldaa instruction and immediate value

Page 17: ECE 372 – Microcontroller Design Basic Assembly Programming

17

ECE 372 – Microcontroller Design Instruction Execution Timing – Initial Loop Execution

ECLK

ADDR15:0

R/W

DATA15:0

$FFFE

$4000

$4000

$8600

$4000

$86

LDAA immediate addressing

$4001

$00

Value to be stored in A

$4002

$81

CMPA immediate addressing

$4003

$0A

Compare Value 10

$4004

$2C

BGE (Branch if greater then or equal to zero)

$4005

$04

PC=PC+2+Rel

$4006

$8B

ADDA immediate addressing

$4007

$01

Value to add to A

$4008

$20

Branch Always

$4009

$F8 PC=PC+2+Rel

$810A

$4002 $4004

A = 0

$2C04

$4006

$8B01

Branch not taken, requires only 1

cycles

A = 1

Page 18: ECE 372 – Microcontroller Design Basic Assembly Programming

18

ECE 372 – Microcontroller Design Instruction Execution Timing – BRA Execution

ECLK

ADDR15:0

R/W

DATA15:0

$4006 $4008

$8B01

$4000

$86

LDAA immediate addressing

$4001

$00

Value to be stored in A

$4002

$81

CMPA immediate addressing

$4003

$0A

Compare Value 10

$4004

$2C

BGE (Branch if greater then or equal to zero)

$4005

$04

PC=PC+2+Rel

$4006

$8B

ADDA immediate addressing

$4007

$01

Value to add to A

$4008

$20

Branch Always

$4009

$F8 PC=PC+2+Rel

$20F8

$4002

$810A

$4004

$2C04

Branch always (BRA) requires 3 cycles

PC = $4008 + 2 + $F8(-8) = $4002

A = 1

Page 19: ECE 372 – Microcontroller Design Basic Assembly Programming

19

ECE 372 – Microcontroller Design Instruction Execution Timing – Final Loop Execution

ECLK

ADDR15:0

R/W

DATA15:0

$4002 $4004

$810A

$4000

$86

LDAA immediate addressing

$4001

$00

Value to be stored in A

$4002

$81

CMPA immediate addressing

$4003

$0A

Compare Value 10

$4004

$2C

BGE (Branch if greater then or equal to zero)

$4005

$04

PC=PC+2+Rel

$4006

$8B

ADDA immediate addressing

$4007

$01

Value to add to A

$4008

$20

Branch Always

$4009

$F8 PC=PC+2+Rel

$2C04

$4010

$????

PC = $4004 + 2 + $04 = $4010

A = 10

A>=10, Branch taken, requires 3

cycles