취업,면접 대비

다양한 컴파일 결과를 볼 수 있는 사이트

studying develop 2020. 1. 9. 15:32

https://godbolt.org/

 

Compiler Explorer

 

godbolt.org

 

#include <cstddef>
#include<cstdio>

void function(size_t len)
{
int arr[len];
for(size_t i = 0; i < len; ++i)
{
arr[i] = i;
printf("%d\n", arr[i]);
}
}

 

.LC0:
        .string "%d\n"
function:
        push    rbp
        mov     rbp, rsp
        push    rbx
        sub     rsp, 56
        mov     QWORD PTR [rbp-56], rdi
        mov     rax, rsp
        mov     rbx, rax
        mov     rax, QWORD PTR [rbp-56]
        mov     rdx, rax
        sub     rdx, 1
        mov     QWORD PTR [rbp-32], rdx
        mov     r10, rax
        mov     r11d, 0
        mov     r8, rax
        mov     r9d, 0
        lea     rdx, [0+rax*4]
        mov     eax, 16
        sub     rax, 1
        add     rax, rdx
        mov     esi, 16
        mov     edx, 0
        div     rsi
        imul    rax, rax, 16
        sub     rsp, rax
        mov     rax, rsp
        add     rax, 3
        shr     rax, 2
        sal     rax, 2
        mov     QWORD PTR [rbp-40], rax
        mov     QWORD PTR [rbp-24], 0
        jmp     .L2
.L3:
        mov     rax, QWORD PTR [rbp-24]
        mov     ecx, eax
        mov     rax, QWORD PTR [rbp-40]
        mov     rdx, QWORD PTR [rbp-24]
        mov     DWORD PTR [rax+rdx*4], ecx
        mov     rax, QWORD PTR [rbp-40]
        mov     rdx, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+rdx*4]
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC0
        mov     eax, 0
        call    printf
        add     QWORD PTR [rbp-24], 1
.L2:
        mov     rax, QWORD PTR [rbp-24]
        cmp     rax, QWORD PTR [rbp-56]
        jb      .L3
        mov     rsp, rbx
        nop
        mov     rbx, QWORD PTR [rbp-8]
        leave
        ret

c코드 결과 - x86-64 gcc 5.x

 

 

반면에 x86-64 gcc 4.x로 하면 아래 처럼 뜬다.

 

<source>: In function 'function':

<source>:6:1: error: 'for' loop initial declarations are only allowed in C99 or C11 mode

for(size_t i = 0; i < len; ++i)

^

<source>:6:1: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code

Compiler returned: 1

 

 

 

 

 

 

 

 

 

.LC0:
        .string "%d\n"
function(unsigned long):
        push    rbp
        mov     rbp, rsp
        push    rbx
        sub     rsp, 56
        mov     QWORD PTR [rbp-56], rdi
        mov     rax, rsp
        mov     rbx, rax
        mov     rax, QWORD PTR [rbp-56]
        sub     rax, 1
        mov     QWORD PTR [rbp-32], rax
        mov     rdx, rax
        add     rdx, 1
        mov     r10, rdx
        mov     r11d, 0
        mov     rdx, rax
        add     rdx, 1
        mov     r8, rdx
        mov     r9d, 0
        add     rax, 1
        lea     rdx, [0+rax*4]
        mov     eax, 16
        sub     rax, 1
        add     rax, rdx
        mov     esi, 16
        mov     edx, 0
        div     rsi
        imul    rax, rax, 16
        sub     rsp, rax
        mov     rax, rsp
        add     rax, 3
        shr     rax, 2
        sal     rax, 2
        mov     QWORD PTR [rbp-40], rax
        mov     QWORD PTR [rbp-24], 0
.L3:
        mov     rax, QWORD PTR [rbp-24]
        cmp     rax, QWORD PTR [rbp-56]
        jnb     .L2
        mov     rax, QWORD PTR [rbp-24]
        mov     ecx, eax
        mov     rax, QWORD PTR [rbp-40]
        mov     rdx, QWORD PTR [rbp-24]
        mov     DWORD PTR [rax+rdx*4], ecx
        mov     rax, QWORD PTR [rbp-40]
        mov     rdx, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+rdx*4]
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC0
        mov     eax, 0
        call    printf
        add     QWORD PTR [rbp-24], 1
        jmp     .L3
.L2:
        mov     rsp, rbx
        nop
        mov     rbx, QWORD PTR [rbp-8]
        leave
        ret

c++ 코드 결과