我有这个机器代码,我在过去4天一直在努力,但我似乎无法掌握它.
pushl %ebp
movl %esp, %ebp
jmp .L3
L4:
addl $3,8(%ebp)
L3:
movl 8(%ebp),%eax // having issues here
movzbl (%eax),%eax//here
cmpb $1,%al// and here
je .L5
cmpl 12(%ebp),%eax
jne .L4
movl $8,%eax
.L5: leave
ret
Run Code Online (Sandbox Code Playgroud)
这是我对解决方案的尝试:
int main(int s, char *t){
while (s != 1) {
if (s == *t){
s = 8;
}
else{s+=3;}
}
return;
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我,我是否正确接近这个?如果没有帮助我指向正确的方向?
该函数看起来是__cdecl,因为它引用8(%ebp)和12(%ebp)不引用任何一个,这表明它们是参数.叫他们arg1和arg2分别.
我们可以注释程序集:
/* Function prologue */
pushl %ebp
movl %esp, %ebp
jmp .L3
L4:
/* Move to next loop iteration */
addl $3, arg1
L3:
/* Copy value of arg1 to %eax */
movl arg1, %eax
/* (%eax) means to dereference %eax (as a memory address).
movzbl means to load a single byte and zero-extend to the rest of the register.
Therefore, the byte at address %eax is put into %eax. */
movzbl (%eax), %eax
/* Compare the low byte of %eax against 1 */
cmpb $1, %al
/* If equal, exit (%eax = 1) */
je .L5
/* Compare %eax against arg2 */
cmpl arg2, %eax
/* If not equal, keep looping */
jne .L4
/* Otherwise, if it was equal, we quit and return %eax = 8 */
movl $8,%eax
.L5: leave
ret
Run Code Online (Sandbox Code Playgroud)
在C代码中,这变成了
int fn(unsigned char *ptr, int sentinel) {
while(1) {
unsigned char c = *ptr;
if(c == 1) return c;
if(c == sentinel) return 8;
ptr += 3;
}
}
Run Code Online (Sandbox Code Playgroud)