Hi guy! Please someone could help with this project! Description: The greatest c
ID: 3548611 • Letter: H
Question
Hi guy! Please someone could help with this project!
Description:
The greatest common divisor of two integers is the largest integer that will evenly
divide both integers.
The
GCD algorithm involves integer division in a loop, described by the following
C++ code:
int
GCD(int x, int y) { x = abs(x); // absolute value y = abs; do { int n = x
% y; x = y; y = n; } while y > 0; return y; }
Implement this function in assembly language and write a test
program That calls the function several times, passing it different
values. Display all results on the screen and include screen shots of the
outputs.
Thank
The screen shot of the out put will be also helpfull!
The greatest common divisor of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division in a loop, described by the following C++ code: int GCD(int x, int y) { x = abs(x); // absolute value y = absyes; do { int n = x % y; x = y; y = n; } while y > 0; return y; }Implement this function in assembly language and write a test program That calls the function several times, passing it different values. Display all results on the screen and include screen shots of the outputs.Explanation / Answer
PartA:
======
/* Program to accept the input values
of 2 integers whose GCD is to be calculated.
The program then calls the assembly routine that
calculates the GCD and returns the value to the calling
program */
#include <stdio.h>
main(){
int x,y, gcd;
extern int calc_gcd(int, int);
printf("Please enter the integers for which the GCD is to be calculated ");
scanf("%d %d",&x,&y);
gcd = calc_gcd(x,y);
printf("First Integer is: %d ",x);
printf("Second Integer is: %d ",y);
printf("GCD is: %d ",gcd);
}
================
Part B
================
;Assembly routine that receives two integers
;and computes the gcd.
;the first integer is found in [esp+4]
;the second integer is found in [esp+8]
;-------------------------------------...
segment .text
global calc_gcd
calc_gcd:
enter 0,0
mov ecx, 2
;----------------------
;Compute abs x and y
;----------------------
compute_abs:
fild dword [ebp+8]
fabs ; abs(x)
fist dword [ebp+8] ; store abs(x)
add ebp, 4
loop compute_abs ; Now move to the next operand
sub ebp, 8
xor edx, edx ; clear edx
mov eax, [ebp+8] ; move first integer x in eax.
mov ebx, [ebp+12]; move second integer y in ebx.
cmp eax, ebx ; if x < y then swap x and y to avoid fp exception on idiv.
jg again
xchg eax, ebx
;--------------------------
;Perform looping operation
;n = x % y
;x = y; y = n; if y!=0 repeat
;----------------------------
again:
div ebx ; quotient in eax, remainder in edx(n).
mov eax, ebx ; x = y
mov ebx, edx ; y = n
xor edx, edx
cmp ebx, 0
jnz again
leave
ret
Sample output1:
./a.out
Please enter the integers for which the GCD is to be calculated
-5
-20
First Integer is: -5
Second Integer is: -20
GCD is: 5
Sample output2:
Please enter the integers for which the GCD is to be calculated
13
-26
First Integer is: 13
Second Integer is: -26
GCD is: 13
Sample output3:
Please enter the integers for which the GCD is to be calculated
438
226
First Integer is: 438
Second Integer is: 226
GCD is: 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.