ASCII and BCD Arithmetic: S. Dandamudi
ASCII and BCD Arithmetic: S. Dandamudi
Chapter 11 S. Dandamudi
Outline
Representation of Numbers
ASCII representation BCD representation
Unpacked BCD Packed BCD
1998
BCD: Page 2
Representation of Numbers
Numbers are in ASCII form
when received from keyboard when sending to the display
Process in binary
S. Dandamudi
1998
BCD: Page 3
BCD representation
Unpacked BCD
Example: 1234 is stored as 01 02 03 04H Additional byte is used for sign Sign byte: 00H for + and 80H for
Packed BCD
Saves space by packing two digits into a byte Example: 1234 is stored as 12 34H
1998 S. Dandamudi BCD: Page 5
To be used with S. Dandamudi, Introduction to Assembly Language Programming, Springer-Verlag, 1998.
1998
S. Dandamudi
BCD: Page 6
Example 2
36H = 00110110B 37H = 00110111B 6DH = 01101101B Should be 13H Ignore 6 and add 9 to D
In all cases, the most significant four bits in AL are cleared Example:
sub mov add aaa or
1998
; ; ; ; ;
clear AL := AL := AX := AL :=
S. Dandamudi
1998
BCD: Page 9
; ; ; ; ;
clear AL := AL := AX := AL :=
S. Dandamudi
Example 2
mov mov and and mul aam or
1998
; ; ; ; ; ; ;
multiplier in ASCII multiplicand in ASCII multiplier in unpacked BCD form multiplicand in unpacked BCD form result 001BH is in AX AX := 0207H AL := 37H
S. Dandamudi BCD: Page 12
aad converts the unpacked BCD number in AX to binary form so that div can be used Example: Multidigit ASCII addition
ASCIIADD.ASM Adds two 10-digit numbers
Adds one digit at a time starting with the rightmost digit
1998 S. Dandamudi BCD: Page 14
To be used with S. Dandamudi, Introduction to Assembly Language Programming, Springer-Verlag, 1998.
Example 2
27H = 00100111B 34H = 00110100B 5BH = 01011101B Should be 61H (add 6)
Example 3
52H = 01010010B 61H = 01100001B B3H = 10110010B
1998
S. Dandamudi
Example:
mov add daa AL,71H AL,43H ; AL := B4H ; AL := 14H and CF := 1
The result including the carry (i.e., 114H) is the correct answer
1998 S. Dandamudi BCD: Page 17
To be used with S. Dandamudi, Introduction to Assembly Language Programming, Springer-Verlag, 1998.
Example:
mov sub das
1998
AL,71H AL,43H
; AL := 2EH ; AL := 28H
S. Dandamudi BCD: Page 18
For storage of the two input numbers and the result, we can use DT (Define Ten-byte) directive
DT stores in packed BCD form Example:
DT 1234567890
is stored as
90 78 56 34 12H
1998 S. Dandamudi BCD: Page 19
1998
S. Dandamudi
BCD: Page 20
10
1.5
1.0
d
0.5
ASCII ad
150
200
dd II a SC A
4.0 3.0 2.0 1.0 0.0 0 50 100 Number of calls (in thousands) 150 200
1998
S. Dandamudi
BCD: Page 22
11