我最近开始为了逆向工程而进行组装。我从了解基本数据类型开始,但我想继续了解更复杂的数据类型和函数。我试图了解这两种方法requestMaxPow和computePowers
这是我使用的来源
#include <stdio.h>
int requestMaxPow();
int computerPowers(int);
int main(){
int max = requestMaxPow();
computePowers(max);
return 0;
}
int requestMaxPow(){
int maxPow;
scanf ("%d", &maxPow);
return maxPow;
}
int computePowers(int MaxPow){
int currentVal = 0;
int currentPow = 0;
for(;currentPow < MaxPow; ++currentPow){
currentVal = currentPow * currentPow;
}
}
Compiled with GCC with the following arguments "gcc -g -O0 morecomplex.c -o morecoplex"
下面的程序集是针对requestMaxPow方法的,这是我最难理解的。具体来说,我不明白 0xc5 处的“gs”是什么意思,我也不知道 0xce - 0x50 之间发生了什么。精通的人可以逐行解释发生了什么吗?
(gdb) disassemble
Dump of assembler code for function requestMaxPow:
0x080484bf <+0>: push ebp
0x080484c0 <+1>: mov ebp,esp
0x080484c2 <+3>: sub esp,0x18
=> 0x080484c5 <+6>: mov eax,gs:0x14
0x080484cb <+12>: mov DWORD PTR [ebp-0xc],eax
0x080484ce <+15>: xor eax,eax
0x080484d0 <+17>: sub esp,0x8
0x080484d3 <+20>: lea eax,[ebp-0x10]
0x080484d6 <+23>: push eax
0x080484d7 <+24>: push 0x80485b0
0x080484dc <+29>: call 0x8048380 <__isoc99_scanf@plt>
0x080484e1 <+34>: add esp,0x10
0x080484e4 <+37>: mov eax,DWORD PTR [ebp-0x10]
0x080484e7 <+40>: mov edx,DWORD PTR [ebp-0xc]
0x080484ea <+43>: xor edx,DWORD PTR gs:0x14
0x080484f1 <+50>: je 0x80484f8 <requestMaxPow+57>
0x080484f3 <+52>: call 0x8048350 <__stack_chk_fail@plt>
0x080484f8 <+57>: leave
0x080484f9 <+58>: ret
End of assembler dump.
computePowers方法的程序集更容易理解。我把它包括在内,以防它与我的主要问题有关。
(gdb) disassemble
Dump of assembler code for function computePowers:
0x080484fa <+0>: push ebp
0x080484fb <+1>: mov ebp,esp
0x080484fd <+3>: sub esp,0x10
=> 0x08048500 <+6>: mov DWORD PTR [ebp-0x4],0x0
0x08048507 <+13>: mov DWORD PTR [ebp-0x8],0x0
0x0804850e <+20>: jmp 0x804851e <computePowers+36>
0x08048510 <+22>: mov eax,DWORD PTR [ebp-0x8]
0x08048513 <+25>: imul eax,DWORD PTR [ebp-0x8]
0x08048517 <+29>: mov DWORD PTR [ebp-0x4],eax
0x0804851a <+32>: add DWORD PTR [ebp-0x8],0x1
0x0804851e <+36>: mov eax,DWORD PTR [ebp-0x8]
0x08048521 <+39>: cmp eax,DWORD PTR [ebp+0x8]
0x08048524 <+42>: jl 0x8048510 <computePowers+22>
0x08048526 <+44>: leave
0x08048527 <+45>: ret
End of assembler dump.
编辑 1 在查看代码一段时间后,我意识到 xor 发生在 eax 上以将其“0”输出,是否发生这种情况以便可以将返回值存储到 eax 中?