YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Chapter 2: HCS12 Assembly Programming

EE383: Introduction to Embedded SystemsUniversity of Kentucky

Samir Rawashdeh

With slides based on material by H. Huang – Delmar Cengage Learning

1

Page 2: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Three Sections of a HCS12/MC9S12 Assembly Program

1. Assembler Directives

- Define data and symbol- Reserve and initialize memory locations- Set assembler and linking condition- Specify output format- Specifies the end of a program.

2. Assembly Language Instructions

- HCS12/MC9S12 instructions

3. Comments

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

Page 3: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Identify the Four Fields of an Instruction

Example

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

movb 0,X,0,Y ; memory to memory copy

(1) no label field(b) “movb” is an instruction mnemonic(c) “0,X,0,Y” is the operand field(d) “; memory to memory copy” is a comment

Page 4: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Fields of a HCS12 Instruction

1. Label field- Optional- Starts with a letter and followed by letters, digits, or special symbols (_ or .)- Can start from any column if ended with “:”- Must start from column 1 if not ended with “:”

2. Operation field- Contains the mnemonic of a machine instruction or an assembler directive- 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- Any line starts with an * or ; is a comment- Separated from the operand and operation field for at least one space- Optional

Page 5: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Assembler Directives

1. end- ends a program to be processed by an assembler- any statement following the end directive is ignored

2. org- The assembler uses a location counter to keep track of the memory location where the

next machine code byte should be placed.- This directive sets a new value for the location counter of the assembler.

The sequence

org $1000ldab #$FF

places the opcode byte for the instruction ldab #$FF at location $1000.

Page 6: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

dc.b (define constant byte)db (define byte)fcb (form constant byte)

- These three directives define the value of a byte or bytes that will be placed at a given location.

- These directives are often preceded by the org directive.- For example,

org $800array dc.b $11,$22,$33,$44

dc.w (define constant word)dw (define word)fdb (form double bytes)

- Define the value of a word or words that will be placed at a given location.- The value can be specified by an expression.- For example,

org $1000vec_tab dc.w $1234, $5620

Page 7: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

fcc (form constant character)

- Used to define a string of characters (a message).

- The first character (and the last character) is used as the delimiter.

- The last character must be the same as the first character.

- The delimiter must not appear in the string.

- The space character cannot be used as the delimiter.

- Each character is represented by its ASCII code.

- For example,

msg fcc “Please enter 1, 2 or 3:”

Page 8: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

ds (define storage)

rmb (reserve memory byte)

ds.b (define storage bytes)

- Each of these directives reserves a number of bytes given as the arguments to the

directive.

- For example,

buffer ds 100

reserves 100 bytes

Page 9: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

ds.w (define storage word)

rmw (reserve memory word)

- Each of these directives increments the location counter by the value indicated in the

number-of-words argument multiplied by two.

- For example,

dbuf ds.w 20

reserves 40 bytes starting from the current location counter.

equ (equate)

- This directive assigns a value to a label.

- Using this directive makes one’s program more readable.

- Examples

arr_cnt equ 100

oc_cnt equ 50

Page 10: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Macro

- A name assigned to a group of instructions- Use macro and endm to define a macro.- Example of macro

sumOf3 macro arg1,arg2,arg3ldaa arg1adda arg2adda arg3endm

- Invoke a defined macro: write down the name and the arguments of the macro

sumOf3 $1000,$1001,$1002

is replaced by ldaa $1000adda $1001adda $1002

Page 11: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Software Development Process1 Problem definition: Identify what should be done

2 Develop the algorithm. Algorithm is the overall plan for solving the problem at hand.

- An algorithm is often expressed in the following format:

Step 1

Step 2

- Another way to express the overall plan is to use flowchart.

3 Programming. Convert the algorithm or flowchart into programs.

4 Program Testing.

5 Program maintenance.

Page 12: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Symbols of Flowchart

Terminal

Process

Input oroutput

Decisionyes

no

Subroutine

A

B

A

on-page connector

off-page connector

Figure 2.1 Flowchart symbols used in this book

Page 13: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Programs to do simple arithmetic

Example 2.4 Write a program to add the values of memory locations at $1000, $1001, and $1002, and save the result at $1100.Solution:Step 1 A m[$1000]Step 2A A + m[$1001]Step 3A A + m[$1002]Step 4$1100 A

org $1500ldaa $1000adda $1001adda $1002staa $1100end

Page 14: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.4 Write a program to subtract the contents of the memory location at $1005 from the sum of the memory locations at $1000 and $1002, and store the difference at $1100.

Solution:

org $1500

ldaa $1000

adda $1002

suba $1005

staa $1100

end

Page 15: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.6 Write a program to add two 16-bit numbers that are stored at $1000-$1001

and $1002-$1003 and store the sum at $1100-$1101.

Solution:

org $1500

ldd $1000

addd $1002

std $1100

end

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

Page 16: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.7 Write a program to add two 4-byte numbers that are stored at $1000-$1003and $1004-$1007, and store the sum at $1010-$1013.Solution: Addition starts from the LSB and proceeds toward MSB.

org $1500ldd $1002 ; add and save the least significant two bytesaddd $1006 ; “std $1012 ; “

ldaa $1001 ; add and save the second most significant bytesadca $1005 ; “staa $1011 ; “

ldaa $1000 ; add and save the most significant bytesadca $1004 ; “staa $1010 ; “end

Page 17: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.8 Write a program to subtract the hex number stored at $1004-$1007 from the the hex number stored at $1000-$1003 and save the result at $1100-$1103.Solution: The subtraction starts from the LSBs and proceeds toward the MSBs.

org $1500ldd $1002 ; subtract and save the least significant two bytessubd $1006 ; “std $1102 ; “

ldaa $1001 ; subtract and save the difference of the second to mostsbca $1005 ; significant bytesstaa $1001 ; “

ldaa $1000 ; subtract and save the difference of the most significant sbca $1004 ; bytesstaa $1100 ; “end

Page 18: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Multiplication and Division

Page 19: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.10 Write an instruction sequence to multiply the 16-bit numbers stored at $1000-$1001 and $1002-$1003 and store the product at $1100-$1103.

Solution:ldd $1000ldy $1002emulsty $1100std $1102

Example 2.11 Write an instruction sequence to divide the signed 16-bit number stored at $1020-$1021 into the 16-bit signed number stored at $1005-$1006 and store the quotient and remainder at $1100 and $1102, respectively.

Solution:ldd $1005ldx $1020idivsstx $1100 ; store the quotientstd $1102 ; store the remainder

Page 20: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Illustration of 32-bit by 32-bit Multiplication

- Two 32-bit numbers M and N are divided into two 16-bit halves

M = MHML

N = NHNL

Page 21: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 a 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 $1000adda $1001daastaa $1002

adds the BCD numbers stored at $1000 and $1001 and saves the sum at $1002.

Page 22: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.13 Write a program to convert the 16-bit number stored at $1000-$1001 to BCD format and store the result at $1010-$1014. Convert each BCD digit into its ASCII code and store it 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 generates the least significant digit, the second division by 10

obtains the second least significant digit, and so on.org $1000

data dc.w 12345 ; data to be testedorg $1010

result ds.b 5 ; reserve bytes to store the resultorg $1500ldd dataldy #resultldx #10idivaddb #$30 ; convert the digit into ASCII codestab 4,Y ; save the least significant digitxgdxldx #10

Page 23: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

idivadcb #$30stab 3,Y ; save the second to least significant digitxgdxldx #10idivaddb #$30stab 2,Y ; save the middle digitxgdxldx #10idivaddb #$30stab 1,Y ; save the second most significant digitxgdxaddb #$30stab 0,Y ; save the most significant digitend

Page 24: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

ASCII Table

Page 25: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 26: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...
Page 27: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Condition Code Register

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

Page 28: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...
Page 29: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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.

Page 30: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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).

Page 31: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 32: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 n2

loopf ldaa i ; check index icmpa #n1blt next ; if i is less than n1, exit the loop… ; perform loop operations… ; “dec i ; increment loop indexbra loopf

next …

Page 33: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 34: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 35: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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:

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 36: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 37: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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

Page 38: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Bit Condition 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, extended, 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 8-bit relative mode.

For example, in the sequence

loop inc count…brclr $66,$e0,loop…

the branch will be taken if the most significant three bits at $66 are all zeros.

Page 39: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.17 Write a program to compute the number of elements that are divisible by 4 in an array of N 8-bit elements. Use the repeat S until C looping construct.Solution: A number divisible by 4 would have the least significant two bits equal 0s.

N equ 20org $1000

total ds.b 1org $1500clr total ; initialize total to 0ldx #arrayldab #N ; use B as the loop count

loop brclr 0,x,$03,yes ; check bits 1 and 0bra chkend

yes inc totalchkend inx

dbne b,loopforever bra foreverarray db 2,3,4,8,12,13,19,24,33,32,20,18,53,52,80,82,90,94,100,102

end

Page 40: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Instructions for Variable Initialization

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

where opr is specified using the extended or index addressing modes. 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 41: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Shift and Rotate Instructions

The HCS12 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 modes.

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 42: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

The HCS12 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 HCS12 has one 16-bit arithmetic shift left instruction:

[<label>] asld [<comment>]

The operation is

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

Page 43: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

The HCS12 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 HCS12 has one 16-bit logical shift left instruction:

[<label>] lsld [<comment>]

The operation is

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

Page 44: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

The HCS12 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 HCS12 has one 16-bit logical shift right instruction:

[<label>] lsrd [<comment>]

The operation is

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

accumulator A accumulator B

Page 45: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

The HCS12 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 HCS12 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 46: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.18 Suppose that [A] = $95 and C = 1. Compute the new values of A and Cafter the execution of the instruction asla.Solution:

Example 2.19 Suppose that m[$800] = $ED and C = 0. Compute the new values of m[$800] and the C flag after the execution of the instruction asr $1000.Solution:

01 1 110 0 00

1 1 1 10 0 0 0 0

Figure 2.11a Operation of the ASLA instruction

C flag

accumulator A

Original value New value

[A] = 10010101C = 1

[A] = 00101010C = 1

Figure 2.11b Execution result of the ASLA instruction

1 0 111 1 01

11 0 11 1 1 1 0

Figure 2.12a Operation of the ASR $1000 instruction

memory location$1000

C flagOriginal value New value

[$1000] = 11101101C = 0

[$1000] = 11110110C = 1

Figure 2.12b Result of the asr $1000 instruction

Page 47: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.20 Suppose that m[$1000] = $E7 and C = 1. Compute the new contents of m[$1000] and the C flag after the execution of the instruction lsr $1000.Solution:

Example 2.21 Suppose that [B] = $BD and C = 1. Compute the new values of B and the C flag after the execution of the instruction rolb.Solution:

Page 48: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.22 Suppose that [A] = $BE and C = 1. Compute the new values of mem[$00] after the execution of the instruction rora.Solution:

Page 49: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.23 Write a program to count the number of 0s in the 16-bit number storedat $1000-$1001 and save the result in $1005.Solution:* The 16-bit number is shifted to the right 16 time.* If the bit shifted out is a 0 then increment the 0s count by 1.

org $1000db $23,$55 ; test dataorg $1005

zero_cnt rmb 1lp_cnt rmb 1

org $1500clr zero_cnt ; initialize the 0s count to 0ldaa #16staa lp_cntldd $1000 ; place the number in D

loop lsrd ; shift the lsb of D to the C flagbcs chkend ; is the C flag a 0?inc zero_cnt ; increment 1s count if the lsb is a 1

chkend dec lp_cnt ; check to see if D is already 0bne loop

forever bra foreverend

Page 50: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 51: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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 left 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 52: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.24 Write a program to shift the 32-bit number stored at $820-$823 to the right four places.Solution:

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

again lsr 0,Xror 1,Xror 2,Xror 3,Xdbne b,againend

Page 53: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Boolean Logic Instructions

Changing a few bits are often done in I/O applications.

Boolean logic operation can be used to change a few I/O port pins easily.

Page 54: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Program Execution Time

The HCS12 uses the E clock as a timing reference. The frequency of the E clock is half of that of the crystal oscillator.

There are many applications that require the generation of time delays.

The creation of a time delay involves two steps:

1. Select a sequence of instructions that takes a certain amount of time to execute.2. Repeat the selected instruction sequence for an appropriate number of times.

For example, the instruction sequence on the next page takes 40 E cycles to execute. By repeating this instruction sequence certain number of times, any time delay can be created.

Assume that the HCS12 runs under a crystal oscillator with a frequency of 16 MHz, then the E frequency is 8 MHz and hence its clock period is 125 ns. Therefore the instruction sequence on the next page will take 5 ms to execute.

Page 55: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

loop psha ; 2 E cyclespula ; 3 E cyclespshapulapshapulapshapulapshapulapshapulapshapulanop ; 1 E cyclenop ; 1 E cycledbne x,loop ; 3 E cycles

Page 56: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

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

loop psha ; 2 E cyclespula ; 3 E cyclespshapulapshapulapshapulapshapulapshapulapshapulanop ; 1 E cyclenop ; 1 E cycledbne x,loop ; 3 E cycles

Page 57: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

From the CPU12 Reference Manual:

PSHA takes 2 E-cycles to execute

Page 58: EE383: Introduction to Embedded Systems University of ...courses.engr.uky.edu/ideawiki/lib/exe/fetch.php?... · EE383: Introduction to Embedded Systems University of Kentucky ...

Example 2.26 Write an instruction sequence to create a delay of 10 seconds.

Solution: By repeating the previous instruction sequence 100 times, we can create a delay of 10 seconds.

ldab #100out_loop ldx #20000in_loop psha ; 2 E cycles

pula ; 3 E cyclespshapulapshapulapshapulapshapulapshapulapshapulanop ; 1 E cyclenop ; 1 E cycledbne x,in_loop ; 3 E cyclesdbne b,out_loop ; 3 E cycles


Related Documents