我需要将地址传递给汇编函数,但似乎我无法做到这一点。
这是 c 文件:
int asm_func(void *arg);
struct foo {
int len;
char *buf;
};
int bar(int size, char *buf){
struct foo arg_to_asm_function;
arg_to_asm_function.len = size;
arg_to_asm_function.buf = buf;
return asm_func(&arg_to_asm_function);
}
这是 assembly 体:
.global asm_func
asm_func:
pushl %esi
movl 8(%ebp), %esi
/* do something with &arg_to_asm_function, which is in esi */
popl %esi
ret
如果我使用参数 bar(5, "hello world") 调用 c 函数 bar,并且我单步执行指令
movl 8(%ebp), %esi
我在 %esi 中得到值 5(struct foo 中第一个字段的值)。
%esi 中的预期值是指向我声明的 struct foo 的指针,即 &arg_to_asm_function,而不是该地址内的值。
为什么会发生这种情况?编译器会自动为我取消引用指针吗?我如何将结构体的地址传递到 %esi 中?
最佳答案
您没有在汇编函数中设置堆栈帧,因此 8(%ebp)
不会为您提供正确的值。由于 ebp
仍然具有 C 函数中的值,因此您会看到传递给该函数的第一个参数的值。
您需要设置堆栈框架
push %ebp
mov %esp, %ebp
...
pop %ebp
这是假设调用约定在堆栈上传递函数参数 - 否则您需要从寄存器获取参数值。
关于c - 将 c 结构指针传递给 x86-32 程序集将自动取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28397060/