View Single Post
  #3 (permalink)  
Old 08-05-2007, 11:39 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by Lee View Post
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.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 08-05-2007 at 11:50 PM.
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (08-06-2007)