packed bcd to unpacked bcd program 8086

packed bcd to unpacked bcd program 8086

Method 1 for packed bcd to unpacked bcd program 8086:

Write a program in 8086 assembly language to convert a two digit unpacked BCD number into equivalent ASCII digits and a packed BCD number. The packed BCD number is to be stored in BH register. Your program should print the two ASCII digits. You may assume that the unpacked BCD numbers are in the AL and BL registers

DATA SEGMENT
UNPACKEDBCD1 DB 05H
UNPACKEDBCD2 DB 02H
ASCII1       DB ?
ASCII2       DB ?
PACKEDBCD    DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
MOV AL,UNPACKEDBCD1
MOV BL,UNPACKEDBCD2
MOV AH,AL
MOV BH,BL
ADD AH,30H
ADD BH,30H
MOV ASCII1,AH
MOV ASCII2,BH
MOV CL,04H
ROL AL,CL
OR  AL,BL
MOV BH,AL
MOV PACKEDBCD,BH
MOV DL,ASCII1
MOV AH,2
INT 21H
MOV DL,ASCII2
MOV AH,2
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START

Method 2 for packed bcd to unpacked bcd program 8086:

// Manually store the packed BCD number [eg: 98 in this case] in the memory locations 3000H
// Store the result i.e, the unpacked numbers in the memory locations 3001H and 3002H
// For this Example result will be 09 and 08
// 3000<-09, 3001<-08

#ORG 0000H
#BEGIN 0000H

    LDA 3000H  //Get the packed BCD number from the memory
    MOV B,A
    MVI C,04
    ANI F0     // A = 90H

L1: RRC        // Need to be rotated right for 4 times to get A = 09H
    DCR C
    JNZ L1

    STA 3001
    MOV A,B
    ANI 0F     // A = 08H
    STA 3002
    HLT

#ORG 3000H
#DB 98H


Method 3 for packed bcd to unpacked bcd program 8086:

Let packed BCD no →→ unpacked BCD no
47 →→ 04 (upper byte) 07 (Lower byte)
ALP to covert two digit packed
BCD no eq 47BCD47BCD into
Unpkd BCD number 04 0704 07.
Data Segment
PKDBCDNO dB 47
UnPKD HB DB ?
UnPKDLB DB ?
Data Ends
Code Segment
Assume CS : Code, DS : data
Start : MOV AX, Data ;

MOV DS, AX ; initdata seg

MOV AL , PKDBCDMO ; AL $\leftarrow$  47

AND AL, OFH ; AL $\leftarrow$  07

MOV UNPKDLB, AL ; UNPKDLB = 07

MOV AL, PKDBCDNO ; AL $\leftarrow$  47

AND AL, F0H ; AL $\leftarrow$  40

MOV CL, 04H ; init Count

SHR AL, CL ; Shift logically

Right AL $\leftarrow$  04

MOV UNPKDHB , AL , UNPKD HB = 04

MOV AH 1 , 4CH ;

INT 21H Terminate program

Code ends

End start

End.
In above program we get unpkd BCD value in the variables.
UNPKDHB = 04
UNPKD LB = 07.

No comments: