Sie sind auf Seite 1von 9

Lab 6 BCD Multiplication and Division

6
BCD Multiplication and Division

6.1 Objectives:

In this lab, we will look at the 68HC12 instructions to do the rest of the arithmetic
functions, like multiplication, division, and BCD addition and subtraction. When you
complete this lab you will be able to:

• Write programs to do BCD addition and subtraction.

• Write a program to do BCD-to-binary and binary-to-BCD conversion.

• Use the 68HC12 to do multiplication and division.

6.2 Related material to read:

• Writing Programs to do Arithmetic: Section 2.5 in the text, pages 37 – 50.

• Instruction Reference: Appendix A in the text, pages 635 – 653.

6.3 BCD numbers:

Humans do arithmetic calculations with decimal numbers because our number system is
base ten. Because microprocessors use a binary number system, special techniques are
needed to add or subtract decimal numbers.
Each of the decimal digits can be coded in binary using four bits. This
representation is called binary coded decimal or BCD. The BCD code is shown in Figure
6.1. Thus the decimal numbers 26 and 27 would be stored as 0010 0110 and 0010 0111 in
BCD. Many data input or output devices represent numbers as decimal digits and encode
them as BCD. For this reason, it is useful to do addition and subtraction in BCD, but
special techniques are required.

-1-
Lab 6 BCD Multiplication and Division

Decimal Number Binary Coded Decimal (BCD)


0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

Figure 6.1: Binary Coded Decimal equivalent of the decimal number.

6.3.1 BCD Addition:

The addition of two decimal numbers 26 and 27 gives the decimal number 53, but if the
microprocessor added these two numbers using their binary representation, instead of
getting the true answer, the result would be:

0010 0110
+ 0010 0111
0100 1101 = 4D hex = 77 decimal

You can correct this by using the decimal adjust accumulator or DAA instruction. After a
two-digit BCD addition is done, the DAA instruction looks at the result in accumulator
A, the carry bit, and the H or half carry bit of the condition code register. The H bit is set
if the addition caused a carry from the least significant half-byte to the most significant
half-byte, in other words, from bit-3 to bit-4 in binary. If the least significant half-byte in
the accumulator is greater than nine, or if the H bit is set, six is added to the result. Next,
six is added to the most significant half-byte if it is greater than nine, or if the C flag is
set. In this example, because the least significant half-byte is greater than nine, 6 is
added to 4D, giving the correct result in BCD of 53.

0100 1101
+ 0000 0110
0101 0011 = 53 decimal (correct answer)

The flow chart in Figure 6.2 shows how to implement BCD multi-byte addition.

-2-
Lab 6 BCD Multiplication and Division

Start

Get LS
bytes

Add them

Decimal
Accumulator
Adjust

Get MS
bytes

Add them

Decimal
Accumulator
Adjust

End

Figure 6.2: Two-byte BCD addition.

6.3.2 BCD Subtraction:

The DAA instruction does not correct the result of BCD subtraction. When doing BCD
subtraction, the ten's complement of the subtrahend (the number being subtracted) must
be added to the minuend (the number from which the subtrahend is subtracted from). The

-3-
Lab 6 BCD Multiplication and Division

ten's complement is formed by subtracting the number from 99 for single-byte or 9999
for double-byte numbers. Note how this is done for the operation 8713 - 4395:

Subtract 4395 from 9999:


9999
– 4395
5604
Now add 1 to form the 10’s complement:
5604
+ 1
5605
Finally add the 10’s complement result to 8713:
8713
+ 5605
1 4318

This is the correct answer and the carry bit being 1 indicates the result is a
positive number. If the carry bit were 0, this would be a negative number. The flow chart
in Figure 6.3 shows how to implement two-byte BCD subtraction.

6.3.3 Multiplication:

The MUL instruction multiplies the binary, unsigned number in accumulator A times the
binary, unsigned number in accumulator B and places the result in accumulator D.
Because A and B are both 8 bits and D is 16 bits, there can be no overflow. The largest
product you can get is $FF x $FF = $FE01.

6.3.4 Division:

The IDIV instruction performs an unsigned integer division of two 16-bit numbers. The
dividend should be stored in accumulator D and the divisor should be stored in the X
register before the instruction is issued. After the instruction is executed, the quotient and
remainder are stored in the X register and the D accumulator, respectively. IDIV expects
the quotient to be 1 or greater. The FDIV instruction works in the same way as IDIV, but
it expects the quotient to be less than 1.

-4-
Lab 6 BCD Multiplication and Division

Start

Decimal
Subtract LS byte of Accumulator
subtrahend from 99 Adjust

Subtract MS byte of Add LS byte of


subtrahend from 99 10’s complement
to LS byte of
minuend
Add 1 to LS byte
of subtrahend for
10’s complement Decimal
Accumulator
Adjust

Decimal
Accumulator
Adjust Add MS byte of
10’s complement
with carry to MS
byte of minuend
Add 00 with carry
to MS byte of
subtrahend to
propagate carry Decimal
Accumulator
Adjust

End

Figure 6.3: Two-byte BCD subtraction.

-5-
Lab 6 BCD Multiplication and Division

6.4 Conversion between BCD and binary numbers:

There are no 68HC12 instructions for multiplying or dividing BCD numbers. The BCD
numbers must first be converted to binary; then they can be multiplied or divided.

6.4.1 BCD to binary conversion:

To do BCD to binary conversion, start with the most significant BCD digit, multiply it by
$0A and then add the next most significant digit. This can be considered as the partial
conversion number. Now multiply this partial conversion number by $0A and add the
next most significant digit. This procedure is repeated until the digit to be added is the
least significant digit. A flow chart for this algorithm is shown in Figure 6.4.

6.4.2 Binary to BCD conversion:

To do binary to BCD conversion, the IDIV instruction can be used to do repeated


division by $0A. The binary number to be converted is divided by $0A and the remainder
becomes the least significant digit. The quotient of this division is then divided by $0A
and this remainder becomes the next BCD digit. This is continued until the quotient is
less than $0A. When the quotient is less than $0A, it becomes the most significant BCD
digit. A flow chart for this algorithm is included in Figure 6.5. The BCD digits need to be
packed two digits per byte. This can be done by saving them in separate memory
locations and then shifting every even digit four positions to the left. Now if you OR
every pair of even and odd digits, they will be combined into one byte.

6.5 More utility subroutines:

We have already introduced 3 monitor utility subroutines in the previous labs, viz.,
INCHAR, OUTSTRG and OUT1BSP. In this lab, we introduce 1 more utility subroutine,
i.e., OUT2BSP. This subroutine combines the 2 hex numbers starting at address in the
index register X and outputs the resultant 4 digit hex number on the screen followed by
space. It returns the address of the next byte in the index register X. Following code
shows a sample program that uses this utility subroutine.
1 out2bsp equ $ff58
2 addr equ $0800
3 org $0A00
4 start lds #$0DFF
5 ldd #$1278
6 std addr
7 ldx #addr
8 jsr out2bsp
9 swi
10 end

The equ directive in line 1 just makes the hard to remember address of the subroutine,
$FF58, equal to an easy to remember name out2bsp. Line 4 initializes the stack pointer.
In line 5 and 6, $1278 is stored in two memory address locations $0800 and $0801. In
line 7, the index register X points to the address $0800. Line 8 calls the subroutine
out2bsp that outputs 1278 (contents of $0800 and $0801) on the screen.

-6-
Lab 6 BCD Multiplication and Division

Start

Current Digit = Current MSB


Partial Convert = 0

Add Current Digit to


Partial Convert

Was that
the Last Yes End
Digit?

No

Multiply Partial
Convert by $0A

Current Digit = Next Digit

Figure 6.4: BCD to binary conversion.

-7-
Lab 6 BCD Multiplication and Division

Start

Result = Binary number


Memory = 0

Is Save Result in
Yes
Result
< 10?
the Memory

No Pack BCD digits in


the Memory
Divide Result
by $0A

End

Result = Quotient

Save Remainder in the


Memory

Memory = Memory + 1

Figure 6.5: Binary to BCD conversion.

-8-
Lab 6 BCD Multiplication and Division

6.6 Procedure:

Before you come to the lab, individually draw flowcharts and write programs for the
following procedures.

1. Write a program to add two four-digit BCD numbers together. To test your program,
load the two numbers into memory manually (using ‘mm’ command) and have the
program read them. Display them to the terminal screen, save the result in memory,
and display that result to the screen. Show the working program to TA.
2. Write a program to perform BCD subtraction on two four-digit BCD numbers.
Follow the same procedure as above.
3. Write a program to multiply two unsigned two-digit BCD numbers that are stored in
memory. Read the BCD numbers from memory, convert them to binary, perform the
multiplication, and then convert the result back to BCD. Display the numbers and the
product to the terminal screen. Show the working program to TA. (NOTE: You will
use these conversion subroutines again in the future labs. Hence keep them handy.)

6.7 Questions:

1. Draw the flowchart for a subroutine that would do multiplication of two signed 8-bit
numbers represented as two's complement numbers. Make the flowchart detailed
enough so a program could easily be written from it.
2. Draw a flowchart for a program that multiplies two 16-bit numbers. Assume that
EMUL instruction is not available and the microprocessor can multiply only two 8-bit
numbers at a time.

6.8 Lab Report:

For the lab write up, include


1. Your individual flowcharts and programs that you wrote before the lab.
2. A copy of your working .asm files (DO NOT include .lst files).
3. A brief discussion of the objectives of the lab and the procedures performed in the
lab.
4. Answers to any questions in the discussion, procedure, or question sections of the lab.

-9-