Initial Revision
[ohcount] / test / expected_dir / assembler1.asm / assembler / code
1 BITS 32
2 GLOBAL _gcdAsm
3 SECTION .text
4 _gcdAsm:
5 push ebp
6 mov ebp,esp
7 push ebx
8 push ecx
9 push edx
10 push edi
11 mov eax,[ebp + 8]   ; eax = a (0 <= a <= 2^31 - 1)
12 mov ebx,[ebp + 12]  ; ebx = b (0 <= b <= 2^31 - 1)
13 mov ecx,eax
14 or ecx,ebx
15 bsf ecx,ecx         ; greatest common power of 2 of a and b
16 jnz notBoth0
17 mov eax,1           ; if a = 0 and b = 0, return 1
18 jmp done
19 notBoth0:
20 mov edi,ecx
21 test eax,eax
22 jnz aNot0
23 mov eax,ebx         ; if a = 0, return b
24 jmp done
25 aNot0:
26 test ebx,ebx
27 jz done             ; if b = 0, return a
28 bsf ecx,eax         ; "simplify" a as much as possible
29 shr eax,cl
30 bsf ecx,ebx         ; "simplify" b as much as possible
31 shr ebx,cl
32 mainLoop:
33 mov ecx,ebx
34 sub ecx,eax         ; b - a
35 sbb edx,edx         ; edx = 0 if b >= a or -1 if a > b
36 and ecx,edx         ; ecx = 0 if b >= a or b - a if a > b
37 add eax,ecx         ; a-new = min(a,b)
38 sub ebx,ecx         ; b-new = max(a,b)
39 sub ebx,eax         ; the difference is >= 0
40 bsf ecx,eax         ; "simplify" as much as possible by 2
41 shr eax,cl
42 bsf ecx,ebx         ; "simplify" as much as possible by 2
43 shr ebx,cl
44 jnz mainLoop        ; keep looping until ebx = 0
45 mov ecx,edi         ; shift back with common power of 2
46 shl eax,cl
47 done:
48 pop edi
49 pop edx
50 pop ecx
51 pop ebx
52 mov esp,ebp
53 pop ebp
54 ret                 ; eax = gcd(a,b)