Anda di halaman 1dari 22

BRANCH INSTRUCTIONS

The major use of the condition codes is with


CONDITIONAL BRANCH instructions.
As explained earlier, a BRANCH instruction directs
the PC to another address.
Instruction
Branch
Branch
Branch
Branch
Branch
Branch
Branch
Branch
Branch

Mnemonic

Op Code

Always
BRA
if carry SET
BCS
if carry CLEAR
BCC
if zero
BEQ
if not zero
BNE
if minus
BMI
if plus
BPL
if overflow SET
BVS
if overflow CLEAR BVC

CC

20
25
24
27
26
2B
2A
29
28

C=
C=
Z=1
Z=
N=1
N=0
V=
V=0

1
0
0
1

BRANCH INSTRUCTIONS
The instruction BRA, is an unconditional branch. The
program always jumps or branches when the BRA
(Branch Always) instruction occurs.
The remaining instructions are conditional branches;
they take the branch only if the condition codes are
set properly.
Each branch instruction is accompanied by an
offset, a number that is added to the current value
of the Program Counter to determine where the P
will go if it branches.
Branch instructions skip over a part of the program
to a location that is relative to the program counter.

Example 11
At a point in a program the H, I, and C bits of the CCR
should be SET and the
N, V, and Z bits should be CLEAR. Write a sequence of
instructions to set the
bits accordingly.
SOLUTION
The CCR should look like this
11110001
lt can be forced into this configuration by the following
instructions:
Code
Mnemonic
Comments
86 F1
LDAA #$F1
Load desired contents of
CCR into A
06
TAP
Transfer A to CCR

Calculating Offsets
A BRANCH instruction for the 6800 consists of the
BRANCH op code in one byte and an 8-bit offset in the
following byte.
The offset is a 2s-complement number that is added to
the address.
If N is the location of the branch instruction and T is the
target address (the address the program will jump to),
the following formula can be used to calculate the offset:
Offset: T (N + 2)
Note that both forward and backward branches can be
accommodated.
Backward branches have negative offsets.

Example 12
The op code of the branch instruction is in $011A.
What should the offset be if the program must jump
to $0150?
SOLUTION
From the equation we obtain
$011A + 2 = $011C
Offset = $0150 - $011C = $34
Thus the program segment would be as follows:
011A XX
(branch op code)
011B 34 (offset)

Example 13
The op code of the branch instruction is in $011A.
What should the offset be if the program must jump
to $00EA?
SOLUTION
From the formula
$011A + 2 = $011C
Offset = $00EA - $011C = $FFCE
For an 8-bit negative offset the $FF is discarded and
the program is as follows:
011A XX
(branch op code)
011B CE
(offset)
Note that the offset is a negative number ($CE) and
the program branches backward from $11C to $EA.

Long and Short Branches


The maximum positive number that can be
represented in one byte is 7F16 (or 12710), and the
most negative number is 8016 (or -12810).
Thus the program can branch forward no more
than 127 bytes or backward no more than 128
bytes from where it would be if it did not branch
(i.e., the start of the next instruction).
This range is often sufficient because the
destination of most branches is usually nearby.

BCD ADDITION AND THE H BIT


Most people prefer to communicate with their
computers using ordinary decimal numbers.
In applications that deal with money such as cash
registers, it is preferable to keep the numbers in
decimal form rather than converting them to
binary or hex.
The use of the H bit of the CCR and the DAA
(Decimal Adjust Accumulator) instruction makes
decimal arithmetic possible.

Expressing Numbers in BCD


Decimal numbers are usually entered into a
computer in BCD (Binary-Coded Decimal) form.
The BCD code uses 4 binary bits called a decade
to represent a single decimal digit (0 to 9).
Because numbers greater than 9 are not used,
the numbers from 10 to 15, which are also
possible with 4-bit representation should never
appear in a BCD output.

Expressing Numbers in BCD

Example 13
Express the decimal number 630910 in BCD form.
SOLUTION
From the code conversion table, we find that
6 = 0110
3 = 0011
0 = 0000
9 = 1001
The number 6309 is expressed by stringing these
bits together:
(630910):0110 0011 0000 1001

Adding BCD Numbers


Because each 6800 memory location contains 8
bits and each BCD decade contains 4 bits, it is
natural to store two BCD digits in a single memory
location.
This is sometimes called packing or packed BCD.
Addition and subtraction of BCD numbers is
possible, but because all addition and subtraction
instructions in the 6800 assume binary numbers,
the binary results must be manipulated to convert
them to BCD.
This is done by using the H bit and the DAA
instruction.

Adding BCD Numbers


The H bit is changed only by addition instructions.
It is SET when the addition produces a carry out
of bit position 3 and into bit position 4.
For BCD numbers the H bit is SET whenever the
sum of the two digits, plus carry, is equal to or
greater than (16)10

Example 14
Accumulators A and B contain the decimal numbers
48 and 79, respectively. They are added by an ADD
accumulator (ABA) instruction. What is the result,
and what are the conditions of the C and H bits after
the addition?
SOLUTION
The 6800 adds 48 and 79 as though they were hex
digits, placing the sum, C1, in accumulator A. At the
end of the addition the H bit is SET (because the
sum of 8 and 9 produces a carry), but the carry bit
is CLEAR because the sum of the two most
significant digits is less than 16.

DAA Instruction
The result of Example 14 (48 + 79 = C1) is
unsatisfactory if decimal arithmetic is being used.
This instruction must be followed by a Decimal
Adjust Accumulator (DAA) instruction to convert
the hex result to the correct BCD result.
lt examines four parts of the result:

1.
2.
3.
4.

The
The
The
The

lower half-byte
upper halfbyte
H bit
C bit

Example 15
What happens if a DAA instruction is added to
Example 14?
SOLUTION
ln Example 14, the sum was C1. The DAA notes the
following:
1. The lower half-byte is 0-3.
2. The H bit is SET.
3. The upper half-byte is A-F.
4. The C bit is CLEAR.
The DAA adds 66 to the result and SETs the C bit.
After the DAA, A contains C1 + 66 = 127 and the
carry bit is SET, which indicates a carry (a weight of
100 in decimal arithmetic). Therefore, the BCD sum
is 127, which is correct.

Example 16
The decimal numbers 2948 and 4957 are in locations
$20, $21 and $22, $23, respectively. Write a program to
add them and store the BCD result in locations $24 and
$25.
ADDR OBJECT
MNEMONICS
COMMENTS
C000
96 21
LDA $21 Lead LS byte of Augend
C002
9B 23 ADDA $23 Add LS byte of Addend
C004
19
DAA
Adjust for half carry, if any
C005
97 25 STAA $25
Store results in 25
C007
96 20 LDAA $20 Load MS byte of Augend in
A
C009
99 22 ADCA $22
Add W-C MS byte of
Addend
COOB
19
DAA
Adjust result
COOC
97 24 STAA $24
Store MS byte

Subtracting BCD Numbers


BCD subtraction in the 6800 P can be
accomplished by complementation.
Subtraction by complementation works well for
decimal numbers.
ln
subtraction
by
complementation,
the
subtrahend must be replaced by its 9s
complement, which is obtained by taking each
decimal digit and replacing it with the difference
between itself and 9.

Subtracting BCD Numbers


Decimal subtraction can be performed by using
the following procedure:
1. Take the 9s complement of the subtrahend and
add it to the minuend.
2. Remove the most significant 1 and add it to the
least significant digit. This is known as an endaround-carry.

Example 17
Subtract 19,307 from 28,652.
SOLUTION
The 9s complement of 19,307 is 80,692. Adding this to
the minuend, we obtain
(minuend) 28,652 (original number)
(subtrahend)
80,692 (9s complement)
109,344
Removing the most significant t and adding it to the
least significant digit yields

This is the correct result.

Subtracting BCD Numbers


In the 6800 the H bit is not set by subtraction
instructions.
The 6800 can be programmed for BCD
subtraction as follows:
1. Load accumulator A with 99.
2. Subtract the subtrahend (to obtain the 9s
complement).
3. Add 1 (to make the 10s complement).
4. Add the minuend (BCD addition).
5. Decimal adjust the accumulator.

Example 18
Subtract 35 from 82 using the method just
described. Assume that B2 is in $20 and 35 is in
$22.
SOLUTION
The correct answer, 47, appears in A at the end of
the program. Some adjustments are required in the
program if multiple-byte subtraction or the
possibility of negative results are to be allowed.
ADDR
OBJECT MNEMOMCS
COMMENTS
C000 86 99
LDAA #$99 Make 9s Complement
C002 90 22
SUBA $22
Get Subtrahend
C004 4C
INCA
Make 10s Complement
C005 9B 20
ADDA $20 Add other number
C007 19
DAA
Decimal Adjust

Anda mungkin juga menyukai