H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The...

50
H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State University, Mankato

Transcript of H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The...

Page 1: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-1

The 68HC11 Microcontroller

Chapter 2: 68HC11 Assembly Programming

The 68HC11 Microcontroller

Han-Way Huang

Minnesota State University, Mankato

Page 2: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-2

The 68HC11 Microcontroller

Assembly Program Structure

C program

main (){ int i, j, k; ; i, j, k are integer variables i = 75; ; assign 75 to i j = 10; ; assign 10 to j k = i + j - 6;}

Page 3: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-3

The 68HC11 Microcontroller

Assembly Program

(1) * Data storage declaration section(2) ORG $00(3) i RMB 1 ; variable i(4) j RMB 1 ; variable j(5) k RMB 1 ; variable k(6) * program instruction section(7) start ORG $C000 ; starting address of program(8) LDAA #75(9) STAA i ; initialize i to 75(10) LDAA #10(11) STAA j ; initialize j to 10(12) ADDA i ; compute i + j(13) SUBA #6 ; compute i + j -6(14) STAA k ; store i + j - 6 to k(15) END

Page 4: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-4

The 68HC11 Microcontroller

Global View of a 68HC11 Assembly Program

1. Assembler Directives

- define data and symbol- reserve and initialize memory locations- set assembler and linking condition- specify output format- etc.

2. Assembly Language Instructions

3. END directive

- last statement of a program- any statement after END will be ignored

4. Comments

- explain the function of a single or a group of instructions

Page 5: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-5

The 68HC11 Microcontroller

Fields of a 68HC11 Instruction

1. Label field- is optional- starts with a letter and followed by letters, digits, or special symbols (_ or .)- can start from any column if ended with “:” (not true for Motorola freeware as11)- must start from column 1 if not ended with “:”

2. Operation field- contains the mnemonic of a machine instruction or a directive- is separated from the label by at least one space

3. Operand field- follows the operation field and is separated from the operation field by at least one space- contains operands for instructions or arguments for assembler directives

4. Comment field- a whole line comment starts with a *- is separated from the operand and operation field for at least one space

Page 6: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-6

The 68HC11 Microcontroller

Identify the Four Fields of an Instruction

Example 2.3

loop ADDA #$40 ; add 40 to accumulator A

(1) “loop” is a label(2) “ADDA” is an instruction mnemonic(3) “#$40” is the operand(4) “add #$40 to accumulator A” is a comment

Page 7: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-7

The 68HC11 Microcontroller

Assembler Directives -- a sample

1. END- ends a program to be processed by an assembler- any statement following the END directive is ignored- not supported by the Motorola freeware as11

2. ORG- sets a new value for the location counter of the assembler- tells the assembler where to put the next byte it generates after the ORG directive

The sequenceORG $C000LDAB #$FF

will put the opcode byte for the instruction LDAB #$FF at location $C000.

3. RMB -- reserve memory bytes- reserve memory bytes without initialization- syntax is

[<label>] RMB <expression> [<comment>] The statement

buffer RMB 100allocates 100 bytes for data and can be referred to by the label “buffer”.

Page 8: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-8

The 68HC11 Microcontroller

BSZ -- block storage of zeros

- causes the assembler to allocate a block of bytes that are initialized to zeros- syntax is

[<label>] BSZ <expression> [<comment>]The statement

buffer BSZ 80

reserves a block of 80 bytes and their value are initialized to 0.

FCB -- form constant byte

- reserves as many bytes as the number of arguments in the directive- each argument specifies the initial value of the corresponding byte- syntax is

[<label>] FCB [<expression>][,<expression>,...,<expression>][<comment>]

The statement

ABC FCB $11,$22,$33

reserves three consecutive memory bytes and initializes their values to $11, $22, and $33.

Page 9: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-9

The 68HC11 Microcontroller

FDB -- form double byte- reserves 2 bytes for each argument of the directive- each argument specifies the initial value of the corresponding double bytes- syntax is

[<label>] FDB [<expression>][,<expression>,...,<expression>] [comment>]

The directive

ABC FDB $11,$22,$33

will initialize 6 consecutive bytes in memory to $00 $11 $00 $22 $00 $33

FCC -- form constant character- generates ASCII code bytes for the letters in the arguments- syntax is

[label] FCC “<string>“ [<comment>]

The directive

ALPHA FCC “DEF”

will generate the values $44 $45 $46 in memory

Page 10: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-10

The 68HC11 Microcontroller

DCB -- define constant block- reserve an area of memory and initialize each byte to the same constant value- syntax is

[label] DCB <length>,<value>- not supported by the Motorola freeware as11

The directive

space DCB 80,$20

will generate a line of 80 space characters.

FILL -- fill a block of constant values- serve as the same purpose as does the DCB directive- syntax

[<label>] FILL <value>,<length>

The directive

ones FILL 1,40

will force the freeware assembler to fill each of the 40 memory locations with a 1.

Page 11: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-11

The 68HC11 Microcontroller

EQU -- equate- allows the user to use a symbolic name in place of a number- syntax is

<label> EQU <expression> [<comment>]

The directive

ROM EQU $E000

tells the assembler that wherever ROM appears in the program, the value $E000is to be substituted.

Page 12: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-12

The 68HC11 Microcontroller

Terminal

Process

Input oroutput

Decision

Subroutine

A

B

A

Flowchart

- is a form of program documentation- is a tool for developing program logic flow

Symbols of Flowchart

yes

no On-pageconnector

Off-page connector

Page 13: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-13

The 68HC11 Microcontroller

Procedure of Using Computer in Solving the Problem

start

analyze the problem

Express the solution to theproblem using flowchart orother method

Convert the flowchart into source code

Compile or assemble togenerate machine code

Place the executable code in the computer

Run the program andevaluate the result

Is the resultsatisfactory?

Refine the solution

No

Yes

Stop

Page 14: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-14

The 68HC11 Microcontroller

Programs to do simple arithmetic

Example 2.4 Write a program to add the values of memory locations at $00, $01, and $02, and save the result at $03.

LDAA $00 ; load the contents of memory location at $00 into AADDA $01 ; add the contents of memory location at $01 to AADDA $02 ; add the contents of memory location at $02 to ASTAA $03 ; save the result at memory location at $03

Example 2.5 Write a program to subtract 6 from three 8-bit numbers stored at $00, $01, and$02 respectively.

LDAA $00 ; load the first number into ASUBA #06 ; subtract 6 from the first numberSTAA $00 ; store the decremented value back to $00LDAA $01 ; load the second number into ASUBA #06 ; subtract 6 from the second numberSTAA $01 ; store the decremented value back to $01LDAA $02 ; load the third number into ASUBA #06 ; subtract 6 from the third numberSTAA $02 ; store the decremented value back to $02

Page 15: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-15

The 68HC11 Microcontroller

The Carry Flag

- bit 0 of the CCR register- set to 1 when the addition operation produces a carry 1- set to 1 when the subtraction operation produces a borrow 1- enables the user to implement multi-precision arithmetic

Example 2.6 Write a program to add the 3-byte numbers stored at $00-$02 and$03-$05 and save the result at $06-$08.Solution: The addition starts from the least significant byte.

LDAA $02 ; add the LSBsADDA $05 ; “STAA $08 ; “LDAA $01 ; add the middle bytesADCA $04 ; “STAA $07 ; “LDAA $00 ; add the MSBsADCA $03 ; “STAA $06 ; “

Page 16: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-16

The 68HC11 Microcontroller

Example 2.7 Write a program to subtract the 3-byte number stored at $03-$05 from the 3-byte number stored at $00-$02 and save the result at $10-$12.Solution: The subtraction starts from the LSBs.

LDAA $02 ; subtract the LSBsSUBA $05 ; “STAA $12 ; “LDAA $01 ; subtract the middle bytesSUBA $04 ; “STAA $11 ; “LDAA $00 ; subtract the MSBsSUBA $03 ; “STAA $10 ; “

Page 17: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-17

The 68HC11 Microcontroller

BCD numbers and addition

- each digit is encoded by 4 bits- two digits are packed into one byte- the addition of two BCD numbers is performed by binary addition and an adjust

operation using the DAA instruction- the instruction DAA can be applied after the instructions ADDA, ADCA,

and ABA- simplifies I/O conversion

For example, the instruction sequence

LDAA $00ADDA $01DAASTAA $02

adds the BCD numbers stored at $00 and $01 and saves the sum at $02.

Page 18: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-18

The 68HC11 Microcontroller

Multiplication

- MUL computes the product of A and B and leaves the result in D- multi-byte multiplication can be performed by using a method similar to the

pen-and-pencil method (partial products are generated and added together)

Example 2.10 Multiply the two 16-bit numbers stored at M and N and save the product at location P.

Solution: Rewrite M and N as MHML and NHNL where

MH and NH are upper 8 bits of M and N respectivelyML and NL are lower 8 bits of M and N respectively

MH and ML are stored at M and M+1 respectivelyNH and NL are stored at N and N+1 respectively

Page 19: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-19

The 68HC11 Microcontroller

Illustration of 16-bit by 16-bit Multiplication

MLNL

MHNL

MLNH

MHNH

M × N

8-bit 8-bit 8-bit 8-bit

address P P+1 P+2 P+3

MSB LSB

upper byte lower byte

upper byte lower byte

upper byte lower byte

upper byte lower byte

Page 20: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-20

The 68HC11 Microcontroller

Example 2.10 Program for multiplying two 16-bit numbers

ldaa M+1 ; place ML in A ldab N+1 ; place NL in Bmul ; compute ML × NL

std P+2 ; save ML × NL to memory locations P+2 and P+3ldaa M ; place MH in Aldab N ; place NH in Bmul ; compute MH × NH

std P ; save MH × NH to memory locations P and P+1ldaa M ; place MH in Aldab N+1 ; place NL in Bmul ; compute MH × NL

addd P+1 ; add MH × NL to memory locations P+1 and P+2std P+1 ; “ldaa P ; add the C flag to memory location Padca #0 ; “staa P ; “ldaa M+1 ; place ML in A ldab N ; place NH in Bmul ; compute ML × NH

addd P+1 ; add ML × NH to memory locations P+1 and P+2ldaa P ; add the C flag to memory location Padca ; “staa P ; “

Page 21: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-21

The 68HC11 Microcontroller

Divide Instructions

1. IDIV: integer division

- D is the dividend and X is the divisor- Quotient is left in X and remainder is left in D- The quotient is set to $FFFF in the case of divide by 0

2. FDIV: fractional division

- D is the dividend, X is the divisor- Quotient is left in X and remainder is left in D- The radix point is assumed to be in the same place for both the dividend and divisor- The radix is to the left of bit 15- The dividend must be smaller than the divisor- The quotient is set to $FFFF in the case of divide by 0 or overflow

3. Example 2.12 Divide the fractional number $.2222 by $.4444

LDD #$2222 ; divide $0.2222 by $0.4444LDX #$4444 ; “FIDV ; “

Page 22: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-22

The 68HC11 Microcontroller

The 68HC11 provides swap instructions so that further division to the quotientcan be performed.

- XGDX: swap the contents of D and X- XGDY: swap the contents of D and Y

Example 2.13 Write a program to convert the 16-bit number stored at $00-$01 to BCDformat and store the result at $02-$06. Each BCD digit is stored in one byte.

Solution:

- A binary number can be converted to BCD format by using repeated division by 10.- The largest 16-bit binary number is 65535 which has five decimal digits.- The first division by 10 obtains the least significant digit, the second division by 10

obtains the second least significant digit, and so on.

Page 23: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-23

The 68HC11 Microcontroller

LDD $00 ; place the 16-bit number in DLDX #10IDIV ; compute the least significant digitSTAB $06 ; save the least significant BCD digitXGDX ; place the quotient in DLDX #10IDIV ; compute the second least significant BCD digitSTAB $05 ; save the second least significant BCD digitXGDX ; place the quotient in DLDX #10IDIV ; compute the middle BCD digitSTAB $04 ; save the middle BCD digitXGDXLDX #10IDIV ; compute the second most significant digitSTAB $03 ; the second most significant BCD digitXGDXSTAB $02 ; save the most significant BCD digitEND

Page 24: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-24

The 68HC11 Microcontroller

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 25: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-25

The 68HC11 Microcontroller

Condition Code Register

S X H I N Z V C

- C: carry flag- V: overflow flag- Z: zero flag- N: negative flag- H: half carry flag

Conditional Branch Instruction

[<label>] Bcc rel [<comment>]

where cc is a condition code listed in Table 2.1.

Unconditional Branch Instruction

[<label>] BRA rel [<comment>]

Page 26: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-26

The 68HC11 Microcontroller

Table 2.1 Branch Condition Codes

Condition code Meaning

CC carry clearCS carry setEQ equal to 0GE greater than or equal to 0 (signed comparison)GT greater than 0 (signed comparison)HI higher (unsigned comparison)HS higher or same (unsigned comparison)LE less than or equal to 0LO lower (unsigned comparison)LS lower or same (unsigned comparison)LT less than 0 (signed comparison)MI minus (signed comparison)NE not equal to 0PL plus (signed comparison)VC overflow bit clearVS overflow bit set

Page 27: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-27

The 68HC11 Microcontroller

Conditional Branch Instructions that check only one condition flag

- C flag: BCC branch if C flag is 0BCS branch if C flag is 1BLO branch if C flag is 1BHS branch if C flag is 0

- Z flag BEQ branch if Z flag is 1BNE branch if Z flag is 0

- N flag BPL branch if N flag is 0BMI branch if N flag is 1

- V flag BVS branch if V flag is 1BVC branch if V flag is 0

Conditional Branch Instructions that check more than one condition flag

- BGE branch if (N V) = 0- BGT branch if (Z + (N V)) = 0- BHI branch if (C + Z) = 0- BLE branch if (Z + (N V)) = 1- BLS branch if (C + Z) = 1- BLT branch if (N V) = 1

Page 28: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-28

The 68HC11 Microcontroller

Decrementing and Incrementing Instructions

- DECA: A [A] - 1- DECB: B [B] - 1- DEC opr: mem[opr] [mem[opr]] - 1- DES: SP [SP] - 1 - DEX: X [X] - 1- DEY: Y [Y] - 1- INCA: A AINCB: B B] 1- INC opr: mem[opr] [mem[opr]] + 1- INS: SP [SP] + 1- INX: X [X] + 1- INY: Y [Y] + 1

Note 1. Incrementing and decrementing instructions can be used to update the loopindices.

Note 2. The memory operand opr is specified in either extended or index addressing mode.

Page 29: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-29

The 68HC11 Microcontroller

Example 2.15 Write a program to compute 1 + 2 + ... + 20 and save the sum at $00.

Solution:

Start

i = 0sum = 0

i = i + 1

sum = sum + i

i = 20 ?

Stop

no

yes

* The following program use accumulator B as the loop index* i and A as the sum.

N equ 20

ldab #0 ; initialize loop index i to 0ldaa #0 ; initialize sum to 0

again incb ; increment iaba ; add i to sumcmpb #20 ; compare i with the upper limitbne again ; continue if i is less than 20staa $00 ; save the sumend

Page 30: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-30

The 68HC11 Microcontroller

Example 2.16 Write a program to find the largest number from an array of 208-bit numbers. The array is stored at $00-$13. Save the result at $20.Solution:

Start

array max array [0] i 1

array max < array [i] ?

i = array count - 1?

Stop

i i + 1 array max array [i]yes

no

no

yes

Page 31: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-31

The 68HC11 Microcontroller

* The following program uses A to hold the temporary array max and uses B* as the loop index.

N equ 20 ; array countorg $00

array fcb .... ; array

ldaa array ; set array[0] as the temporary array maxldab #1 ; initialize loop index to 1

loop ldx #array ; point X to array[0]abx ; compute the address of array[i]cmpa 0,X ; compare temp. array max to the next elementbhs chkend ; do we need to update the temporary array max?ldaa 0,X ; update the temporary array max

chkend cmpb #N-1 ; compare loop index with loop limitbeq exit ; is the whole array checked yet?incb ; increment loop indexbra loop

exit staa $20 ; save the array maxend

Page 32: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-32

The 68HC11 Microcontroller

Compare Instructions

- are executed to set the condition flags of the CCR register- are often used to implement the program loop

Instruction format

[<label>] CBA [<comment>] compare A to B[<label>] CMPA opr [<comment>] compare A to a memory location or value[<label>] CMPB opr [<comment>] compare B to a memory location or value[<label>] CPD opr [<comment>] compare D to a memory location or value[<label>] CPX opr [<comment>] compare X to a memory location or value[<label>] CPY opr [<comment>] compare Y to a memory location or value[<label>] TST opr [<comment>] test a memory location for negative or zero[<label>] TSTA [<comment>] test A for negative or zero[<label>] TSTB [<comment>] test B for negative or zero

Table 2.2 68HC11 Compare Instructions

opr is specified in one of the following addressing modes:- EXT- INDX- INDY- IMM (not applicable to “TST opr”)- DIR (not applicable to “TST opr”)

Page 33: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-33

The 68HC11 Microcontroller

Special Conditional Branch Instructions

[<label>] BRCLR (opr) (msk) (rel) [<comment>][<label>] BRSET (opr) (msk) (rel) [<comment>]

where

opr specifies the memory location to be checked and must be specified using eitherthe direct or index addressing mode.

msk is an 8-bit mask that specifies the bits of the memory location to be checked.The bits of the memory byte to be checked correspond to those bit positions that are 1s in the mask.

rel is the branch offset and is specified in the relative mode.

For example, the sequence

ldx #$1000here brclr $30,X %10000000 here

ldaa $31,X

will force the 68HC11 continue to execute the second instruction until the bit 7 is set to 1.

Page 34: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-34

The 68HC11 Microcontroller

Example 2.17 Write a program to compute the sum of the odd numbers in an arraywith 20 8-bit elements. The array is stored at $00-$13. Save the sum at $20-$21.Solution:

Start

sum 0 ptr 0

bit 0 of mem[ptr] = 0?

sum sum + [mem[ptr]]

no

ptr = $13?ptr ptr + 1

Stop

no

yes

Page 35: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-35

The 68HC11 Microcontroller

* The index register X is used as the pointer to the array element.

N equ $13org $20

sum rmb 2

org $C000ldaa #$00staa sum ; initialize sum to 0staa sum+1 ; “ldx #$00 ; point X to array[0]

loop brclr 0,X $01 chkend ; is it an odd number?ldd sum ; add the odd number to the sumaddb 0,X ; “adca #0 ; “std sum ; “

chkend cpx #N ; compare the pointer to the address of the last elementbhs exit ; is this the end?inxbra loop ; not yet done, continue

exit end

Page 36: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-36

The 68HC11 Microcontroller

Instructions for Variable Initialization

1. [<label>] CLR opr [<comment>]

where opr is specified using the extended or index addressing mode. Thespecified memory location is cleared.

2. [<label>] CLRA [<comment>]

Accumulator A is cleared to 0

3. [<label>] CLRB [<comment>]

Accumulator B is cleared to 0

Page 37: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-37

The 68HC11 Microcontroller

Shift and Rotate Instructions

The 68HC11 has shift and rotate instructions that apply to a memory location, accumulators A, B and D. A memory operand must be specified using the extended or index addressing mode.

There are three 8-bit arithmetic shift left instructions:

[<label>] ASL opr [<comment>] -- memory location opr is shifted left one place[<label>] ASLA [<comment>] -- accumulator A is shifted left one place[<label>] ASLB [<comment>] -- accumulator B is shifted left one place

The operation is

C 0b7 ----------------- b0

Page 38: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-38

The 68HC11 Microcontroller

The 68HC11 has arithmetic shift right instructions that apply to a memory location and accumulators A and B.

[<label>] ASR opr [<comment>] -- memory location opr is shifted right one place [<label>] ASRA [<comment>] -- accumulator A is shifted right one place[<label>] ASRB [<comment>] -- accumulator B is shifted right one place

The operation is

Cb7 ----------------- b0

The 68HC11 has one 16-bit arithmetic shift left instruction:

[<label>] ASLD [<comment>]

The operation is

C 0b7 ----------------- b0 b7 ----------------- b0accumulator A accumulator B

Page 39: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-39

The 68HC11 Microcontroller

The 68HC11 has logical shift left instructions that apply to a memory location and accumulators A and B.

[<label>] LSL opr [<comment>] -- memory location opr is shifted left one place [<label>] LSLA [<comment>] -- accumulator A is shifted left one place[<label>] LSLB [<comment>] -- accumulator B is shifted left one place

The operation is

C b7 ----------------- b0 0

The 68HC11 has one 16-bit logical shift left instruction:

[<label>] LSLD [<comment>]

The operation is

C 0b7 ----------------- b0 b7 ----------------- b0

accumulator A accumulator B

Page 40: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-40

The 68HC11 Microcontroller

The 68HC11 has three logical shift right instructions that apply to 8-bit operands.

[<label>] LSR opr [<comment>] -- memory location opr is shifted right one place [<label>] LSRA [<comment>] -- accumulator A is shifted right one place[<label>] LSRB [<comment>] -- accumulator B is shifted right one place

The operation is

Cb7 ----------------- b00

The 68HC11 has one 16-bit logical shift right instruction:

[<label>] LSRD [<comment>]

The operation is

C0 b7 ----------------- b0 b7 ----------------- b0

accumulator A accumulator B

Page 41: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-41

The 68HC11 Microcontroller

The 68HC11 has three rotate left instructions that operate on 9-bit operands.

[<label>] ROL opr [<comment>] -- memory location opr is rotated left one place [<label>] ROLA [<comment>] -- accumulator A is rotated left one place[<label>] ROLB [<comment>] -- accumulator B is rotated left one place

The operation is

Cb7 ----------------- b0

The 68HC11 has three rotate right instructions that operate on 9-bit operands.

[<label>] ROR opr [<comment>] -- memory location opr is rotated right one place [<label>] RORA [<comment>] -- accumulator A is rotated right one place[<label>] RORB [<comment>] -- accumulator B is rotated right one place

The operation is

C b7 ----------------- b0

Page 42: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-42

The 68HC11 Microcontroller

Example 2.18 Suppose that [A] = $74 and C = 1. Compute the new values of A and Cafter the execution of the instruction ASLA.Solution: The operation is

0 1 1 1 0 1 0 0 0

1 1 1 0 1 0 0 00

C A

Result: [A] = %11101000 C = 0

Example 2.19 Suppose that [mem[$00]] = $F6 and C = 1. Compute the new values of mem[$00] and the C flag after the execution of the instruction ASR $00.Solution: The operation is

1 1 1 1 0 1 1 0

1 1 1 1 1 0 1 1

mem[$00] C

0

Result: [mem[$00]] = %11111011 C = 0

Page 43: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-43

The 68HC11 Microcontroller

Example 2.20 Suppose that [mem[$00]] = $F6 and C = 1. Compute the new contents of mem[$00] and the C flag after the execution of the instruction LSR $00.Solution: The operation is

1 1 1 1 0 1 1 0

0 1 1 1 1 0 1 1

mem[$00] C

0

Result: [mem[$00]] = % 01111011 C = 0

0

Example 2.21 Suppose that [B] = $BE and C = 1. Compute the new values of B after the execution of the instruction ROLB.Solution: The operation is

1 0 1 1 1 1 1 0

0 1 1 1 1 1 0 1

B C

1

1

Result: [B] = % 01111101 C = 1

Page 44: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-44

The 68HC11 Microcontroller

Example 2.22 Suppose that [B] = $BE and C = 1. Compute the new values of mem[$00] after the execution of the instruction RORB.Solution: The operation is

1 0 1 1 1 1 1 0

1 1 0 1 1 1 1 1

C B

0

1

Result: [B] = % 11011111 C = 0

Page 45: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-45

The 68HC11 Microcontroller

Example 2.23 Write a program to count the number of 1s in the 16-bit number storedat $00-$01 and save the result in $02.Solution:

* The 16-bit number is shifted to the right up to 16 time or until the shifted value becomes 0.* If the bit shifted out is a 1 then increment the 1s count by 1.

org $C000ldaa #$00 ; initialize the 1s count to 0staa $02 ; “ldd $00 ; place the number in D

loop lsrd ; shift the lsb of D to the C flagbcc testzero ; is the C flag a 0?inc $02 ; increment 1s count if the lsb is a 1

testzero cpd #0 ; check to see if D is already 0bne loopend

Page 46: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-46

The 68HC11 Microcontroller

Shift a multi-byte number

For shifting right

1. The bit 7 of each byte will receive the bit 0 of its immediate left byte with theexception of the most significant byte which will receive a 0.

2. Each byte will be shifted to the right by 1 bit. The bit 0 of the least significant bytewill be lost.

Suppose there is a k-byte number that is stored at loc to loc+k-1.

method for shifting right

Step 1: Shift the byte at loc to the right one place.Step 2: Rotate the byte at loc+1 to the right one place.Step 3: Repeat Step 2 for the remaining bytes.

Page 47: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-47

The 68HC11 Microcontroller

For shifting left

1. The bit 0 of each byte will receive the bit 7 of its immediate right byte with the exception of the least significant byte which will receive a 0.

2. Each byte will be shifted to the left by 1 bit. The bit 7 of the most significant byte will be lost.

Suppose there is a k-byte number that is stored at loc to loc+k-1.

method for shifting left

Step 1: Shift the byte at loc+k-1 to the leftt one place.Step 2: Rotate the byte at loc+K-2 to the left one place.Step 3: Repeat Step 2 for the remaining bytes.

Page 48: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-48

The 68HC11 Microcontroller

Example 2.24 Write a program to shift the 32-bit number stored at $20-$23 to theright four places.Solution:

ldab #4 ; set up the loop countldx #$20 ; use X as the pointer to the left most byte

again lsr 0,Xror 1,Xror 2,Xror 3,Xdecbbne againend

Page 49: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-49

The 68HC11 Microcontroller

Program Execution Time

An easy way to create a delay is to use program loops. Use the instructions in Table 2.3 asan example.

Instruction Execution time (E clock cycles)

BNE <rel> 3DECB 2DEX 3LDAB <imme> 2LDX <imme> 3NOP 2

Table 2.3 Execution times of a sample of instructions

The following instruction sequence takes 5 s to execute for 2 MHz E clock signal.

again nop ; 2 E cyclesnop ; 2 E cyclesdex ; 3 E cyclesbne again ; 3 E cycles

Page 50: H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State.

H. Huang Transparency No.2-50

The 68HC11 Microcontroller

Example 2.25 Write a program loop to create a delay of 100 ms.Solution: A delay of 100 ms can be created by repeating the previous loop 20000 times.

The following instruction sequence creates a delay of 100 ms.ldx #20000

again nopnopdexbne again

Longer delays can be created by using nested program loops.

Example 2.26 Write a program to create a 10-second delay.Solution: A 10-second delay can be created by repeating the loop in example 2.25 100 times.

ldab #100outer ldx #20000inner nop

nopdexbne innerdecbbne outer