PLEASE YOUR OWN WORK............. INSTRUCTOR APPLY PLAGIARISM CHECKER 6. Greates
ID: 3712149 • Letter: P
Question
PLEASE YOUR OWN WORK............. INSTRUCTOR APPLY PLAGIARISM CHECKER
6. Greatest Common Divisor (GCD)
The greatest common divisor (GCD) 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
pseudocode:
int GCD(int x, int y)
{
x = abs(x) // absolute value
y = abs(y)
do {
int n = x % y
x = y
y = n
} while (y > 0)
return x
}
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.
Explanation / Answer
Include Irvine32.inc
.data
array DW 10,85,56,78,16,120,32,652,39,36
str1 byte "GCD is - ",0
.code
main PROC
mov ecx, LENGTHOF array /2
mov esi,OFFSET array
l1:
mov edi,2
l3:
mov eax,[esi]
add esi,4
cmp eax,0
jl l9
jmp l2
l9:
neg eax
l2:
push eax
dec edi
cmp edi,0
jne l3
call gcd
mov edx, offset str1
call writestring
call writedec
call crlf
loop l1
exit
main ENDP
gcd proc
pop edi
pop eax
pop ebx
gcd1:
mov edx,0
div ebx
mov eax,ebx
mov ebx,edx
cmp ebx,0
jg gcd1
push edi
ret
gcd endp
END main
Result is :
GCD is – 5
GCD is – 2
GCD is – 8
GCD is – 4
GCD is – 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.