0% found this document useful (0 votes)
9 views

EXPERIMENT NUMBER 10

This document outlines an assembly language program designed to calculate the GCD of two 16-bit unsigned numbers using the Euclidean algorithm. It includes a detailed explanation of the algorithm, a flowchart, and the complete program code written in MASM. The program successfully executes and verifies the GCD of the specified numbers.

Uploaded by

waghjayesh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

EXPERIMENT NUMBER 10

This document outlines an assembly language program designed to calculate the GCD of two 16-bit unsigned numbers using the Euclidean algorithm. It includes a detailed explanation of the algorithm, a flowchart, and the complete program code written in MASM. The program successfully executes and verifies the GCD of the specified numbers.

Uploaded by

waghjayesh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NUMBER :10

TITLE :

Write an Assembly Language Program to find the GCD of two 16 bit unsigned
numbers

Software required: MASM (Macro Assembler from Microsoft Corp.)


Explanation : GCD of two numbers is performed by dividing the greater number by the smaller number
till the remainder is zero. If it is zero the divisor is the GCD if not the remainder and the divisor of the
previous division are the new set of two numbers. The process is repeated by dividing greater of the two
numbers by smaller number till the remainder is zero and GCD is found.

Example:

First number = 90 Second number = 120

Iteration Operation Remainder


1 120 % 90 30
2 90 % 30 0
Hence , GCD is 30.

Algorithm;

1. Initialize the data segment.


2. Load AX and BX registers with operands.
3. Check if two numbers are equal. If yes go to step 10, else go to step 4.
4. Is number 1 > number 2 ? If yes go to step 6 else go to step 5.
5. Exchange the contents of AX and BX register, such that AX contains the bigger number.
6. Initialize DX register with 00H
7. Perform the division operation (AX/BX).
8. Check if there is remainder. If yes go to step 9, else go to step 10.
9. Move the remainder into AX register and go to step 4.
10. Save the contents of BX as GCD.
11. Display the result.
12. Stop.

Flowchart:
Start

Load AX and BX registers with operands

Y
Is no 1= no 2 ?

N
Y
Is no 1 >no 2 ?

N
Interchange contents

DX = 0000H

Perform division (no 1/no 2)

Y
Is Rem = 0 ?

N
Move remainder into AX

Save and display result

Stop
Program:

.MODEL SMALL

.DATA

NO1 DW 0120

NO2 DW 0090

GCD DW 0H

.CODE

MOV AX,@DATA

MOV DS, AX

MOV AX,NO1

MOV BX,NO2

AGAIN: CMP AX,BX

JE FINISH

JB EXCHG

UP: MOV DX,0

DIV BX

CMP DX,0

JE FINISH

MOV AX,DX

JMP AGAIN

EXCHG: XCHG AX,BX

JMP UP

FINISH: MOV GCD,BX

mov ch,04h ;count of digits to be displayed

mov cl,04h ;count to roll by 4 bits

L1: rol bx,cl ;roll bl so that msb comes to lsb


mov dl,bl ;load dl with data to be displayed

and dl,0fh ;get only lsb

cmp dl,09 ;check if digit is 0-9 or letter A-F

jbe L2

add dl,07 ;if letter add 37H else add 30H

L2: add dl,30h

mov ah,02h ;display character

int 21h

dec ch ;decrement count

jnz L1

mov ah,4ch

int 21h

END

CONCLUSION: Program executed to find GCD of the given numbers and output is verified.

You might also like