PeNpeL
PeNpeL

Reputation: 103

Do I have to clean the stack when I call the C function "exit" from assembly?

I was asked to create a small program in assembly while using C functions. While doing so, I was wondering about something specific.

I know that when working with assembly, wherever I want to call a C function, I must push it's arguments to the stack, and after the function returned I must pop those arguments (or add 4×x to esp, where x represents the number of pushed arguments).

My question is this: When calling the C function exit in particular, I must first push an argument for the status. Let's say I want to push 0 to indicate that my program worked without errors.

Knowing that the exit function does not return and that I must use it and can't just call the exit system interrupt myself, how can I, in this case, pop that 0 from the stack? Does the exit function perform that for me?

Upvotes: 4

Views: 528

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You don't have to. As exit() does not return and the program is terminated, the system will deallocate all memory you used, including the stack.

Note though that the compiler will generate an add esp, 4 to cleanup the stack because the compiler doesn't know that exit will never return.

Upvotes: 1

Related Questions