| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- Write a program in 8086 assembly language, which converts a packed four-digit packed BCD number to equivalent four ASCII digits; for example, a packed BCD number “4367” stored in a word is converted to ASCII “4”, “3”, “6”, “7”. Explain the algorithm of the program
- January 15, 2025 by Prathamesh
- 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 and SI to point to the array for ASCII digits.
- Repeat the following steps for each digit (4 times):
- a. Load the packed BCD number into AX.
- b. Extract the least significant digit by performing a bitwise AND operation with 0Fh.
- 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.
|