# 5 the greatest common divisor gcd of two positive

• Homework Help
• 4
• 89% (9) 8 out of 9 people found this document helpful

This preview shows page 4 out of 4 pages.

5.The greatest common divisor (GCD) of two positive integers m and n can be calculated recursively bythe function described below in pseudocode.function GCD(m, n : integer) : integer;if n = 0thenreturn m;elseRemainder := m mod n;return GCD(n, Remainder);end if;Implement this recursive definition in assembly language. Use the stack to pass the two doubleword-size argument values. Return the value of the function in the EAX register. The procedure should removethe parameters from the stack. Test your function with a main program that inputs two integers, calls thegreatest common divisor function GCD, and displays the value returned.Program:GCD:pushebp;saveebp registermovebp, espmoveax, [ebp+8];pass second number into eaxmovecx, [ebp+12];pass first number into ecxpushedx;save edx registerloop:cmp0, ecx;stop when remainder is 0jeendxoredx, edx;clear edx in order to perform eax/ecx becausedoubleword operation uses (edx:eax)/ecxdivecx;eax divided by ecx , quotient in eax, remainder in edxmoveax, ecx;store the current result into eaxmovecx, edx;move remainder to ecx for next divisionjmploopend:popedx;remove from stackpopebp;remove from stackleaveret
End of preview. Want to read all 4 pages?