Archetypical Example: Euclid's Method of Computing Greatest Common Divisors (assembler) 超典型的ソースコードの見本~最大公約数を計算する、ユークリッドの互除法 (アセンブラ)

形式
Asm
投稿日時
2015-03-31 15:13
公開期間
無期限
  1. .file "gcd.c"
  2. .text
  3. .globl gcd
  4. .type gcd, @function
  5. gcd:
  6. pushl %ebp
  7. movl %esp, %ebp
  8. subl $16, %esp
  9. jmp .L2
  10. .L3:
  11. movl 8(%ebp), %eax
  12. cmpl 12(%ebp), %eax
  13. jge .L4
  14. movl 8(%ebp), %eax
  15. movl %eax, -8(%ebp)
  16. movl 12(%ebp), %eax
  17. movl %eax, 8(%ebp)
  18. movl -8(%ebp), %eax
  19. movl %eax, 12(%ebp)
  20. .L4:
  21. movl 12(%ebp), %eax
  22. subl %eax, 8(%ebp)
  23. .L2:
  24. movl 8(%ebp), %eax
  25. cmpl 12(%ebp), %eax
  26. jne .L3
  27. movl 8(%ebp), %eax
  28. leave
  29. ret
  30. .size gcd, .-gcd
  31. .section .rodata
  32. .LC0:
  33. .string "usage: %s <numA> <numB>\n"
  34. .align 4
  35. .LC1:
  36. .string "\tto calculate the greatest common divisor\n\tof two unsigned numbers."
  37. .LC2:
  38. .string "greatest common divisor: %ld\n"
  39. .LC3:
  40. .string "%ld/%ld => %ld/%ld\n"
  41. .align 4
  42. .LC4:
  43. .string "%s is not an unsigned number I can recognize.\n"
  44. .type .LSSH0, @object
  45. .size .LSSH0, 5
  46. .LSSH0:
  47. .string "main"
  48. .text
  49. .globl main
  50. .type main, @function
  51. main:
  52. leal 4(%esp), %ecx
  53. andl $-16, %esp
  54. pushl -4(%ecx)
  55. pushl %ebp
  56. movl %esp, %ebp
  57. pushl %ebx
  58. pushl %ecx
  59. subl $64, %esp
  60. call __i686.get_pc_thunk.bx
  61. addl $_GLOBAL_OFFSET_TABLE_, %ebx
  62. movl 4(%ecx), %eax
  63. movl %eax, -44(%ebp)
  64. movl __guard_local@GOTOFF(%ebx), %eax
  65. movl %eax, -12(%ebp)
  66. xorl %eax, %eax
  67. cmpl $3, (%ecx)
  68. je .L9
  69. movl -44(%ebp), %eax
  70. movl (%eax), %eax
  71. movl %eax, 4(%esp)
  72. leal .LC0@GOTOFF(%ebx), %eax
  73. movl %eax, (%esp)
  74. call printf@PLT
  75. leal .LC1@GOTOFF(%ebx), %eax
  76. movl %eax, (%esp)
  77. call puts@PLT
  78. movl $1, -48(%ebp)
  79. jmp .L11
  80. .L9:
  81. movl -44(%ebp), %eax
  82. addl $4, %eax
  83. movl (%eax), %edx
  84. movl $0, 8(%esp)
  85. leal -24(%ebp), %eax
  86. movl %eax, 4(%esp)
  87. movl %edx, (%esp)
  88. call strtoul@PLT
  89. movl %eax, -16(%ebp)
  90. movl -44(%ebp), %eax
  91. addl $4, %eax
  92. movl (%eax), %edx
  93. movl -24(%ebp), %eax
  94. cmpl %eax, %edx
  95. jae .L12
  96. movl -44(%ebp), %eax
  97. addl $8, %eax
  98. movl (%eax), %edx
  99. movl $0, 8(%esp)
  100. leal -24(%ebp), %eax
  101. movl %eax, 4(%esp)
  102. movl %edx, (%esp)
  103. call strtoul@PLT
  104. movl %eax, -20(%ebp)
  105. movl -44(%ebp), %eax
  106. addl $8, %eax
  107. movl (%eax), %edx
  108. movl -24(%ebp), %eax
  109. cmpl %eax, %edx
  110. jae .L14
  111. movl -20(%ebp), %eax
  112. movl %eax, 4(%esp)
  113. movl -16(%ebp), %eax
  114. movl %eax, (%esp)
  115. call gcd
  116. movl %eax, -28(%ebp)
  117. movl -28(%ebp), %eax
  118. movl %eax, 4(%esp)
  119. leal .LC2@GOTOFF(%ebx), %eax
  120. movl %eax, (%esp)
  121. call printf@PLT
  122. movl -20(%ebp), %edx
  123. movl %edx, %eax
  124. sarl $31, %edx
  125. idivl -28(%ebp)
  126. movl %eax, %ecx
  127. movl -16(%ebp), %edx
  128. movl %edx, %eax
  129. sarl $31, %edx
  130. idivl -28(%ebp)
  131. movl %ecx, 16(%esp)
  132. movl %eax, 12(%esp)
  133. movl -20(%ebp), %eax
  134. movl %eax, 8(%esp)
  135. movl -16(%ebp), %eax
  136. movl %eax, 4(%esp)
  137. leal .LC3@GOTOFF(%ebx), %eax
  138. movl %eax, (%esp)
  139. call printf@PLT
  140. movl $0, -48(%ebp)
  141. jmp .L11
  142. .L14:
  143. movl -44(%ebp), %eax
  144. addl $8, %eax
  145. movl (%eax), %eax
  146. movl %eax, 4(%esp)
  147. leal .LC4@GOTOFF(%ebx), %eax
  148. movl %eax, (%esp)
  149. call printf@PLT
  150. jmp .L16
  151. .L12:
  152. movl -44(%ebp), %eax
  153. addl $4, %eax
  154. movl (%eax), %eax
  155. movl %eax, 4(%esp)
  156. leal .LC4@GOTOFF(%ebx), %eax
  157. movl %eax, (%esp)
  158. call printf@PLT
  159. .L16:
  160. movl $1, -48(%ebp)
  161. .L11:
  162. movl -48(%ebp), %eax
  163. movl -12(%ebp), %edx
  164. xorl __guard_local@GOTOFF(%ebx), %edx
  165. je .L18
  166. leal .LSSH0@GOTOFF(%ebx), %eax
  167. movl %eax, (%esp)
  168. call __stack_smash_handler@PLT
  169. .L18:
  170. addl $64, %esp
  171. popl %ecx
  172. popl %ebx
  173. popl %ebp
  174. leal -4(%ecx), %esp
  175. ret
  176. .size main, .-main
  177. .section .gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
  178. .globl __i686.get_pc_thunk.bx
  179. .hidden __i686.get_pc_thunk.bx
  180. .type __i686.get_pc_thunk.bx, @function
  181. __i686.get_pc_thunk.bx:
  182. movl (%esp), %ebx
  183. ret
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト