0% found this document useful (0 votes)
21 views12 pages

Unpacked BCD

The document discusses the representation of numbers in ASCII and binary forms, highlighting the need for conversion between these two formats for processing. It details how ASCII and BCD representations work, along with specific instructions for processing ASCII numbers on the Pentium architecture, including addition, subtraction, multiplication, and division. The document provides examples to illustrate how these operations are performed and the adjustments needed for accurate results.

Uploaded by

Taveed Ghazarian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Unpacked BCD

The document discusses the representation of numbers in ASCII and binary forms, highlighting the need for conversion between these two formats for processing. It details how ASCII and BCD representations work, along with specific instructions for processing ASCII numbers on the Pentium architecture, including addition, subtraction, multiplication, and division. The document provides examples to illustrate how these operations are performed and the adjustments needed for accurate results.

Uploaded by

Taveed Ghazarian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Representation of Numbers

• Numbers are in ASCII form


when received from keyboard
when sending to the display
• Binary form is efficient to process numbers
internally
Representation of Numbers (cont’d)
• Requires conversion between these two number
representations
In some applications, processing of numbers is
simple (e.g. a simple addition)
• » Does not justify the input and output conversion overheads
» In this case, it is better to process numbers in the decimal form

• Decimal numbers can be represented in


» ASCII
» BCD
Representation of Numbers (cont’d)
• ASCII representation
Numbers are stored as a string of ASCII characters
» Example: 1234 is stored as 31 32 33 34H
ASCII for 1 is 31H, for 2 is 32H, etc.
• BCD representation
Unpacked BCD
» Example: 1234 is stored as O1 O2 O3 O4H
– Additional byte is used for sign
Sign byte: OOH for + and 8OH for
Packed BCD
» Saves space by packing two digits into a byte
– Example: 1234 is stored as 12 34H
Processing ASCII Numbers
• Pentium provides four instructions
aaa ASCII adjust after addition
aas ASCII adjust after subtraction
aam ASCII adjust after multiplication
aad ASCII adjust before division
These instructions do not take any operands
» Operand is assumed to be in AL
Processing ASCII Numbers (cont’d)
ASCII addition
Example 1 Example 2
34H = 00110100B 36H = 00110110B
35H = 00110101B 37H = 00110111B
69H = 01101001B 6DH = 01101101B
Should be O9H Should be 13H
Ignore 6 Ignore 6 and add 9 to D
• The aaa instruction performs these adjustments to
the byte in AL register
Processing ASCII Numbers (cont’d)
• The aaa instruction works as follows:
If the least significant four bits in AL are > 9 or
if AF =1, it adds 6 to AL and 1 to AH.
– Both CF and AF are set
In all cases, the most significant four bits in AL are
cleared
Example:
sub AH,AH ; clear AH
mov AL,'6' ; AL := 36H
add AL,'7' ; AL := 36H+37H = 6DH
aaa ; AX := 0103H
or AL,30H ; AL := 33H
Processing ASCII Numbers (cont’d)
ASCII subtraction
• The aas instruction works as follows:
If the least significant four bits in AL are > 9 or
if AF =1, it subtracts 6 from AL and 1 from AH.
– Both CF and AF are set
In all cases, the most significant four bits in AL are
cleared
• This adjustment is needed only if the result is
negative
Processing ASCII Numbers (cont’d)
• Example 1: Positive result
sub AH,AH ; clear AH
mov AL,'9' ; AL := 39H
sub AL,'3' ; AL := 39H-33H = 6H
aas ; AX := 0006H
or AL,30H ; AL := 36H

• Example 2: Negative result


sub AH,AH ; clear AH
mov AL,'3' ; AL := 33H
sub AL,'9' ; AL := 33H-39H = FAH
aas ; AX := FF04H
or AL,30H ; AL := 34H
Processing ASCII Numbers (cont’d)
ASCII multiplication
• The aam instruction adjusts the result of a mul
instruction
Multiplication should not be performed on ASCII
» Can be done on unpacked BCD
• The aam instruction works as follows
AL is divided by 1O
Quotient is stored in AH
Remainder in AL
• aam does not work with imul instruction
Processing ASCII Numbers (cont’d)
• Example 1
mov AL,3 ; multiplier in unpacked BCD form
mov BL,9 ; multiplicand in unpacked BCD form
mul BL ; result 001BH is in AX
aam ; AX := 0207H
or AX,3030H ; AX := 3237H

• Example 2
mov AL,'3' ; multiplier in ASCII
mov BL,'9' ; multiplicand in ASCII
and AL,0FH ; multiplier in unpacked BCD form
and BL,0FH ; multiplicand in unpacked BCD form
mul BL ; result 001BH is in AX
aam ; AX := 0207H
or AL,30H ; AL := 37H
Processing ASCII Numbers (cont’d)
ASCII division
• The aad instruction adjusts the numerator in AX
before dividing two unpacked decimal numbers
The denominator is a single unpacked byte
• The aad instruction works as follows
Multiplies AH by 1O and adds it to AL and sets AH to O
Example:
» If AX is O2O7H before aad
» AX is changed to OO1BH after aad

• aad instruction reverses the changes done by aam


Processing ASCII Numbers (cont’d)
• Example: Divide 27 by 5
mov AX,0207H ; dividend in unpacked BCD form
mov BL,05H ; divisor in unpacked BCD form
aad ; AX := 001BH
div BL ; AX := 0205H

• aad converts the unpacked BCD number in AX to


binary form so that div can be used

You might also like