displayNumber2.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 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
  2. January 15, 2025 by Prathamesh
  3. Here’s an assembly language program for the 8086 microprocessor that converts a packed four-digit BCD number to equivalent ASCII digits:
  4. .model small
  5. .stack 100h
  6. .data
  7. packed_bcd_word dw 4367h ; Packed BCD number stored in a word
  8. ascii_digits db 4 dup('$') ; Array to store ASCII digits
  9. .code
  10. main:
  11. mov ax, @data ; Load data segment address to AX
  12. mov ds, ax ; Set data segment register
  13. mov bx, offset packed_bcd_word ; BX points to the packed BCD number
  14. lea si, ascii_digits ; SI points to the array for ASCII digits
  15. mov cx, 4 ; Repeat the conversion process for 4 digits
  16. convert_digit:
  17. mov ax, [bx] ; Load the packed BCD number into AX
  18. and ax, 0Fh ; Extract the least significant digit
  19. add al, '0' ; Convert the digit to ASCII
  20. mov [si], al ; Store the ASCII digit in the array
  21. inc si ; Move to the next element in the array
  22. shr word ptr [bx], 4 ; Shift the packed BCD number to the right by 4 bits
  23. loop convert_digit ; Repeat for the next digit
  24. ; Display the ASCII digits (optional)
  25. mov dx, offset ascii_digits ; DX points to the array of ASCII digits
  26. mov ah, 9 ; AH = 9 (DOS function for displaying string)
  27. int 21h ; Call DOS interrupt for displaying string
  28. mov ax, 4C00h ; AH = 4Ch (DOS function for program termination)
  29. int 21h ; Call DOS interrupt for program termination
  30. end main
  31. Algorithm Explanation:
  32. Set up the data segment register (DS) with the address of the data segment.
  33. Initialize registers BX to point to the packed BCD number and SI to point to the array for ASCII digits.
  34. Repeat the following steps for each digit (4 times):
  35. a. Load the packed BCD number into AX.
  36. b. Extract the least significant digit by performing a bitwise AND operation with 0Fh.
  37. c. Convert the digit to ASCII by adding '0' to it.
  38. d. Store the ASCII digit in the array.
  39. e. Shift the packed BCD number to the right by 4 bits to get the next digit.
  40. Display the ASCII digits (optional).
  41. Terminate the program.
  42. 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.