Mixed Language ( C and TASM) program to find the GCD of two numbers

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
Objective:
Write a mixed language code to calculate GCD of  2 numbers.
Prerequisite:
Turbo C++ Compiler
Algorithm:
1.      Start
2.      Declare 3 numbers a,b,gcd as integers.
3.      Move the a and b value to AX and BX respectively
4.      Repeat till AX!=BX step no 5 to 7 otherwise goto 8
5.      If AX > BX then goto step no 6 else goto step no 7
6.      Subtract AXß AX-BX
7.      Subtract BXßBX-AX
8.      Move AX  or BX to gcd variable
9.      Display the gcd.

10.  Stop
TASM Program :
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,res;
cout<<"Please enter the first number:"<<endl;
cin>>a;
cout<<"Please enter the second number:"<<endl;
cin>>b;
cout<<endl;
asm mov ax,a
asm mov bx,b
bck:
asm cmp ax,bx
asm jc second
first:
asm sub ax,bx
asm jmp chk
second:
asm sub bx,ax
chk:
asm cmp ax,bx
asm jnz bck
asm mov res,ax
cout<<"The GCD is: "<<res;
getch();
}
output:-
Please enter the first number:
12                                                                              
Please enter the second number:                                                 
16 

The GCD is: 4   
--------------------------------

0 comments: