using the irvine.32 library only. The greatest common divisor (GCD) of two integ
ID: 3717829 • Letter: U
Question
using the irvine.32 library only. 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
Program should load two registers with two Numbers and then apply the logic for GCD of two Numbers . 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 the smaller number till the remainder is zero and GCD is found. INCLUDE irvine32.inc .data msg1 byte " Enter first number", 0ah, 0dh,0 msg2 byte " Enter second number",0ah, 0dh,0 msg3 byte " GCD is ",0ah,0dh,0 ValA DWORD ? ValB DWORD ? .code main PROC ..............main start: mov edx, offset msg1 call WriteString call ReadDec mov ValA,eax mov edx, offset msg2 call WriteString call ReadDec mov ValB,eax mov eax,DWORD ptr[ValA] mov ebx,DWORD ptr[ValB] push ValB push ValA call CalcGcd call DumpRegs call start exit main ENDP CalcGcd PROC push ebp mov ebp, esp xor edx,edx mov eax, [ebp+8] mov ebx, [ebp+12] L1: cmp eax,ebx JE DONE JB EXCH L2: div ebx cmp edx,0 JE DONE mov eax,ebx mov ebx,edx JMP L1 EXCH: XCHG eax,ebx JMP L1 DONE: mov eax,ebx mov edx, offset msg3 call writestring call writedec ret 8 CalcGcd ENDP END main .....end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.