Skip to content
Related Articles

Related Articles

8086 program to convert a 16 bit decimal number to Hexadecimal
  • Last Updated : 26 Jun, 2019

Problem: We are given a 16 bit decimal number we have to print the number in Hexadecimal format.


Input: d1 = 999
Output: 3E7

Input: d1 = 123
Output: 7B 


  1. Load the value stored into register
  2. Divide the value by 16 to convert it to Hexadecimal
  3. Push the remainder into the stack
  4. Increase the count
  5. Repeat the steps until the value of the register is greater than 0
  6. Until the count is greater than zero
  7. POP the stack
  8. If the value of top of stack is greater than 9
  9. Then add 7 more to the value so that they correspond to HEX characters A, B, C, D, E, F
  10. Add 48 to the top element to convert it into ASCII
  11. Print the character using interrupt
  12. Decrements the count






;8086 program to convert a 16 bit decimal number to Hexadecimal
    .STACK 100H
        d1 dw 999
            MOV AX,
        MOV DS,
;load the value stored;
in variable d1
    mov ax,
;convert the value to Hexadecimal;
print the value
;interrupt to exit
    MOV AH,
    4CH INT 21H
        PRINT PROC
;initialize count
    mov cx,
    0 mov dx, 0 label1:
    ax is zero
        cmp ax,
        0 je print1
;initialize bx to 16 mov bx, 16
;divide it by 16
;to convert it to Hexadecimal
    div bx
;push it in the stack
    push dx
;increment the count
    inc cx
;set dx to 0
    xor dx,
        jmp label1
;check if count
;is greater than zero
    cmp cx,
    0 je exit
;pop the top of stack
    pop dx
;compare the value
;with 9 cmp dx, 9 jle continue
value is greater than 9
;then add 7 so that after
;adding 48 it represents A
;for example 10 + 7 + 48 = 65
;which is ASCII value of A
    add dx,
;add 48 so that it
;represents the ASCII
;value of digits
    add dx,
;interrupt to print a;
    mov ah,
    02h int 21h
;decrease the count
    dec cx
        jmp print1
            exit : ret
                       PRINT ENDP
                           END MAIN




Note: The program cannot be run on an online editor, please use MASM to run the program and use dos box to run MASM, you might use any 8086 emulator to run the program

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :