Skip to content
Related Articles
Open in App
Not now

Related Articles

8086 program to find GCD of two numbers and print the GCD

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 03 Jun, 2021
Improve Article
Save Article

Problem: We are given two decimal numbers we have to find the GCD of two numbers and print the GCD in decimal format.
Refer for finding GCD of two numbers.


Input: d1 = 16, d2 = 24
Output: GCD = 8

Input: d1 = 12, d2 = 18
Output: GCD = 6 


  1. Load value d1 in ax and d2 in bx
  2. Call the gcd function
  3. If value in bx is zero
  4. Then set the value of gcd ( CX ) as ax
  5. Else set the value of ax as bx and value of bx as ax % bx
  6. Call the gcd function recursively
  7. Load the value of cx into ax
  8. Call the print function to print the gcd of two numbers


    .STACK 100H
        d1 dw 16 d2 dw 24
            MOV AX,
        MOV DS,
;initialize ax and bx
    mov bx,
        mov ax,
;find gcd of two numbers
    call gcd
;load the gcd in ax
    mov ax,
;print the value
;interrupt to exit
    MOV AH,
    4CH INT 21H
        GCD PROC
    bx is 0 cmp bx, 0 jne continue
;then gcd is ax
    mov cx,
;else gcd(b, a % b)
    xor dx,
;divide ax by bx
    div bx
;initialize ax as bx
    mov ax,
bx as ax % bx
    mov bx,
;recursively call gcd
    call GCD
            GCD ENDP
                PRINT PROC
;initialize count
    mov cx,
    0 mov dx, 0 label1:
    ax is zero
        cmp ax,
        0 je print1
;initialize bx to 10 mov bx, 10
;extract the last digit
    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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!