gpt4 book ai didi

c - 推送堆栈位置的地址而不是地址处的值

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:57 25 4
gpt4 key购买 nike

我正在处理一个小型编译器项目,我似乎无法弄清楚如何推送堆栈位置的地址而不是该堆栈位置的值。我的目标是推送一个堆栈位置地址,它包含一个整数值,作为指向打印它的 C 函数的空指针。我的最终目标是在函数中做一些指针整数运算。我成功地从运行时库扩展调用了 C 函数,但问题只是弄清楚如何在程序集中推送地址。

我的 C 函数。

void print(void* ptr){
int* int_ptr = (int*)ptr;
printf("*int_ptr is %d\n",*int_ptr);
}

我的程序集

.globl main
main:
pushl %ebp
movl %esp, %ebp
subl $4, %esp

movl $42, %eax
movl %eax, -4(%ebp)

pushl -4(%ebp)
//Instead of the value at -4(%ebp), I would like the address of -4(%ebp)
call print

addl $8, %esp
leave
ret

至于我现在拥有的东西,它会崩溃,因为我将值 42 转换为一个地址。有人可以指导我引用或资源以了解更多信息吗?

最佳答案

通常,您可以使用 LEA 获取基于堆栈的值的地址。获取 -4(%ebp) 的有效地址并将其放入寄存器的指令。然后您可以将该寄存器压入堆栈。 LEA 指令在 instruction set reference 中描述。这样:

Computes the effective address of the second operand (the source operand) and stores it in the first operand (destination operand). The source operand is a memory address (offset part) specified with one of the processors addressing modes; the destination operand is a general-purpose register.

在你的代码中,这样的事情会起作用:

lea -4(%ebp), %eax
push %eax

这应该有效地在堆栈上传递 -4(%ebp) 的地址,供您的函数 print 使用。

关于c - 推送堆栈位置的地址而不是地址处的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732748/

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