小编Ghj*_*hdf的帖子

为什么编译器不使用“sub*”和“add*”在堆栈上分配和释放局部变量?

根据一些教科书,编译器将使用sub*为局部变量分配内存。

比如我写了一个Hello World程序:

int main()
{
    puts("hello world");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想这将在 64 位操作系统上编译为一些汇编代码:

    subq    $8, %rsp
    movq    $.LC0, (%rsp)
    calq    puts
    addq    $8, %rsp
Run Code Online (Sandbox Code Playgroud)

所述subq分配8字节存储器(一个点的大小),用于该参数和addq解除分配它。

但是当我输入时gcc -S hello.c(我在 Mac OS X 10.8 上使用 llvm-gcc),我得到了一些汇编代码。

.section    __TEXT,__text,regular,pure_instructions
.globl  _main
.align  4, 0x90
_main:
Leh_func_begin1:
       pushq    %rbp
Ltmp0:
    movq    %rsp, %rbp
Ltmp1:
    subq    $16, %rsp
Ltmp2:
    xorb    %al, %al
    leaq    L_.str(%rip), %rcx
    movq    %rcx, %rdi
    callq   _puts
    movl    $0, -8(%rbp)
    movl    -8(%rbp), %eax …
Run Code Online (Sandbox Code Playgroud)

c compiler-construction macos assembly llvm-gcc

2
推荐指数
1
解决办法
1391
查看次数

标签 统计

assembly ×1

c ×1

compiler-construction ×1

llvm-gcc ×1

macos ×1