8086 program to convert a 16 bit decimal number to binary

Last Updated : 21 Aug, 2025

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

Examples: 

Input: d1 = 16
Output: 10000

Input: d1 = 7
Output: 111

Explanation:  

  1. Load the value stored into register
  2. Divide the value by 2 to convert it to binary
  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. Add 48 to the top element to convert it into ASCII
  9. Print the character using interrupt
  10. Decrements the count

Program: 

CPP
; 8086 program to convert a 16-bit decimal number to binary
.MODEL SMALL
.STACK 100H
.DATA
    d1 DW 16        ; input decimal number
.CODE
MAIN PROC FAR
    MOV AX, @DATA   ; initialize DS
    MOV DS, AX

    MOV AX, d1      ; load number into AX
    CALL PRINT      ; call print procedure

    MOV AH, 4CH     ; terminate program
    INT 21H
MAIN ENDP

;--------------------------------------------
; Procedure to convert AX to binary and print
;--------------------------------------------
PRINT PROC
    MOV CX, 0       ; count = 0
    XOR DX, DX

label1:
    CMP AX, 0       ; if AX = 0 -> done dividing
    JE print1

    MOV BX, 2       ; divisor = 2
    XOR DX, DX      ; clear remainder
    DIV BX          ; AX = AX / 2, remainder -> DX
    PUSH DX         ; push remainder (0 or 1)
    INC CX          ; increment count
    JMP label1

print1:
    CMP CX, 0
    JE exit         ; if no more digits, exit

    POP DX          ; get remainder
    ADD DL, 48      ; convert to ASCII ('0'/'1')
    MOV AH, 02h     ; DOS print char
    INT 21h

    DEC CX
    JMP print1

exit:
    RET
PRINT ENDP
END MAIN

Output: 

10000

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
 

Comment

Explore