H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage...

13
H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning ram Loops of program loops: finite and infinite loops ing Mechanisms statement S forever r i = n1 to n2 do statement S or For i = n2 downto n1 do statement S ile C do statement S peat statement S until C am loops are implemented by using the conditional branch instructions and xecution of these instructions depends on the contents of the CCR register.

Transcript of H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage...

Page 1: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-1

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Program Loops

Types of program loops: finite and infinite loops

Looping Mechanisms

1. do statement S forever

2. For i = n1 to n2 do statement S or For i = n2 downto n1 do statement S

3. While C do statement S

4. Repeat statement S until C

Program loops are implemented by using the conditional branch instructions and the execution of these instructions depends on the contents of the CCR register.

Page 2: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-2

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Figure 2.4 An infinite loop

SC S

true

false

Figure 2.6 The While ... Do looping construct

Figure 2.7 The Repeat ... Until looping construct

initialize C

S

Ctrue

false

Figure 2.5 For looping construct

i n1

i n2 ?

S

yes

i i + 1

(a) For i = n1 to n2 DO S

no

i n2

i n1 ?

S

yes

i i - 1

(b) For i = n2 downto n1 DO S

no

Page 3: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-3

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Condition Code Register

S X I

7 6 5 4 3 2 1 0

Figure 2.8 Condition code register

H N Z V C

Four types of branch instructions:

- Unary (unconditional) branch: always execute

- Simple branches: branch is taken when a specific bit of CCR is in a specific status

- Unsigned branches: branches are taken when a comparison or test of unsigned numbers

results in a specific combination of CCR bits

- Signed branches: branches are taken when a comparison or test of signed quantities are in

a specific combination of CCR bits

Two categories of branches

- Short Branches: in the range of -128 ~ +127 bytes

- Long Branches: in the range of 64KB

Page 4: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-4

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Table 2.2 Summary of short and long branch instructions

Mnemonic Function

Unary Branches

bra rel8 or lbra rel16brn rel8 or lbrn rel16

Branch alwaysBranch never

Equation or Operation

Simple Branches

Mnemonic Function

bcc rel8 or lbcc rel16bcs rel8 or lbcs rel16beq rel8 or lbeq rel16bmi rel8 or lbmi rel16bne rel8 or lbne rel16bpl rel8 or lbpl rel16bvc rel8 or lbvc rel16bvs rel8 or lbvs rel16

Branch if carry clearBranch if carry setBranch if equalBranch if minusBranch if not equalBranch if plusBranch if overflow clearBranch if overflow set

1 = 11 = 0

Unsigned Branches

Mnemonic Function

bhi rel8 or lbhi rel16bhs rel8 or lbhs rel16blo rel8 or lblo rel16bls rel8 or lbls rel16

Branch if higherBranch if higher or sameBranch if lowerBranch if lower or same

C = 0C = 1Z = 1N = 1Z = 0N = 0V = 0V = 1

Equation or Operation

Equation or Operation

C + Z = 0C = 0C = 1

C + Z = 1

Mnemonic Function Equation or Operation

Signed Branches

bge rel8 or lbge rel16bgt rel8 or lbgt rel16ble rel8 or lble rel16blt rel8 or lblt rel16

Branch if greater than or equalBranch if greater thanBranch if less than or equalBranch if less than

N V = 0Z + (N V) = 0Z + (N V) = 1

N V = 1

Note. 1. Each row contains two branch instructions that are separated by the word ‘or’2. The instruction to the left of ‘or’ is a short branch with 8-bit offset..3. The instruction to the right of ‘or’ is a long branch with 16-bit offset.

Page 5: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-5

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Compare and Test Instructions

- Condition flags need to be set up before conditional branch instruction should be executed.

- The HCS12 provides a group of instructions for testing the condition flags.

Table 2.3 Summary of compare and test instructions

Mnemonic Function

Compare Instructions

cbacmpa <opr>cmpb <opr>cpd <opr>cps <opr>cpx <opr>cpy <opr>

Compare A to BCompare A to memoryCompare B to memoryCompare D to memoryCompare SP to memoryCompare X to memoryCompare Y to memory

Operation

(A) - (B)(A) - (M)(B) - (M)

(D) - (M:M+1)(SP) - (M:M+1)(X) - (M:M+1)(Y) - (M:M+1)

Test instructions

Mnemonic Function

tst <opr>tstatstb

Test memory for zero or minusTest A for zero or minusTest B for zero or minus

Operation

(M) - $00(A) - $00(B) - $00

Page 6: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-6

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Loop Primitive Instructions

- HCS12 provides a group of instructions that either decrement or increment a loop count

to determine if the looping should be continued.

- The range of the branch is from $80 (-128) to $7F (+127).

Table 2.5 Summary of loop primitive instructions

Mnemonic Function

dbeq cntr, rel

dbne cntr, rel

ibeq cntr, rel

ibne cntr, rel

tbeq cntr, rel

tbne cntr, rel

Decrement counter and branch if = 0(cntr = A, B, D, X, Y, or SP)

Decrement counter and branch if 0(cntr = A, B, D, X, Y, or SP)

Increment counter and branch if = 0(cntr = A, B, D, X, Y, or SP)

Increment counter and branch if 0(cntr = A, B, D, X, Y, or SP)

Test counter and branch if = 0(cntr = A, B, D, X, Y, or SP)

Test counter and branch if 0(cntr = A, B, D, X, Y, or SP)

Equation or Operation

cntr (cntr) - 1If (cntr) = 0, then branchelse continue to next instruction cntr (cntr) - 1If (cntr) 0, then branchelse continue to next instructioncntr (cntr) + 1If (cntr) = 0, then branchelse continue to next instructioncntr (cntr) + 1If (cntr) 0, then branchelse continue to next instructionIf (cntr) = 0, then branchelse continue to next instruction

If (cntr) 0, then branchelse continue to next instruction

Note. 1. cntr is the loop counter and can be accumulator A, B, or D and register X, Y, or SP. 2. rel is the relative branch offset and is usually a label

Page 7: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-7

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Implementation of Looping Constructsfor I = n1 to n2 do S

n1 equ xx ; starting indexn2 equ yy ; ending index

…i ds.b 1 ; loop index variable

…movb #n1,i ; initialize i to n1

loopf ldaa i ; check index icmpa #n2bgt next ; if i is greater than n2, exit the loop… ; performs loop operations… ; “inc i ; increment loop indexbra loopf

next …

Page 8: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-8

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

for i = n2 downto n1 do S

n1 equ xx ; starting indexn2 equ yy ; ending index

…i ds.b 1 ; loop index variable

…movb #n2,i ; initialize i to n1

loopf ldaa i ; check index icmpa #n2blt next ; if i is greater than n2, exit the loop… ; perform loop operations… ; “dec i ; increment loop indexbra loopf

next …

Page 9: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-9

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

While loop Loop terminating condition is checked at the start of the loop In the following example, icount == 0 is the condition to be check. The update of icount is done by an interrupt service routine (not shown below).

N equ xx…

icount ds.b 1…movb #N,icount

wloop ldaa #0cmpa icountbeq next……bra wloop

next …

Page 10: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-10

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Repeat S until C Often used when a certain operation need to be performed a fixed number of times The template of this looping construct is as follows:

N equ xx……ldy #N ; Y is used as the loop counter

loopr … ; perform the desired operations… ; “dbne Y,loopr ; decrement the loop counter and decide whether to

; to continue

Page 11: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-11

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Example 2.14 Write a program to add an array of N 8-bit numbers and store the sum at memory locations $1000~$1001. Use the For i = n1 to n2 do looping construct.Solution:

Start

i 0sum 0

i = N?

no

sum sum + array[i]

i i + 1

Stop

Figure 2.9 Logic flow of example 2.14

yes

N equ 20org $1000

sum ds.b 2i ds.b 1

org $1500movb #0,imovw #0,sum ; sum 0

loop ldab icmpb #N ; is i = N?beq doneldx #arrayabxldab 0,X ; sum sum + array[i]ldy sum ; “aby ; “sty sum ; “inc ibra loop

done swi

Page 12: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-12

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

array dc.b 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20end

Example 2.15 Write a program to find the maximum element from an array of N 8-bit elements using the repeat S until C looping construct.Solution:

Page 13: H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

H. Huang Transparency No.2-13

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

N equ 20org $1000

max_val ds.b 1org $1500ldaa array ; set array[0] as the temporary max maxstaa max_val ; “ldx #array+N-1 ; start from the end of the array ldab #N-1 ; set loop count to N - 1

loop ldaa max_valcmpa 0,xbge chk_endldaa 0,xstaa max_val ; update the array max

chk_end dex ; move to the next array elementdbne b,loop ; finish all the comparison yet?

forever bra foreverarray db 1,3,5,6,19,41,53,28,13,42,76,14

db 20,54,64,74,29,33,41,45end