gpt4 book ai didi

c - 了解 x86-64 汇编代码中的指针分配

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:43 24 4
gpt4 key购买 nike

我正在尝试理解汇编代码。我卡在分配指针的部分和 leaq 命令之后的代码

这是我的 C 代码:

#include <stdio.h>
#include<stdlib.h>

int main(){
int x=50;
int *y=&x;
return 0;
}

这是我对应的汇编代码:

.file   "AssemlyCode.c"
.def __main; .scl 2; .type 32; .endef
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $48, %rsp
.seh_stackalloc 48
.seh_endprologue
call __main
movl $50, -12(%rbp)
leaq -12(%rbp), %rax
movq %rax, -8(%rbp)
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (GNU) 5.4.0"

最佳答案

    leaq    -8(%rbp), %rax
movl %eax, -4(%rbp)
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
  1. leaq 将变量x 的地址保存在栈上以注册rax。变量 x 是堆栈上的自动变量,因此它的地址计算为保存堆栈帧指针 (rbp) 的寄存器的偏移量。

  2. movl eax 到堆栈将 argc 参数保存到堆栈。

  3. 下一步是将main函数的返回值放入eax寄存器(返回0)

  4. 接下来的两个操作码是函数尾声 - 您正在清理使用过的堆栈并恢复之前的帧指针寄存器。

  5. 最后一条指令是简单的返回。

关于c - 了解 x86-64 汇编代码中的指针分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40881528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com