Hardware Reference
In-Depth Information
Now we need to subtract $7 from $3. To do this, we need to borrow from somewhere. The
HCS12 borrows from the C fl ag, thus setting the C fl ag. When we borrow from the next higher
digit of a hex number, the borrow has a value of decimal 16. After the borrow from the C fl ag,
the problem can be completed.
$3 9
$ 7 4
$ C 5
When the HCS12 executes a subtract instruction, it always borrows from the C fl ag. The bor-
row is either 1 or 0. The C fl ag operates as follows during a subtraction:
If the HCS12 borrows a 1 from the C flag during a subtraction, the C flag is set to 1.
If the HCS12 borrows a 0 from the C flag during a subtraction, the C flag is set to 0.
2.5.4 Multiprecision Subtraction
For a 16-bit microcontroller, multiprecision subtraction is the subtraction of numbers that
are larger than 16 bits. To subtract the hex number $16753284 from $98765432, the HCS12 has
to perform a multiprecision subtraction.
$
$
9
1
8
6
7
7
6
5
5
7
4
2
3
8
2
4
Like multiprecision addition, multiprecision subtraction is performed 1 byte at a time, begin-
ning with the least signifi cant byte. The HCS12 does allow us to subtract 2 bytes at a time
because it has the subd instruction. The following two instructions can be used to subtract the
least signifi cant 2 bytes of the subtrahend from the minuend:
ldd #$5432
subd #$7284
Since a larger number is subtracted from a smaller one, there is a need to borrow from the
higher byte, causing the C fl ag to be set to 1. The contents of double accumulator D should be
saved before the higher bytes are subtracted. Let's save these 2 bytes at $1002,$1003.
std $1002
When the second most signifi cant bytes are subtracted, the borrow 1 has to be subtracted from
the second most signifi cant byte of the result. In other words, we need a “subtract with borrow”
instruction. There is such an instruction, but it is called subtract with carry. There are two
versions: the sbca instruction for accumulator A and the sbcb instruction for accumulator B.
The instructions to subtract the second most signifi cant bytes are
ldaa #$76
sbca #$75
We also need to save the second most signifi cant byte of the result at $1001 with the following
instruction:
staa $1001
The most significant bytes can be subtracted using similar instructions, and the complete
program with comments is as follows:
org
$1500
; starting address of the program
ldd
#$5432
; D $5432
subd
#$7284
; D [D] 2 $7284
std
$1002
; $1002 , $1003 [D]
 
 
Search WWH ::




Custom Search