In: Computer Science
Assembly Language HW
For this program you will find the largest common factor for 2 numbers, a and b.
The largest common factor is the largest positive number that divides the two numbers.
The C++ code for doing this is:
int LCF (int a, int b)
{
if (a == 0 && b == 0)
b = 1;
else
if (b == 0)
b = a;
else
if (a != 0)
while (a != b)
if (a < b)
b -= a;
else
a -= b;
return b;
}
In your code, you should follow this C++ code as closely as possible.
Read in a and b.
Put a in eax, and b in ebx.
Do not use any loop instructions directly.
Do use compare and jump instructions.
Do not use any procedures except MyProgram.
(the return of an integer from LCF is NOT coded, but replaced with a write of the result
as shown below)
You code should be your work alone.
You example of your console window should look as follows:
Please provide an integer for b: 8
Please provide an integer for a: 4
The largest common factor of a and b is: +4
#include<iostream>
using namespace std;
int helper(int a, int b)
{
if (a == 0 && b == 0)
{
b = 1;
}
else
{
if (b == 0)
b = a;
else
{ if (a != 0)
while (a != b)
{ if (a < b)
b -= a;
else
a -= b;
}
}
}
return b;
}
int main()
{
int eax,ebx;
cout<<"Please provide an integer for b : "<<endl;
cin>>ebx;
cout<<"Please provide an integer for a : "<<endl;
cin>>eax;
int ans=helper(eax,ebx);
cout<<"The largest common factor of a and b is : <<"+"<<ans<<endl;
return 0;
}