Click here to load reader
Mar 11, 2020
Chapter 2: HCS12 Assembly Programming
EE383: Introduction to Embedded Systems University of Kentucky
Samir Rawashdeh
With slides based on material by H. Huang – Delmar Cengage Learning
1
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
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
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
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 $1000 ldab #$FF
places the opcode byte for the instruction ldab #$FF at location $1000.
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 $800 array 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 $1000 vec_tab dc.w $1234, $5620
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:”
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
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
Macro
- A name assigned to a group of instructions - Use macro and endm to define a macro. - Example of macro
sumOf3 macro arg1,arg2,arg3 ldaa arg1 adda arg2 adda arg3 endm
- Invoke a defined macro: write down the name and the arguments of the macro
sumOf3 $1000,$1001,$1002
is replaced by ldaa $1000 adda $1001 adda $1002
Software Development Process 1 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.
Symbols of Flowchart
Terminal
Process
Input or output
Decision yes
no
Subroutine
A
B
A
on-page connector
off-page connector
Figure 2.1 Flowchart symbols used in this book
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 2 A A + m[$1001] Step 3 A A + m[$1002] Step 4 $1100 A
org $1500 ldaa $1000 adda $1001 adda $1002 staa $1100 end
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
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
Example 2.7 Write a program to add two 4-byte numbers that are stored at $1000-$1003 and $1004-$1007, and store the sum at $1010-$1013. Solution: Addition starts from the LSB and proceeds toward MSB.
org $1500 ldd $1002 ; add and save the least significant two bytes addd $1006 ; “ std $1012 ; “
ldaa $1001 ; add and save the second most significant bytes adca $1005 ; “ staa $1011 ; “
ldaa $1000 ; add and save the most significant bytes adca $1004 ; “ staa $1010 ; “ end
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 $1500 ldd $1002 ; subtract and save the least significant two bytes subd $1006 ; “ std $1102 ; “
ldaa $1001 ; subtract and save the difference of the second to most sbca $1005 ; significant bytes staa $1001 ; “
ldaa $1000 ; subtract and save the difference of the most significant sbca $1004 ; bytes staa $1100 ; “ end
Multiplication and Division
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 $1000 ldy $1002 emul sty $1100 std $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 $1005 ldx $1020 idivs stx $1100 ; store the quotient std $1102 ; store the remainder
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
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 $1000 adda $1001 daa staa $1002
adds the BCD numbers stored at $1000 and $1001 and saves the sum at $1002.
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 tested org $1010