【发布时间】:2021-07-25 08:41:03
【问题描述】:
假设我定义了一个新的struct:
struct s {
int *x;
struct {
short sh[2];
int i;
} w;
struct s *next;
};
另外,我写了一个函数来初始化它:
void init_s(struct s *ss) {
ss->w.sh[1] = /* Line 1 */;
ss->x = /* Line 2 */;
ss->next = /* Line 3 */;
}
编译器为init_s生成以下汇编代码:
init_s: # line 1
movw 8(%rdi), %ax # line 2
movw %ax, 10(%rdi) # line 3
leaq 12(%rdi), %rax # line 4
movq %rax, (%rdi) # line 5
movq %rdi, 16(%rdi) # line 6
retq # line 7
我要做的是根据程序集为init_s 填写缺少的代码行。我已经弄清楚(或者至少我是这么认为的)第 1 行和第 2 行。第 1 行应该是 ss->w.sh[0],第 2 行应该是 &(ss->w.sh[2])。但是,我在第 3 行遇到问题。我认为它会是基于程序集的&(ss->x),但我觉得这是不正确的,我不知道为什么。非常感谢任何反馈或建议,以帮助我了解有关程序集和结构的更多信息。
【问题讨论】:
-
虽然我知道您的
init_s是一个带有占位符的示例,但也许您还应该显示用于生成程序集的实际编译代码? -
@AKX:我认为这个想法是对
init_s进行逆向工程。 -
没错。我没有意识到第一部分是假设的。
标签: c pointers assembly struct x86-64