Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gcd.asm:
- segment data
- segment bss
- segment text
- global gcd
- gcd:
- push ebp
- mov ebp,esp
- mov eax,[ebp+8] ; x
- mov ebx,[ebp+12] ; y
- looptop:
- cmp eax,0 ; if (x == 0) we're done
- je goback
- cmp eax,ebx ; make certain x is the larger number
- jge modulo
- xchg eax,ebx ; swap x and y
- modulo:
- cdq ; set up for division
- idiv ebx ; divide edxeax by eb
- mov eax,edx ; the remainder is in edx
- jmp looptop
- goback:
- mov eax,ebx ; return y
- mov esp,ebp
- pop ebp
- ret
- gcdargs.asm:
- segment .data
- segment .bss
- segment .text
- global gcdargs
- gcdargs:
- push ebp
- mov ebp,esp
- looptop:
- mov eax,[ebp+8]
- cmp eax,0 ; if (x == 0) we're done
- je goback
- mov ebx,[ebp+12]
- cmp eax,ebx ; make certain x is the larger number
- jge modulo
- mov eax,[ebp+8]
- xchg eax,[ebp+12]
- mov [ebp+8],eax
- modulo:
- mov ebx,[ebp+12]
- mov eax,[ebp+8]
- cdq ; set up for division
- idiv ebx ; divide edxeax by ebx
- mov [ebp+8],edx ; the remainder is in edx
- jmp looptop
- goback:
- mov eax,[ebp+12] ; return y
- mov esp,ebp
- pop ebp
- ret
- gcdlocal.asm:
- segment .data
- segment .bss
- segment .text
- global gcdlocal
- gcdlocal:
- push ebp
- mov ebp,esp
- sub esp,8 ; room for local variables
- mov eax,[ebp+8] ; x
- mov [ebp-4],eax
- mov eax,[ebp+12] ; y
- mov [ebp-8],eax
- looptop:
- mov eax,[ebp-4]
- cmp eax,0 ; if (x == 0) we're done
- je goback
- cmp eax,[ebp-8] ; make certain x is the larger number
- jge modulo
- xchg eax,[ebp-8]
- mov [ebp-4],eax
- modulo:
- mov ebx,[ebp-8]
- mov eax,[ebp-4]
- cdq ; set up for division
- idiv ebx ; divide edxeax by ebx
- mov [ebp-4],edx ; the remainder is in edx
- jmp looptop
- goback:
- mov eax,[ebp-8] ; return y
- mov esp,ebp
- pop ebp
- ret
- gcdmem.asm:
- segment .data
- segment .bss
- x: resd 1
- y: resd 1
- segment .text
- global gcdmem
- gcdmem:
- push ebp
- mov ebp,esp
- mov eax,[ebp+8] ; x
- mov [x],eax
- mov eax,[ebp+12] ; y
- mov [y],eax
- looptop:
- mov eax,[x]
- cmp eax,0 ; if (x == 0) we're done
- je goback
- mov ebx,[y]
- cmp eax,ebx ; make certain x is the larger number
- jge modulo
- mov [x],ebx
- mov [y],eax
- modulo:
- mov ebx,[y]
- mov eax,[x]
- cdq ; set up for division
- idiv ebx ; divide edxeax by ebx
- mov [x],edx ; the remainder is in edx
- jmp looptop
- goback:
- mov eax,[y] ; return y
- mov esp,ebp
- pop ebp
- ret
- --------------------------------------
- mainline.c:
- #include <stdio.h>
- int gcd(int a,int b);
- int main()
- {
- int result;
- int a;
- int b;
- a = 46;
- b = 90;
- printf("%d and %d have a gcd of %d\n",a,b,gcd(a,b));
- a = 9863;
- b = 41272;
- printf("%d and %d have a gcd of %d\n",a,b,gcd(a,b));
- }
- mainlineargs.c:
- #include <stdio.h>
- int gcdargs(int a,int b);
- int main()
- {
- int result;
- int a;
- int b;
- a = 46;
- b = 90;
- printf("%d and %d have a gcd of %d\n",a,b,gcdargs(a,b));
- a = 9863;
- b = 41272;
- printf("%d and %d have a gcd of %d\n",a,b,gcdargs(a,b));
- }
- mainlinelocal.c:
- #include <stdio.h>
- int gcdlocal(int a,int b);
- int main()
- {
- int result;
- int a;
- int b;
- a = 46;
- b = 90;
- printf("%d and %d have a gcd of %d\n",a,b,gcdlocal(a,b));
- a = 9863;
- b = 41272;
- printf("%d and %d have a gcd of %d\n",a,b,gcdlocal(a,b));
- }
- mainlinemem.c:
- #include <stdio.h>
- int gcdmem(int a,int b);
- int main()
- {
- int result;
- int a;
- int b;
- a = 46;
- b = 90;
- printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
- a = 9863;
- b = 41272;
- printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement