【发布时间】:2020-04-09 06:55:01
【问题描述】:
我试图将以下 C 代码转换为程序集。这是C代码:
typedef struct {
int x;
int y;
} point;
int square_distance( point * p ) {
return p->x * p->x + p->y * p->y;
}
我的汇编代码如下:
square_distance:
.LFB23:
.cfi_startproc
movl (%edi), %edx
imull %edx, %edx
movl 4(%edi), %eax
imull %eax, %eax
addl %edx, %eax
ret
.cfi_endproc
当我尝试运行此程序时出现分段错误。有人可以解释为什么吗?谢谢!我将不胜感激!
【问题讨论】:
-
是什么让您认为
%edi指向p参数?看看 -
函数的第一个参数不是传入edi/rdi寄存器吗?
-
你在哪里找到这些信息的?
-
@Jabberwocky:另一种可能性是它被组装为 64 位代码,并使用 32 位寻址模式将 RDI 中的 64 位指针截断为 32 位。 (与地址大小覆盖前缀组合在一起。)
-
@Jabberwocky:如果没有 OP 解释他们如何将其构建到可执行文件中,我们不知道。尽管看起来他们复制/粘贴了编译器输出,但考虑到没有人会手写的
.LFB23:,以及初学者不会的 cfi 指令。因此,也许他们试图将 64 位编译器输出移植到 32 位,而不是仅使用-m32进行编译。我觉得没有必要再写一个 x86 调用约定指南来回答这个非minimal reproducible example 的基础; Agner Fog 已经这样做了:agner.org/optimize/calling_conventions.pdf
标签: c assembly segmentation-fault x86-64 calling-convention