Quote:
Originally Posted by Lee Reverse software engineering has been something i am interested in but it seems and i have been told its very hard to understand sometimes so really i am focusing on other things before looking into this. |
When you say "its very hard to understand sometimes", are you referring to ASM or reverse engineering? I've never tried the latter other than converting simple file formats. As for ASM, the most important thing you can do is comment. A simple factorial function in ASM (note - using Intel syntax with the NASM assembler):
Code:
global _factorial ;let a linker know this function exists
;C prototype - int factorial (int argument);
_factorial:
push ebp
mov ebp, esp ;preserve the stack pointer
sub esp, 4 ;reserve 4 bytes for local storage
mov eax, dword [ebp + 8] ;copy 1st function argument into EBX
cmp eax, 1
jg factorial_run ;if (EAX > 1) goto factorial_run
mov eax, 1 ;set EAX to 1 (our return value can't be less than 1)
jmp factorial_end ;goto factorial_end
factorial_run:
mov dword [esp], eax ;copy EAX onto the top of the stack
dec eax ;eax = eax - 1 (or eax--)
push eax
call _factorial ;factorial(EAX)
;return value of the the function is stored in EAX
add esp, 4 ;set the stack pointer back to our saved value
imul dword [esp] ;if we omit the destination, it is assumed to be EAX, which is convenient in this case
factorial_end:
mov esp, ebp ;restore the stack pointer
pop ebp ;finish restoring the original stuff
ret ;return (EAX) Simple, right?
All of that code in ASM is equivalent to:
PHP Code:
int factorial (int argument) {
if (argument > 1)
return (argument * factorial(argument - 1));
return 1;
}
Care to compare the file sizes of the object files (compiled (if necessary) and assembled, but not linked with libraries)? The file size of the C function alone is 575 bytes for me. The file size of an entire ASM program that demonstrates the ASM version of the function is 483 bytes. While that's a little less than 100 bytes, that amount of saved space can really come in handy for making things run faster, especially for things like games. My ASM version could be structured differently, but I figured I'd add something in to make things more logical when something like -14 was passed to the factorial function. Typically, I don't even check for that because I am just writing it to get it done with.
