; PROGRAM: GCD AND LCM
read macro num
mov ah, 01h
int 21h
sub al, 30h
mov bh, 0ah
mul bh
mov num, al
mov ah, 01h
int 21h
sub al, 30h
add num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg db 'Enter Number: $'
gcdis db 'GCD: $'
lcmis db 'LCM: $'
nl db 0dh,0ah,'$'
num1 db 00h
num2 db 00h
gcd db 00h
lcm dw 00h
pgcd db 5 dup('0$')
plcm db 5 dup('0$')
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
print msg
read num1
print nl
print msg
read num2
mov bh, num1
mov bl, num2
cmp bh, 00h
je ZERO_NUM
cmp bl, 0h
je ZERO_NUM
cmp bh, bl ;
jl BH_SMALLER
xchg bh, bl
BH_SMALLER: ;BH smaller val
mov cl, bh
DIV_GCD:
mov al, bh
mov ah, 00h
div cl
cmp ah, 00h
jne SKIP_DIV
mov al, bl
mov ah, 00h
div cl
cmp ah, 00h
je GCD_FOUND
SKIP_DIV:
dec cl
jmp DIV_GCD
GCD_FOUND:
mov gcd, cl
mov al, cl ;Print GCD
mov ah, 00h
mov si, offset pgcd
call hex2ascii
print nl
print gcdis
print pgcd
;LCM = (A*B)/GCD
mov al, num1
mov bl, num2
mul bl
mov cl, gcd
div cl
mov lcm, ax ;Print LCM
mov si, offset plcm
call hex2ascii
print nl
print lcmis
print plcm
jmp EXIT
ZERO_NUM: ;If any of Num is 0
print nl
print gcdis
print pgcd
print nl
print lcmis
print plcm
EXIT:
mov ah, 4ch
int 21h
hex2ascii proc near
mov bx, 000ah
mov cx, 0000h
REP_DIV:
mov dx, 0000h
div bx
add dl, 30h
push dx
inc cx
cmp al, 0ah
jge REP_DIV
cmp al, 00h
je SKIP_0
add al, 30h
mov [si], al
inc si
SKIP_0:
SAVE_ASCII:
pop dx
mov [si], dl
inc si
loop SAVE_ASCII
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>gcd_lcm
Enter Number: 02
Enter Number: 04
GCD: 2
LCM: 4
Z:\S5IT\masm>gcd_lcm
Enter Number: 06
Enter Number: 08
GCD: 2
LCM: 24
**************************
0 comments:
Post a Comment