1

I am trying to convert this binary to C but I am having trouble with the while loop. What does
sub esp, 8 mean? as well as push [ebp+_a] ? I am able to get the esp 8 sometimes while changing the print statement. Why is this the case?

fig1

What I have so far.

    #include <stdio.h>

    int main(int argc, char *argv[])

{
    int a = 9;

    while (a) {

        printf("%d, ");
        --a;
    }
        puts("FIRE!");

    return 0;
}
  • You don't need to have the exact same assembly, just have the logic right. Correct? – user202729 Nov 27 '18 at 5:02
1

When the printf function is called (or any other function), it takes all its arguments from the stack. In x86, According to cdecl arguments are pushed in reverse order. So, first the integer value a is pushed on to the top of the stack (push [ebp+_a]) and then the string "%d ," is pushed on to the stack (push offset aD). While printf pops the arguments out, first it takes out the "%d, " and then it gets the value of a i.e., [ebp+_a]. The stack pointer should also be changed accordingly so that the printf function will get the right arguments. Hence the instruction sub esp, 8 which changes the stack pointer(esp).

  • 1
    Actually, sub esp, 8 is there to align the stack on a 16 byte boundary. – user2347953 Nov 27 '18 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.