Archetypical Example: Euclid's Method of Computing Greatest Common Divisors (assembler) 超典型的ソースコードの見本~最大公約数を計算する、ユークリッドの互除法 (アセンブラ)
- .file "gcd.c"
- .text
- .globl gcd
- .type gcd, @function
- gcd:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- jmp .L2
- .L3:
- movl 8(%ebp), %eax
- cmpl 12(%ebp), %eax
- jge .L4
- movl 8(%ebp), %eax
- movl %eax, -8(%ebp)
- movl 12(%ebp), %eax
- movl %eax, 8(%ebp)
- movl -8(%ebp), %eax
- movl %eax, 12(%ebp)
- .L4:
- movl 12(%ebp), %eax
- subl %eax, 8(%ebp)
- .L2:
- movl 8(%ebp), %eax
- cmpl 12(%ebp), %eax
- jne .L3
- movl 8(%ebp), %eax
- leave
- ret
- .size gcd, .-gcd
- .section .rodata
- .LC0:
- .string "usage: %s <numA> <numB>\n"
- .align 4
- .LC1:
- .string "\tto calculate the greatest common divisor\n\tof two unsigned numbers."
- .LC2:
- .string "greatest common divisor: %ld\n"
- .LC3:
- .string "%ld/%ld => %ld/%ld\n"
- .align 4
- .LC4:
- .string "%s is not an unsigned number I can recognize.\n"
- .type .LSSH0, @object
- .size .LSSH0, 5
- .LSSH0:
- .string "main"
- .text
- .globl main
- .type main, @function
- main:
- leal 4(%esp), %ecx
- andl $-16, %esp
- pushl -4(%ecx)
- pushl %ebp
- movl %esp, %ebp
- pushl %ebx
- pushl %ecx
- subl $64, %esp
- call __i686.get_pc_thunk.bx
- addl $_GLOBAL_OFFSET_TABLE_, %ebx
- movl 4(%ecx), %eax
- movl %eax, -44(%ebp)
- movl __guard_local@GOTOFF(%ebx), %eax
- movl %eax, -12(%ebp)
- xorl %eax, %eax
- cmpl $3, (%ecx)
- je .L9
- movl -44(%ebp), %eax
- movl (%eax), %eax
- movl %eax, 4(%esp)
- leal .LC0@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call printf@PLT
- leal .LC1@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call puts@PLT
- movl $1, -48(%ebp)
- jmp .L11
- .L9:
- movl -44(%ebp), %eax
- addl $4, %eax
- movl (%eax), %edx
- movl $0, 8(%esp)
- leal -24(%ebp), %eax
- movl %eax, 4(%esp)
- movl %edx, (%esp)
- call strtoul@PLT
- movl %eax, -16(%ebp)
- movl -44(%ebp), %eax
- addl $4, %eax
- movl (%eax), %edx
- movl -24(%ebp), %eax
- cmpl %eax, %edx
- jae .L12
- movl -44(%ebp), %eax
- addl $8, %eax
- movl (%eax), %edx
- movl $0, 8(%esp)
- leal -24(%ebp), %eax
- movl %eax, 4(%esp)
- movl %edx, (%esp)
- call strtoul@PLT
- movl %eax, -20(%ebp)
- movl -44(%ebp), %eax
- addl $8, %eax
- movl (%eax), %edx
- movl -24(%ebp), %eax
- cmpl %eax, %edx
- jae .L14
- movl -20(%ebp), %eax
- movl %eax, 4(%esp)
- movl -16(%ebp), %eax
- movl %eax, (%esp)
- call gcd
- movl %eax, -28(%ebp)
- movl -28(%ebp), %eax
- movl %eax, 4(%esp)
- leal .LC2@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call printf@PLT
- movl -20(%ebp), %edx
- movl %edx, %eax
- sarl $31, %edx
- idivl -28(%ebp)
- movl %eax, %ecx
- movl -16(%ebp), %edx
- movl %edx, %eax
- sarl $31, %edx
- idivl -28(%ebp)
- movl %ecx, 16(%esp)
- movl %eax, 12(%esp)
- movl -20(%ebp), %eax
- movl %eax, 8(%esp)
- movl -16(%ebp), %eax
- movl %eax, 4(%esp)
- leal .LC3@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call printf@PLT
- movl $0, -48(%ebp)
- jmp .L11
- .L14:
- movl -44(%ebp), %eax
- addl $8, %eax
- movl (%eax), %eax
- movl %eax, 4(%esp)
- leal .LC4@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call printf@PLT
- jmp .L16
- .L12:
- movl -44(%ebp), %eax
- addl $4, %eax
- movl (%eax), %eax
- movl %eax, 4(%esp)
- leal .LC4@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call printf@PLT
- .L16:
- movl $1, -48(%ebp)
- .L11:
- movl -48(%ebp), %eax
- movl -12(%ebp), %edx
- xorl __guard_local@GOTOFF(%ebx), %edx
- je .L18
- leal .LSSH0@GOTOFF(%ebx), %eax
- movl %eax, (%esp)
- call __stack_smash_handler@PLT
- .L18:
- addl $64, %esp
- popl %ecx
- popl %ebx
- popl %ebp
- leal -4(%ecx), %esp
- ret
- .size main, .-main
- .section .gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
- .globl __i686.get_pc_thunk.bx
- .hidden __i686.get_pc_thunk.bx
- .type __i686.get_pc_thunk.bx, @function
- __i686.get_pc_thunk.bx:
- movl (%esp), %ebx
- ret