【发布时间】:2017-03-02 20:31:41
【问题描述】:
我有以下来自 C 函数 long loop(long x, int n) 的汇编代码
x 在%rdi 中,n 在%esi 中,在 64 位机器上。我已经将我的 cmets 写在我认为组装说明正在做的事情上。
loop:
movl %esi, %ecx // store the value of n in register ecx
movl $1, %edx // store the value of 1 in register edx (rdx).initial mask
movl $0, %eax //store the value of 0 in register eax (rax). this is initial return value
jmp .L2
.L3
movq %rdi, %r8 //store the value of x in register r8
andq %rdx, %r8 //store the value of (x & mask) in r8
orq %r8, %rax //update the return value rax by (x & mask | [rax] )
salq %cl, %rdx //update the mask rdx by ( [rdx] << n)
.L2
testq %rdx, %rdx //test mask&mask
jne .L3 // if (mask&mask) != 0, jump to L3
rep; ret
我有以下需要对应汇编代码的C函数:
long loop(long x, int n){
long result = _____ ;
long mask;
// for (mask = ______; mask ________; mask = ______){ // filled in as:
for (mask = 1; mask != 0; mask <<n) {
result |= ________;
}
return result;
}
我需要一些帮助来填补空白,我不能 100% 确定组装说明是什么,但我已经通过对每一行进行评论来尽力而为。
【问题讨论】:
-
我们不是“做我的功课”的网站。向你的老师寻求建议。
-
这些练习的第一步是猜测哪些变量在哪些寄存器中。需要注意的一件事是函数的返回值最终在
rax寄存器中。 -
对不起,我不是要你做我的作业。我只是将 cmets 添加到我认为每条指令正在进行的操作中。我需要帮助将它连接到 C 代码。
-
test指令主要用于设置标志位,方便后面的分支指令使用。因此,虽然从技术上讲test正在执行mask & mask,但等效的 C 代码只是mask != 0。 -
orq指令后面的注释不太对。你应该再看一遍。
标签: c assembly x86-64 reverse-engineering att