Download Chapter 2 HCS12 Assembly Language and more Study notes Logic in PDF only on Docsity!
Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/
[email protected]
Chapter 2
HCS12 Assembly Language
ECE 3120
Outline
2.
Assembly language program structure
Arithmetic instructions
Branch and loop instructions
Shift and rotate instructions
Boolean logic instructions
2.6 Bit test and manipulate instructions
1. end- Ends a program to be processed by an assembler- Any statement following the end directive is ignored2. Org (origin)- Tells the assembler where to place the next instruction/data in
memory- Example:
org
ldab
#$FF
;this instruction will be stored in
memory starting from location $1000.
3. dc.b (define constant byte), db (define byte), fcb (form
constant byte)- Define the value of a byte or bytes that will be placed at a givenlocation.- Example:
org
array dc.b
4. 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 agiven location.- For example:
org
array
dc.w
$AC11,$F122,$33,$F
$11 $22 $33$0F$
(^800801802803)
$AC^ $F1 $
(^804805806807)
5. fcc (form constant character)
- Tells the assembler to store a string of characters (a message) in
memory.
- 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.
8. ds.w (define storage word), rmw (reserve memory word)
- Reserve a number of words Dbuf ds.w 20 ;Reserves 20 words (or 40 bytes) starting
from the current location counter.
9. equ (equate)
-^
Assigns a value to a label.
-^
Makes programs more readable.
-^
Examples:
loop_count
equ 50
Informs the assembler whenever the symbol loop_count isencountered, it should be replaced with the value 50
Example 3: Two dimensional arrays
This computation isdone by assembler
Later, we will write a code to read oneand two dimensional arrays
A line of an assembly program
Label field- Labels are symbols defined by the user to identify memory
locations in the programs and data areas- Optional- Must starts with a letter (A-Z or a-z) and can be followed byletters, digits, or special symbols (_ or .)
- Optional- Explain the function of a single or a group of instructions- For programmer – not for assembler or processor.- Ignored by assembler and are not converted to machine code.-^
Can improve a program readability -
very important in assembly
-^
Any line starts with an
or
;^
is a comment
-^
Separated from the operand and operation field for at least one space Comment field
Instructions - Instruct the processor to do a sequence of operations- Converted to machine code- Operands follow the opcode and is separated from the opcode by atleast one space- Operands are separated by commas- Opcode is the operation and separated from the label by at leastone space- Assembler instructions or directives are not case sensitive- Must not start at column 1
Instructions
Outline
Assembly language program structure
2.
Arithmetic instructions
Branch and loop instructions
Shift and rotate instructions
Boolean logic instructions
2.6 Bit test and manipulate instructions
- Zero flag (Z): set when the result is zero- Negative flag (N): set whenever the result is negative, i.e., most
significant bit of the result is 1.
- Half carry flag (H): set when there is a carry from the lower four
bits to the upper four bits.
- Carry/borrow flag (C): set when addition/subtraction generates a
carry/borrow.
- Overflow flag (V): Set when the addition of two positive numbers
results in a negative number or the addition of twonegative numbers results in a positive number. i.e.whenever the carry from the most significant bitand the second most significant bit differs
+^
C = 0,
V = 0,
Z = 0,
N = 1
Overflow
Overflow Detection 1- Unsigned numbers: Overflow occurs when C = 1, C flag can be
considered as a bit of the result.
Problem 2- Signed numbers: Overflow occurs when V = 1Overflow cannot occur when adding numbers of opposite sign why?
: fixed width registers have limited range
Overflow occurs when two numbers are added or subtracted andthe
correct
result
is
a
number
outside
the
range
that
can
a
register hold
If there is an overflow, then the given result is not correct
+ 0000 00010000 0000C =1, V =
Signed numbers:
-1 +1 = 0 , no overflow and the result is
correct
Unsigned numbers:
255 +1 = 256, overflow,
256 needs
9 bits instead of 8,
the result is
incorrect
2 - 16
Addition
C = 1, the result needs more space than the register widthV = 1, (+ve) + (+ve) = (-ve) or (–ve) + (-ve) = (+ve) Subtraction
: A - B
There is no unsigned overflow but there is signed overflowC = 1, when there is a borrow or B > AV =1, when(-ve) - (+ve) = (+ve) this is equivalent to (–ve) + (-ve) = (+ve)(+ve) - (-ve) = (-ve)
this is equivalent to (+ve) + (+ve) = (-ve)
+^
+^
Unsigned^1128
+^
Signed^1 -
C = 0V = 1
-^
-^
Unsigned
-^
Signed- 37- 109
C = 1 (called borrow),V = 1