//---------------------------------------------------------------- // fastgcd.s // // Here is a faster implementation for our 'gcd()' function. // // assemble using: $ as fastgcd.s -o fastgcd.o // // programmer: ALLAN CRUSE // written on: 03 APR 2006 //---------------------------------------------------------------- .section .text gcd: # function prototype: int gcd( int m, int n ); push %ebp # preserve caller's EBP mov %esp, %ebp # setup access to frame # preserve the registers that we will clobber push %ebx # used for divisor push %edx # used by dividend # compute absolute-value of n and leave in EBX mov 12(%ebp), %eax # get integer n into EAX cdq # sign-extended into EDX xor %edx, %eax # flips bits if negative sub %edx, %eax # increments if negative mov %eax, %ebx # save abs( n ) in EBX # compute absolute value of m and leave in EAX mov 8(%ebp), %eax # get integer m into EAX cdq # sign-extended into EDX xor %edx, %eax # flips bits if negative sub %edx, %eax # increments if negative while: # execute loop unless EBX is zero or %ebx, %ebx # check: EBX equals zero? jz until # ready to return EAX xor %edx, %edx # prepare EDX for division div %ebx # divide (EDX,EAX) by EBX mov %ebx, %eax # assign EBX to EAX mov %edx, %ebx # assign EDX to EBX jmp while # check for zero remainder until: # restore clobbered registers pop %edx # restore saved registers pop %ebx leave # restore caller's frame ret # return control to caller .global gcd .end