美文网首页
C语言 hello.c 的汇编解释

C语言 hello.c 的汇编解释

作者: 暴风鱼 | 来源:发表于2017-11-16 14:37 被阅读99次

    下面是著名的hello world!程序,此文将逐行解释c程序使用gcc 编译生成的汇编的 汇编程序。

    hello.c

    #include <stdio.h>
        
    int main(void){
        printf("hello.\n");
        return 0;
    }
    

    编译生成汇编文件hello.s

    gcc -S hello.c

        .file   "hello.c"
        .section    .rodata
    .LC0:
        .string "hello."
        .text
        .globl  main
        .type   main, @function
    main:
    .LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        leaq    .LC0(%rip), %rdi
        call    puts@PLT
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE0:
        .size   main, .-main
        .ident  "GCC: (Ubuntu 7.2.0-8ubuntu3) 7.2.0"
        .section    .note.GNU-stack,"",@progbits
    
    

    解释 line by line

    .file "hello.c"
    # 汇编来源的原始文件名

    .section .rodata
    # 定义一个data section,里面的数据是ro(read only的)

    .LC0:
    # .LC0 local constant, e.g string literal.

    .string "hello"

    .text
    # 代码开始

    .globl main
    # global makes the symbol visible to ld.
    # 定义一个全局可见的symbol
    # We define a function called "main" and
    # globally visible (other object files will be able to invoke it).

    .type main, @function
    # set the type of a symbol.
    # 定义symbol main 的类型为一个function

    .LFB0:
    # .LFB0 local function beginning

    pushq %rbp
    movq %rsp, %rbp
    leaq .LC0(%rip), %rdi
    call puts@PLT
    movl $0, %eax
    popq %rbp

    ret
    # funtion exit

    .LFE0:
    # .LFE0 local function ending,

    .size main, .-main
    # set the size associated with a symbol
    # 设置symbol main 的大小
    # “.-main” : 从当前位置到main

    .ident "GCC: (Ubuntu 7.2.0-8ubuntu3) 7.2.0"
    # 备注编译器版本

    .section .note.GNU-stack,"",@progbits
    # 意思是这个segment会被标记为 “非可执行栈”(栈中不能存可执行的指令,只存数据)
    # With this section, the executable will be marked as
    # "compatible with a non-executable stack" which the kernel
    # will happily provide as such.
    # A special section where GCC writes that the code can
    # accommodate a non-executable stack. This is the normal case.
    # Executable stacks are needed for some special usages (not standard C).
    # On modern processors, the kernel can make a non-executable stack
    # (a stack which triggers an exception if someone tries to execute
    # as code some data which is on the stack); this is viewed by
    # some people as a "security feature" because putting code on the stack
    # is a common way to exploit buffer overflows.

    参考链接:https://stackoverflow.com/questions/5325326/what-is-the-meaning-of-each-line-of-the-assembly-output-of-a-c-hello-world



    END.

    相关文章

      网友评论

          本文标题:C语言 hello.c 的汇编解释

          本文链接:https://www.haomeiwen.com/subject/szmlvxtx.html