SHARE
TWEET

Assembly & C Code

KirillDeVries Jul 25th, 2012 20 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gcd.asm:
  2.  
  3.  
  4. segment data
  5.  
  6. segment bss
  7.  
  8. segment text
  9.     global gcd
  10. gcd:
  11.     push ebp
  12.     mov  ebp,esp
  13.     mov  eax,[ebp+8]        ; x
  14.     mov  ebx,[ebp+12]       ; y
  15. looptop:
  16.     cmp  eax,0              ; if (x == 0) we're done
  17.     je   goback
  18.     cmp  eax,ebx            ; make certain x is the larger number
  19.     jge  modulo
  20.     xchg eax,ebx            ; swap x and y
  21. modulo:
  22.     cdq                    ; set up for division
  23.     idiv ebx               ; divide edxeax by eb
  24.     mov  eax,edx           ; the remainder is in edx
  25.     jmp  looptop
  26.  
  27. goback:
  28.     mov  eax,ebx           ; return y
  29.     mov  esp,ebp
  30.     pop  ebp
  31.     ret
  32.  
  33.  
  34.  
  35. gcdargs.asm:
  36.  
  37.  
  38. segment .data
  39.  
  40. segment .bss
  41.  
  42. segment .text
  43.     global gcdargs
  44. gcdargs:
  45.     push ebp
  46.     mov  ebp,esp
  47. looptop:
  48.     mov  eax,[ebp+8]
  49.     cmp  eax,0              ; if (x == 0) we're done
  50.     je   goback
  51.     mov  ebx,[ebp+12]
  52.     cmp  eax,ebx            ; make certain x is the larger number
  53.     jge  modulo
  54.     mov  eax,[ebp+8]
  55.     xchg eax,[ebp+12]
  56.     mov  [ebp+8],eax
  57. modulo:
  58.     mov  ebx,[ebp+12]
  59.     mov  eax,[ebp+8]
  60.     cdq                    ; set up for division
  61.     idiv ebx               ; divide edxeax by ebx
  62.     mov  [ebp+8],edx           ; the remainder is in edx
  63.     jmp  looptop
  64.  
  65. goback:
  66.     mov  eax,[ebp+12]           ; return y
  67.     mov  esp,ebp
  68.     pop  ebp
  69.     ret
  70.  
  71.  
  72.  
  73. gcdlocal.asm:
  74.  
  75.  
  76. segment .data
  77.  
  78. segment .bss
  79.  
  80. segment .text
  81.     global gcdlocal
  82. gcdlocal:
  83.     push ebp
  84.     mov  ebp,esp
  85.     sub  esp,8              ; room for local variables
  86.     mov  eax,[ebp+8]        ; x
  87.     mov  [ebp-4],eax
  88.     mov  eax,[ebp+12]       ; y
  89.     mov  [ebp-8],eax
  90. looptop:
  91.     mov  eax,[ebp-4]
  92.     cmp  eax,0              ; if (x == 0) we're done
  93.     je   goback
  94.     cmp  eax,[ebp-8]        ; make certain x is the larger number
  95.     jge  modulo
  96.     xchg eax,[ebp-8]
  97.     mov  [ebp-4],eax
  98. modulo:
  99.     mov  ebx,[ebp-8]
  100.     mov  eax,[ebp-4]
  101.     cdq                    ; set up for division
  102.     idiv ebx               ; divide edxeax by ebx
  103.     mov  [ebp-4],edx       ; the remainder is in edx
  104.     jmp  looptop
  105.  
  106. goback:
  107.     mov  eax,[ebp-8]           ; return y
  108.     mov  esp,ebp
  109.     pop  ebp
  110.     ret
  111.  
  112.  
  113.  
  114. gcdmem.asm:
  115.  
  116.  
  117. segment .data
  118.  
  119. segment .bss
  120. x:    resd  1
  121. y:    resd  1
  122. segment .text
  123.     global gcdmem
  124. gcdmem:
  125.     push ebp
  126.     mov  ebp,esp
  127.     mov  eax,[ebp+8]        ; x
  128.     mov  [x],eax
  129.     mov  eax,[ebp+12]       ; y
  130.     mov  [y],eax
  131. looptop:
  132.     mov  eax,[x]
  133.     cmp  eax,0              ; if (x == 0) we're done
  134.     je   goback
  135.     mov  ebx,[y]
  136.     cmp  eax,ebx            ; make certain x is the larger number
  137.     jge  modulo
  138.     mov  [x],ebx
  139.     mov  [y],eax
  140. modulo:
  141.     mov  ebx,[y]
  142.     mov  eax,[x]
  143.     cdq                    ; set up for division
  144.     idiv ebx               ; divide edxeax by ebx
  145.     mov  [x],edx           ; the remainder is in edx
  146.     jmp  looptop
  147.  
  148. goback:
  149.     mov  eax,[y]           ; return y
  150.     mov  esp,ebp
  151.     pop  ebp
  152.     ret
  153.  
  154. --------------------------------------
  155. mainline.c:
  156.  
  157.  
  158. #include <stdio.h>
  159.  
  160. int gcd(int a,int b);
  161.  
  162. int main()
  163. {
  164.     int result;
  165.     int a;
  166.     int b;
  167.  
  168.     a = 46;
  169.     b = 90;
  170.     printf("%d and %d have a gcd of %d\n",a,b,gcd(a,b));
  171.  
  172.     a = 9863;
  173.     b = 41272;
  174.     printf("%d and %d have a gcd of %d\n",a,b,gcd(a,b));
  175. }
  176.  
  177.  
  178.  
  179. mainlineargs.c:
  180.  
  181.  
  182.  
  183. #include <stdio.h>
  184.  
  185. int gcdargs(int a,int b);
  186.  
  187. int main()
  188. {
  189.     int result;
  190.     int a;
  191.     int b;
  192.  
  193.     a = 46;
  194.     b = 90;
  195.     printf("%d and %d have a gcd of %d\n",a,b,gcdargs(a,b));
  196.  
  197.     a = 9863;
  198.     b = 41272;
  199.     printf("%d and %d have a gcd of %d\n",a,b,gcdargs(a,b));
  200. }
  201.  
  202. mainlinelocal.c:
  203.  
  204.  
  205.  
  206. #include <stdio.h>
  207.  
  208. int gcdlocal(int a,int b);
  209.  
  210. int main()
  211. {
  212.     int result;
  213.     int a;
  214.     int b;
  215.  
  216.     a = 46;
  217.     b = 90;
  218.     printf("%d and %d have a gcd of %d\n",a,b,gcdlocal(a,b));
  219.  
  220.     a = 9863;
  221.     b = 41272;
  222.     printf("%d and %d have a gcd of %d\n",a,b,gcdlocal(a,b));
  223. }
  224.  
  225.  
  226. mainlinemem.c:
  227.  
  228.  
  229. #include <stdio.h>
  230.  
  231. int gcdmem(int a,int b);
  232.  
  233. int main()
  234. {
  235.     int result;
  236.     int a;
  237.     int b;
  238.  
  239.     a = 46;
  240.     b = 90;
  241.     printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
  242.  
  243.     a = 9863;
  244.     b = 41272;
  245.     printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
  246. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top