c 语法编译结果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/for_cxc/article/details/86715296

==================================== char 类型
void main(void){
    char a,b,c;
    a = 34;
    b = 126;
    c = a + b;
}
----------------------------
    movb    $34, -3(%ebp)
    movb    $17, -2(%ebp)
    movzbl    -3(%ebp), %edx
    movzbl    -2(%ebp), %eax
    addl    %edx, %eax
    movb    %al, -1(%ebp)         //如果超出char的范围这里会数据丢失

==================================== short 类型
void main(void){
    short a,b,c;
    a = 34;
    b = 126;
    c = a + b;
}
----------------------------
    movw    $34, -6(%ebp)
    movw    $126, -4(%ebp)
    movzwl    -6(%ebp), %edx
    movzwl    -4(%ebp), %eax
    addl    %edx, %eax
    movw    %ax, -2(%ebp)

==================================== int、unsigned int、long 类型
void main(void){
    int a = 34;
    unsigned int b = 126;
    unsigned int c = a + b;
}
----------------------------
    movl    $34, -12(%ebp)
    movl    $126, -8(%ebp)
    movl    -12(%ebp), %edx
    movl    -8(%ebp), %eax
    addl    %edx, %eax
    movl    %eax, -4(%ebp)

==================================== float
void main(void){
    float a = 34.12;
    float b = 126.3;
    float c = a + b;
}
----------------------------
    movl    .LC0, %eax
    movl    %eax, -12(%ebp)
    movl    .LC1, %eax
    movl    %eax, -8(%ebp)
    flds    -12(%ebp)
    fadds    -8(%ebp)
    fstps    -4(%ebp)

==================================== 常量
#define PAI 8;
void main(void){
    const int a = 3;
    const int b = 7;
    int c = a + b + PAI;
}
----------------------------
    movl    $3, -12(%ebp)
    movl    $7, -8(%ebp)
    movl    -8(%ebp), %eax
    movl    -12(%ebp), %edx
    addl    %edx, %eax
    addl    $8, %eax
    movl    %eax, -4(%ebp)

==================================== if else
void main(void){
    int a = 5;
    int b = 3;
    int c;
    if(a>b){
        c = 1;
    }
    else if(a==b){
        c = 0;
    }
    else{
        c = -1;
    }
}
----------------------------
    movl    $5, -12(%ebp)
    movl    $3, -8(%ebp)
    movl    -12(%ebp), %eax
    cmpl    -8(%ebp), %eax
    jle    .L2
    movl    $1, -4(%ebp)
    jmp    .L1
.L2:
    movl    -12(%ebp), %eax
    cmpl    -8(%ebp), %eax
    jne    .L4
    movl    $0, -4(%ebp)
    jmp    .L1
.L4:
    movl    $-1, -4(%ebp)
.L1:
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== switch case
void main(void){
    int a = 5;
    int b;
    switch(a){
        case 4:
            b = -1;
            break;
        case 5:
            b = 0;
    }
}
----------------------------
    movl    $5, -8(%ebp)
    movl    -8(%ebp), %eax
    cmpl    $4, %eax
    je    .L3
    cmpl    $5, %eax
    je    .L4
    jmp    .L1
.L3:
    movl    $-1, -4(%ebp)
    jmp    .L1
.L4:
    movl    $0, -4(%ebp)
.L1:
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== while
void main(void){
    int a = 10;
    int b = 1;
    while(a>b){
       a--;
       b++;
    }
    int c = a + b;
}
----------------------------
.LFB0:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $10, -12(%ebp)
    movl    $1, -8(%ebp)
    jmp    .L2
.L3:
    subl    $1, -12(%ebp)
    addl    $1, -8(%ebp)
.L2:
    movl    -12(%ebp), %eax
    cmpl    -8(%ebp), %eax
    jg    .L3
    movl    -8(%ebp), %eax
    movl    -12(%ebp), %edx
    addl    %edx, %eax
    movl    %eax, -4(%ebp)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== for
void main(void){
    int a = 10;
    int b = 1;
    for(b;b<10;b++){
        a++;
    }
    int c = a + b;
}
----------------------------
.LFB0:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $10, -12(%ebp)
    movl    $1, -8(%ebp)
    jmp    .L2
.L3:
    addl    $1, -12(%ebp)
    addl    $1, -8(%ebp)
.L2:
    cmpl    $9, -8(%ebp)
    jle    .L3
    movl    -8(%ebp), %eax
    movl    -12(%ebp), %edx
    addl    %edx, %eax
    movl    %eax, -4(%ebp)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== 子程序 无参数
int add(void){
    int a = 2,b = 3;
    int c = a + b;
}

void main(void){
    int a = 10;
    int b = 1;
    int c = a + b;
    add();
}
----------------------------
add:
.LFB0:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $2, -12(%ebp)
    movl    $3, -8(%ebp)
    movl    -8(%ebp), %eax
    movl    -12(%ebp), %edx
    addl    %edx, %eax
    movl    %eax, -4(%ebp)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size    add, .-add
    .globl    main
    .type    main, @function
main:
.LFB1:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $10, -12(%ebp)
    movl    $1, -8(%ebp)
    movl    -8(%ebp), %eax
    movl    -12(%ebp), %edx
    addl    %edx, %eax
    movl    %eax, -4(%ebp)
    call    add
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== 子程序 值传递
void swap(int a,int b){
    int temp = a;
    a = b;
    b = temp;
    //return c;
}

void main(void){
    int a = 10;
    int b = 1;
    swap(a,b);
}
----------------------------
swap:
.LFB0:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    8(%ebp), %eax
    movl    %eax, -4(%ebp)
    movl    12(%ebp), %eax
    movl    %eax, 8(%ebp)
    movl    -4(%ebp), %eax
    movl    %eax, 12(%ebp)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size    swap, .-swap
    .globl    main
    .type    main, @function
main:
.LFB1:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $24, %esp
    movl    $10, -8(%ebp)
    movl    $1, -4(%ebp)
    movl    -4(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    -8(%ebp), %eax
    movl    %eax, (%esp)
    call    swap
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== 子程序 引用传递
void swap(int *a,int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

void main(void){
    int a = 10;
    int b = 1;
    swap(&a,&b);
}
----------------------------
swap:
.LFB0:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    8(%ebp), %eax
    movl    (%eax), %eax
    movl    %eax, -4(%ebp)
    movl    12(%ebp), %eax
    movl    (%eax), %edx
    movl    8(%ebp), %eax
    movl    %edx, (%eax)
    movl    12(%ebp), %eax
    movl    -4(%ebp), %edx
    movl    %edx, (%eax)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size    swap, .-swap
    .globl    main
    .type    main, @function
main:
.LFB1:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $24, %esp
    movl    $10, -8(%ebp)
    movl    $1, -4(%ebp)
    leal    -4(%ebp), %eax
    movl    %eax, 4(%esp)
    leal    -8(%ebp), %eax
    movl    %eax, (%esp)
    call    swap
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

==================================== /

----------------------------

==================================== /

----------------------------

==================================== /

----------------------------

==================================================================================================待测试
1、溢出问题

猜你喜欢

转载自blog.csdn.net/for_cxc/article/details/86715296