News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Balance stack before program exit?

Started by BigDaddy, March 30, 2005, 06:09:41 PM

Previous topic - Next topic

BigDaddy

If I'm bailing out from a program due to an error being detected, should I first restore the stack/stack pointer, etc? 

Come to think of it, is there anything that I need to keep track of before returning to the environment?  I never did, before, unless an HLL was calling my proc. 

What if another program loads and executes mine? 

Vortex

Hi BigDaddy,

Before terminating your application, you should always pop up the registers stored on the stack. This is necessary as the system would fail after a program termination with an unbalanced stack.

MichaelW

BigDaddy,

For a COM file terminated via a near return, the stack must be in the condition it was in when the program received control. Balancing the stack for other programs could not hurt, but AFAIK, for a program terminated via Interrupts 20h, 21h, or 27h, it is not necessary (or at least not for programs with sufficient stack space and the stack in the normal location). Also, for a program that allocates memory or hooks interrupt vectors, you must free the allocated memory and unhook the interrupt vectors before the program terminates.
eschew obfuscation