Here’s an assembly language program for the 8086 microprocessor that converts a packed four-digit BCD number to equivalent ASCII digits:
.model small
.stack 100h
.data
packed_bcd_word dw 4367h ; Packed BCD number stored in a word
ascii_digits db 4 dup('$') ; Array to store ASCII digits
.code
main:
mov ax, @data ; Load data segment address to AX
mov ds, ax ; Set data segment register
mov bx, offset packed_bcd_word ; BX points to the packed BCD number
lea si, ascii_digits ; SI points to the array for ASCII digits
mov cx, 4 ; Repeat the conversion process for 4 digits
convert_digit:
mov ax, [bx] ; Load the packed BCD number into AX
and ax, 0Fh ; Extract the least significant digit
add al, '0' ; Convert the digit to ASCII
mov [si], al ; Store the ASCII digit in the array
inc si ; Move to the next element in the array
shr word ptr [bx], 4 ; Shift the packed BCD number to the right by 4 bits
loop convert_digit ; Repeat for the next digit
; Display the ASCII digits (optional)
mov dx, offset ascii_digits ; DX points to the array of ASCII digits
mov ah, 9 ; AH = 9 (DOS function for displaying string)
int 21h ; Call DOS interrupt for displaying string
mov ax, 4C00h ; AH = 4Ch (DOS function for program termination)
int 21h ; Call DOS interrupt for program termination
end main
Algorithm Explanation:
- Set up the data segment register (
DS
) with the address of the data segment. - Initialize registers
BX
to point to the packed BCD number andSI
to point to the array for ASCII digits. - Repeat the following steps for each digit (4 times):
a. Load the packed BCD number intoAX
.
b. Extract the least significant digit by performing a bitwise AND operation with0Fh
.
c. Convert the digit to ASCII by adding'0'
to it.
d. Store the ASCII digit in the array.
e. Shift the packed BCD number to the right by 4 bits to get the next digit. - Display the ASCII digits (optional).
- Terminate the program.
This algorithm iterates through each digit of the packed BCD number, extracts the least significant digit, converts it to ASCII, and stores it in an array. Finally, it displays the ASCII digits if desired.