Open In App
Related Articles

8086 program to convert a 16 bit decimal number to binary

Improve Article
Save Article
Like Article

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


Input: d1 = 16
Output: 10000

Input: d1 = 7
Output: 111 


  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


;8086 program to convert a 16 bit decimal number to binary
    .STACK 100H
        d1 dw 16
            MOV AX,
        MOV DS,
;load the value stored;
in variable d1
    mov ax,
;convert the value to binary;
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 2 mov bx, 2
;divide it by 2
;to convert it to binary
    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
;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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jun, 2021
Like Article
Save Article
Similar Reads