BCD and ASCII arithmetic

 BCD and ASCII arithmetic:-

In some case data to be operated on by the arithmetic instruction is BED (decimal) of ASCII form.

  BCD: - 

BCD encoding uses 4 bits to represents the decimal digits 0 to 9. The hex digits A to F are considered invalid.

 

a)      Packed BCD: A decimal number can also be packed with two digit per byte.

For example 65 D = 0110/6     0101/5                                    (D- decimal)

 

b)      Unpacked BCD: - A decimal number can be unpacked with two one digits pet byte.

For example 65D = 00000110/6      00000101/5                    (D- decimal)

 

1.      ASCII :-

Numerical data communing into a Computer from a terminal is usually in ASCII code. An ASCII number is similar to an unpacked decimal number in that one byte can hold only one digit. In this code number o to 9 are represented by the ASCII Code 30 to 39. In fact it is very easy to convent an ASCII digit to decimal by subtracting 30H. There are no special instructions for adding, subtracting, multiplying or dividing these encoded numbers. This can be lead to invalid result if care is not taken. The following instructions are used to convert invalid result to valid result.

 

 DAA: - Decimal adjust after addition

The DAA instruction Convert the binary (Hew) result of an ADD of ADC instruction in AL to packed BCD format. DAA only work on AL reg.

 

For Ex

MOV AL, 35 H

AL = 35H

MOV BL, 29 H

BL=29H

ADD AL, BL

AL=5EH

DAA

AL= 64D

 

2.      DAS (Decimal adjust after subtraction)

This instruction used after. Subtraction two packed BCD. Number to make sure the result is correct packed BCD. DAS only work on the AL reg.

 

For Example-

 

MOV AL, 56

AL=56 H

MOV BL, 27       

BL=27 H

SUB AL, BL

AL=2FH

DAS  

AL= 29 D

 

3.       AAA (ASCII Adjust for addition)

The 8086 allows you to add the ASCII codes for two decimal digits. The AAA instruction is then used to convert the results into the correct unpacked BCD.

For example:-

           

Mov AH, 00 H

AH=00 H

MOV AL,37 H

ASCII Code of ‘7’

MOV BL,33 H

ASCII Code of ‘3’

ADD AL,BL

AL = 6A H

AAA

Convert to ASCII

 

   AAS (ASCII adjust for subtraction)

The 8086 allows you to subtract the ASCII codes for two decimal digits. The AAS instruction is then used to convert the result into correct unpacked BCD.

Mov AH, 00 H

AH=00 H

MOV AL,39 H

ASCII Code of ‘9’

MOV BL,35 H

ASCII Code of ‘5’

ADD AL,BL

AL = 4 H

AAS

Convert to ASCII

 

 

5.      AAM (BCD adjust after multiply)

The AAM instruction follows the multiplication instruction after multiplying two one-digit unpacked BCD number.

Example shows the short program that multiply 5 by 5. The result after multiplying is 0019 H in the AX register.

In AX=0019 H after multiplication after AAM instruction adjust results and AX=0205. This is unpacked BCD result of 25.

 

6.      AAD (BCD to binary convert before division)

AAD instruction appear before a division. When unpack BCD is load in AX for example 0702 pack BCD is 72 load into the AX register is adjusted by the AAD instruction to 0048

H. Here AAD converts two-digit unpack BCD number i.e. 0702 into binary number (i.e. 0048) so it can be divided with the binary division instruction (DIV).

Post a Comment

0 Comments