YaK:: x86 & amd64 example, compiled to assembly (gcc) [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

x86 & amd64 example, compiled to assembly (gcc)

$ cat fib.c

#include <stdio.h>   // printf
#include <stdlib.h>  // atoi

int fib(int n) {
  if (n < 2) return 1;
  return fib(n-1) + fib(n-2);
}

int main(int argc, char *argv[]) {
  int i;
  for (i = 1; i < argc; ++i) {
    int x = atoi(argv[i]);
    int z = fib(x);
    printf("%d -> %d\n", x, z);
  }
  return 23;
}

$ cc -S -march=i386 fib.c

strick@uhuru:~/work$ cat fib.s
        .file        "fib.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %ebx
        subl        $4, %esp
        cmpl        $1, 8(%ebp)
        jg        .L2
        movl        $1, %eax
        jmp        .L3
.L2:
        movl        8(%ebp), %eax
        decl        %eax
        subl        $12, %esp
        pushl        %eax
        call        fib
        addl        $16, %esp
        movl        %eax, %ebx
        movl        8(%ebp), %eax
        subl        $2, %eax
        subl        $12, %esp
        pushl        %eax
        call        fib
        addl        $16, %esp
        leal        (%ebx,%eax), %eax
.L3:
        movl        -4(%ebp), %ebx
        leave
        ret
        .size        fib, .-fib
        .section        .rodata
.LC0:
        .string        "%d -> %d\n"
        .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        $16, %esp
        movl        %ecx, %ebx
        movl        $1, -12(%ebp)
        jmp        .L6
.L7:
        movl        -12(%ebp), %eax
        sall        $2, %eax
        addl        4(%ebx), %eax
        movl        (%eax), %eax
        subl        $12, %esp
        pushl        %eax
        call        atoi
        addl        $16, %esp
        movl        %eax, -16(%ebp)
        subl        $12, %esp
        pushl        -16(%ebp)
        call        fib
        addl        $16, %esp
        movl        %eax, -20(%ebp)
        movl        $.LC0, %eax
        subl        $4, %esp
        pushl        -20(%ebp)
        pushl        -16(%ebp)
        pushl        %eax
        call        printf
        addl        $16, %esp
        incl        -12(%ebp)
.L6:
        movl        -12(%ebp), %eax
        cmpl        (%ebx), %eax
        jl        .L7
        movl        $23, %eax
        leal        -8(%ebp), %esp
        addl        $0, %esp
        popl        %ecx
        popl        %ebx
        leave
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

        .file        "fib.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %ebx
        subl        $20, %esp
        cmpl        $1, 8(%ebp)
        jg        .L2
        movl        $1, %eax
        jmp        .L3
.L2:
        movl        8(%ebp), %eax
        subl        $1, %eax
        movl        %eax, (%esp)
        call        fib
        movl        %eax, %ebx
        movl        8(%ebp), %eax
        subl        $2, %eax
        movl        %eax, (%esp)
        call        fib
        leal        (%ebx,%eax), %eax
.L3:
        addl        $20, %esp
        popl        %ebx
        popl        %ebp
        ret
        .size        fib, .-fib
        .section        .rodata
.LC0:
        .string        "%d -> %d\n"
        .text
.globl main
        .type        main, @function
main:
        pushl        %ebp
        movl        %esp, %ebp
        andl        $-16, %esp
        subl        $32, %esp
        movl        $1, 28(%esp)
        jmp        .L6
.L7:
        movl        28(%esp), %eax
        sall        $2, %eax
        addl        12(%ebp), %eax
        movl        (%eax), %eax
        movl        %eax, (%esp)
        call        atoi
        movl        %eax, 24(%esp)
        movl        24(%esp), %eax
        movl        %eax, (%esp)
        call        fib
        movl        %eax, 20(%esp)
        movl        $.LC0, %eax
        movl        20(%esp), %edx
        movl        %edx, 8(%esp)
        movl        24(%esp), %edx
        movl        %edx, 4(%esp)
        movl        %eax, (%esp)
        call        printf
        addl        $1, 28(%esp)
.L6:
        movl        28(%esp), %eax
        cmpl        8(%ebp), %eax
        jl        .L7
        movl        $23, %eax
        leave
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

cc -S -march=athlon64 fib.c

        .file        "fib.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %ebx
        subl        $20, %esp
        cmpl        $1, 8(%ebp)
        jg        .L2
        movl        $1, %eax
        jmp        .L3
.L2:
        movl        8(%ebp), %eax
        decl        %eax
        movl        %eax, (%esp)
        call        fib
        movl        %eax, %ebx
        movl        8(%ebp), %eax
        subl        $2, %eax
        movl        %eax, (%esp)
        call        fib
        leal        (%ebx,%eax), %eax
.L3:
        addl        $20, %esp
        popl        %ebx
        leave
        ret
        .size        fib, .-fib
        .section        .rodata
.LC0:
        .string        "%d -> %d\n"
        .text
.globl main
        .type        main, @function
main:
        pushl        %ebp
        movl        %esp, %ebp
        andl        $-16, %esp
        subl        $32, %esp
        movl        $1, 28(%esp)
        jmp        .L6
.L7:
        movl        28(%esp), %eax
        sall        $2, %eax
        addl        12(%ebp), %eax
        movl        (%eax), %eax
        movl        %eax, (%esp)
        call        atoi
        movl        %eax, 24(%esp)
        movl        24(%esp), %eax
        movl        %eax, (%esp)
        call        fib
        movl        %eax, 20(%esp)
        movl        $.LC0, %eax
        movl        20(%esp), %edx
        movl        %edx, 8(%esp)
        movl        24(%esp), %edx
        movl        %edx, 4(%esp)
        movl        %eax, (%esp)
        call        printf
        incl        28(%esp)
.L6:
        movl        28(%esp), %eax
        cmpl        8(%ebp), %eax
        jl        .L7
        movl        $23, %eax
        leave
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

cc -S -march=i686 -Os fib.c

        .file        "fib.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %esi
        xorl        %esi, %esi
        pushl        %ebx
        movl        8(%ebp), %ebx
.L3:
        cmpl        $1, %ebx
        jle        .L2
        subl        $12, %esp
        leal        -1(%ebx), %eax
        subl        $2, %ebx
        pushl        %eax
        call        fib
        addl        $16, %esp
        addl        %eax, %esi
        jmp        .L3
.L2:
        leal        -8(%ebp), %esp
        leal        1(%esi), %eax
        popl        %ebx
        popl        %esi
        popl        %ebp
        ret
        .size        fib, .-fib
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string        "%d -> %d\n"
        .text
.globl main
        .type        main, @function
main:
        leal        4(%esp), %ecx
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %edi
        pushl        %esi
        pushl        %ebx
        movl        $1, %ebx
        pushl        %ecx
        subl        $24, %esp
        movl        (%ecx), %eax
        movl        4(%ecx), %edi
        movl        %eax, -28(%ebp)
        jmp        .L7
.L8:
        subl        $12, %esp
        pushl        (%edi,%ebx,4)
        incl        %ebx
        call        atoi
        movl        %eax, (%esp)
        movl        %eax, %esi
        call        fib
        pushl        %eax
        pushl        %esi
        pushl        $.LC0
        pushl        $1
        call        __printf_chk
        addl        $32, %esp
.L7:
        cmpl        -28(%ebp), %ebx
        jl        .L8
        leal        -16(%ebp), %esp
        movl        $23, %eax
        popl        %ecx
        popl        %ebx
        popl        %esi
        popl        %edi
        popl        %ebp
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

cc -S -march=athlon64 -Os fib.c

        .file        "fib.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %esi
        xorl        %esi, %esi
        pushl        %ebx
        movl        8(%ebp), %ebx
.L3:
        cmpl        $1, %ebx
        jle        .L2
        leal        -1(%ebx), %eax
        subl        $12, %esp
        subl        $2, %ebx
        pushl        %eax
        call        fib
        addl        $16, %esp
        addl        %eax, %esi
        jmp        .L3
.L2:
        leal        -8(%ebp), %esp
        leal        1(%esi), %eax
        popl        %ebx
        popl        %esi
        leave
        ret
        .size        fib, .-fib
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string        "%d -> %d\n"
        .text
.globl main
        .type        main, @function
main:
        leal        4(%esp), %ecx
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %edi
        pushl        %esi
        pushl        %ebx
        movl        $1, %ebx
        pushl        %ecx
        subl        $24, %esp
        movl        (%ecx), %eax
        movl        4(%ecx), %edi
        movl        %eax, -28(%ebp)
        jmp        .L7
.L8:
        subl        $12, %esp
        pushl        (%edi,%ebx,4)
        incl        %ebx
        call        atoi
        movl        %eax, (%esp)
        movl        %eax, %esi
        call        fib
        pushl        %eax
        pushl        %esi
        pushl        $.LC0
        pushl        $1
        call        __printf_chk
        addl        $32, %esp
.L7:
        cmpl        -28(%ebp), %ebx
        jl        .L8
        leal        -16(%ebp), %esp
        movl        $23, %eax
        popl        %ecx
        popl        %ebx
        popl        %esi
        popl        %edi
        leave
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

cat fib64.c

#include <stdio.h>   // printf
#include <stdlib.h>  // atoi
#include <sys/types.h>  // int64_t

int64_t fib(int64_t n) {
  if (n < 2) return 1;
  return fib(n-1) + fib(n-2);
}

int main(int argc, char *argv[]) {
  int i;
  for (i = 1; i < argc; ++i) {
    int64_t x = atoi(argv[i]);
    int64_t z = fib(x);
    printf("%lld -> %lld\n", x, z);
  }
  return 23;
}

cc -S -march=athlon64 -Os fib64.c

        .file        "fib64.c"
        .text
.globl fib
        .type        fib, @function
fib:
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %esi
        pushl        %ebx
        subl        $16, %esp
        movl        8(%ebp), %ebx
        movl        12(%ebp), %esi
        movl        $0, -16(%ebp)
        movl        $0, -12(%ebp)
.L4:
        cmpl        $0, %esi
        jl        .L2
        jg        .L6
        cmpl        $1, %ebx
        jbe        .L2
.L6:
        pushl        %eax
        movl        %esi, %edx
        pushl        %eax
        movl        %ebx, %eax
        addl        $-1, %eax
        adcl        $-1, %edx
        pushl        %edx
        pushl        %eax
        call        fib
        addl        $-2, %ebx
        adcl        $-1, %esi
        addl        %eax, -16(%ebp)
        adcl        %edx, -12(%ebp)
        addl        $16, %esp
        jmp        .L4
.L2:
        movl        -16(%ebp), %eax
        movl        -12(%ebp), %edx
        addl        $1, %eax
        adcl        $0, %edx
        leal        -8(%ebp), %esp
        popl        %ebx
        popl        %esi
        leave
        ret
        .size        fib, .-fib
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string        "%lld -> %lld\n"
        .text
.globl main
        .type        main, @function
main:
        leal        4(%esp), %ecx
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ebp
        movl        %esp, %ebp
        pushl        %edi
        movl        $1, %edi
        pushl        %esi
        pushl        %ebx
        pushl        %ecx
        subl        $24, %esp
        movl        (%ecx), %eax
        movl        4(%ecx), %ecx
        movl        %eax, -28(%ebp)
        jmp        .L9
.L10:
        subl        $12, %esp
        pushl        (%ecx,%edi,4)
        movl        %ecx, -32(%ebp)
        incl        %edi
        call        atoi
        popl        %ebx
        popl        %esi
        movl        %eax, %esi
        movl        %eax, %ebx
        sarl        $31, %esi
        pushl        %esi
        pushl        %eax
        call        fib
        popl        %ecx
        popl        %ecx
        pushl        %edx
        pushl        %eax
        pushl        %esi
        pushl        %ebx
        pushl        $.LC0
        pushl        $1
        call        __printf_chk
        movl        -32(%ebp), %ecx
        addl        $32, %esp
.L9:
        cmpl        -28(%ebp), %edi
        jl        .L10
        leal        -16(%ebp), %esp
        movl        $23, %eax
        popl        %ecx
        popl        %ebx
        popl        %esi
        popl        %edi
        leave
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

$ cc -S -march=athlon64 -Os -m64 fib64.c

        .file        "fib64.c"
        .text
.globl fib
        .type        fib, @function
fib:
.LFB17:
        .cfi_startproc
        pushq        %rbp
        .cfi_def_cfa_offset 16
        xorl        %ebp, %ebp
        .cfi_offset 6, -16
        pushq        %rbx
        .cfi_def_cfa_offset 24
        movq        %rdi, %rbx
        .cfi_offset 3, -24
        subq        $8, %rsp
        .cfi_def_cfa_offset 32
.L3:
        cmpq        $1, %rbx
        jle        .L2
        leaq        -1(%rbx), %rdi
        subq        $2, %rbx
        call        fib
        addq        %rax, %rbp
        jmp        .L3
.L2:
        popq        %rdx
        popq        %rbx
        leaq        1(%rbp), %rax
        popq        %rbp
        ret
        .cfi_endproc
.LFE17:
        .size        fib, .-fib
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string        "%ld -> %ld\n"
        .text
.globl main
        .type        main, @function
main:
.LFB18:
        .cfi_startproc
        pushq        %r13
        .cfi_def_cfa_offset 16
        pushq        %r12
        .cfi_def_cfa_offset 24
        movl        %edi, %r12d
        .cfi_offset 12, -24
        .cfi_offset 13, -16
        pushq        %rbp
        .cfi_def_cfa_offset 32
        movq        %rsi, %rbp
        .cfi_offset 6, -32
        pushq        %rbx
        .cfi_def_cfa_offset 40
        movl        $1, %ebx
        .cfi_offset 3, -40
        subq        $8, %rsp
        .cfi_def_cfa_offset 48
        jmp        .L7
.L8:
        movq        (%rbp), %rdi
        incl        %ebx
        call        atoi
        movslq        %eax,%r13
        movq        %r13, %rdi
        call        fib
        movq        %r13, %rdx
        movq        %rax, %rcx
        movl        $.LC0, %esi
        movl        $1, %edi
        xorl        %eax, %eax
        call        __printf_chk
.L7:
        addq        $8, %rbp
        cmpl        %r12d, %ebx
        jl        .L8
        popq        %rcx
        popq        %rbx
        popq        %rbp
        popq        %r12
        movl        $23, %eax
        popq        %r13
        ret
        .cfi_endproc
.LFE18:
        .size        main, .-main
        .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

   0x400584 <fib>:    55    push   %rbp
   0x400585 <fib+1>:  31 ed      xor    %ebp,%ebp
   0x400587 <fib+3>:  53      push   %rbx
   0x400588 <fib+4>:  48 89 fb      mov    %rdi,%rbx
   0x40058b <fib+7>:  48 83 ec 08      sub    $0x8,%rsp
   0x40058f <fib+11>: 48 83 fb 01       cmp    $0x1,%rbx
   0x400593 <fib+15>: 7e 12       jle    0x4005a7 <fib+35>
   0x400595 <fib+17>: 48 8d 7b ff       lea    -0x1(%rbx),%rdi
   0x400599 <fib+21>: 48 83 eb 02       sub    $0x2,%rbx
   0x40059d <fib+25>: e8 82 ff ff ff       callq  0x400584 <fib>
   0x4005a2 <fib+30>: 48 01 c5       add    %rax,%rbp
   0x4005a5 <fib+33>: eb e8       jmp    0x40058f <fib+11>
   0x4005a7 <fib+35>: 5a       pop    %rdx
   0x4005a8 <fib+36>: 5b       pop    %rbx
   0x4005a9 <fib+37>: 48 8d 45 01       lea    0x1(%rbp),%rax
   0x4005ad <fib+41>: 5d       pop    %rbp
   0x4005ae <fib+42>: c3       retq

=> 0x4005af <main>:   41     push   %r13
   0x4005b1 <main+2>:        push   %r12
   0x4005b3 <main+4>:        mov    %edi,%r12d
   0x4005b6 <main+7>:        push   %rbp
   0x4005b7 <main+8>:        mov    %rsi,%rbp
   0x4005ba <main+11>:        push   %rbx
   0x4005bb <main+12>:        mov    $0x1,%ebx
   0x4005c0 <main+17>:        sub    $0x8,%rsp
   0x4005c4 <main+21>:        jmp    0x4005f3 <main+68>
   0x4005c6 <main+23>:        mov    0x0(%rbp),%rdi
   0x4005ca <main+27>:        inc    %ebx
   0x4005cc <main+29>:        callq  0x400490 <atoi@plt>
   0x4005d1 <main+34>:        movslq %eax,%r13
   0x4005d4 <main+37>:        mov    %r13,%rdi
   0x4005d7 <main+40>:        callq  0x400584 <fib>
   0x4005dc <main+45>:        mov    %r13,%rdx
   0x4005df <main+48>:        mov    %rax,%rcx
   0x4005e2 <main+51>:        mov    $0x4006fc,%esi
   0x4005e7 <main+56>:        mov    $0x1,%edi
   0x4005ec <main+61>:        xor    %eax,%eax
   0x4005ee <main+63>:        callq  0x400470 <__printf_chk@plt>
   0x4005f3 <main+68>:        add    $0x8,%rbp
   0x4005f7 <main+72>:        cmp    %r12d,%ebx
   0x4005fa <main+75>:        jl     0x4005c6 <main+23>
   0x4005fc <main+77>:        pop    %rcx
   0x4005fd <main+78>:        pop    %rbx
   0x4005fe <main+79>:        pop    %rbp
   0x4005ff <main+80>:        pop    %r12
   0x400601 <main+82>:        mov    $0x17,%eax
   0x400606 <main+87>:        pop    %r13
   0x400608 <main+89>:        retq

0x400584 <fib>:                0x53ed3155
0x400588 <fib+4>:        0x48fb8948
0x40058c <fib+8>:        0x4808ec83
0x400590 <fib+12>:        0x7e01fb83
0x400594 <fib+16>:        0x7b8d4812
0x400598 <fib+20>:        0xeb8348ff
0x40059c <fib+24>:        0xffe2e802
0x4005a0 <fib+28>:        0x0148ffff
0x4005a4 <fib+32>:        0x5ae8ebc5
0x4005a8 <fib+36>:        0x458d485b
0x4005ac <fib+40>:        0x41c35d01
0x4005b0 <main+1>:        0x41544155
0x4005b4 <main+5>:        0x4855fc89
0x4005b8 <main+9>:        0xbb53f589
0x4005bc <main+13>:        0x00000001
0x4005c0 <main+17>:        0x08ec8348
0x4005c4 <main+21>:        0x8b482deb
0x4005c8 <main+25>:        0xc3ff007d
0x4005cc <main+29>:        0xfffebfe8
0x4005d0 <main+33>:        0xe8634cff
0x4005d4 <main+37>:        0xe8ef894c
0x4005d8 <main+41>:        0xffffffa8
0x4005dc <main+45>:        0x48ea894c
0x4005e0 <main+49>:        0xfcbec189
0x4005e4 <main+53>:        0xbf004006
0x4005e8 <main+57>:        0x00000001
0x4005ec <main+61>:        0x7de8c031
0x4005f0 <main+65>:        0x48fffffe
0x4005f4 <main+69>:        0x4408c583
0x4005f8 <main+73>:        0xca7ce339
0x4005fc <main+77>:        0x415d5b59
0x400600 <main+81>:        0x0017b85c
0x400604 <main+85>:        0x5d410000
0x400608 <main+89>:        0x909090c3
0x40060c:                0x90909090

(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2011-06-02)       [Login]
(No back references.)