8086 program to convert a 16 bit Decimal number to Octal

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

Examples:

Input: d1 = 16
Output:20

Input: d1 = 123
Output: 173 

Explanation:



  1. Load the value stored into register
  2. Divide the value by 8 to convert it to octal
  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:

filter_none

edit
close

play_arrow

link
brightness_4
code

;8086 program to convert a 16 bit decimal number to octal
    .MODEL SMALL
    .STACK 100H
    .DATA
        d1 dw 16
    .CODE
        MAIN PROC FAR
            MOV AX,
    @DATA
        MOV DS,
    AX
  
;load the value stored;
in variable d1
    mov ax,
    d1
  
;convert the value to octal;
print the value
    CALL PRINT
  
;interrupt to exit
    MOV AH,
    4CH INT 21H
  
    MAIN ENDP
        PRINT PROC
  
;initialize count
    mov cx,
    0 mov dx, 0 label1:;
if
    ax is zero
        cmp ax,
        0 je print1
  
;initialize bx to 8 mov bx, 8
  
;divide it by 8;
to convert it to octal
    div bx
  
;push it in the stack
    push dx
  
;increment the count
    inc cx
  
;set dx to 0
    xor dx,
    dx
        jmp label1
            print1:
  
;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,
    48
  
;interrupt to print a
;character
    mov ah,
    02h int 21h
  
;decrease the count
    dec cx
        jmp print1
            exit : ret
                       PRINT ENDP
                           END MAIN

chevron_right


Output:

20

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.



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.