I have recently started getting into assembly for the purpose of reverse engineering. I started small with understanding basic datatypes, but I want to move on to more complex datatypes and functions. I am trying to understand what is happening in both methods requestMaxPow and computePowers
Here is the source that I use
#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"
The assembly below is for the requestMaxPow method, which is the hardest for me to understand. Specifically I don't understand what "gs" means at 0xc5, and I have no idea what is going on between lines 0xce - 0x50. could someone well versed explain line by line what is happening?
(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.
The assembly for the computePowers method is much easier to understand. I include it just in case it has relevance to my main question.
(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.
Edit 1 After looking at the code for a while longer I realized the xor is happening on eax to "0" it out, does that happen so that a return value can be stored into eax?