GCD Function
Returns the Greatest Common (Positive) Divisor (GCD)of two Integers.

Unit
QESBPCSMath

Declaration
Function GCD(const X, Y: LongWord): LongWord;

Description
Also Refered to as the Highest Common Factor (HCF). Uses Euclid's Algorithm. Please note that routine assumes GCD(0,0) to be 0. BASM Routine donated by Marcel Martin.

Parameters
First Value to process.
Second Value to process.

Category
Arithmetic Routines for Integers

Implementation

function GCD (const X, Y: LongWord): LongWord;
asm
	jmp   @01      // We start with EAX <- X, EDX <- Y, and check to see if Y = 0
@00: mov   ecx, edx	// ECX <- EDX prepare for division
	xor   edx, edx // clear EDX for Division
	div   ecx		// EAX <- EDX:EAX div ECX, EDX <- EDX:EAX mod ECX
	mov   eax, ecx	// EAX <- ECX, and repeat if EDX <> 0
@01: and   edx, edx // test to see if EDX is zero, without changing EDX
	jnz   @00		// when EDX is zero EAX has the result
End;


HTML generated by Time2HELP
http://www.time2help.com