Pushing DS at the beginning of an .exe

Started by BigDaddy, January 03, 2005, 07:44:33 PM

Previous topic - Next topic

BigDaddy

Well, folks, I managed to get back to the issue of the DS getting pushed to the stack at the beginning of some sample code.  These are stand-alone programs, BTW, not intended for linking with an HLL, as I first thought.  I found this technique in many old books, and finally found one that "sort of" answers my questions. 

These .exe's end with a RET, so part of my confusion was that the "push ds;xor ax,ax;push ax" would screw up the return address, because none of this stuff gets popped before the return.  (And besides, I thought that we were always supposed end with an int 21,ah=4c.) 

Now, the explanation that I got is that the DS is always pointed to the beginning of the PSP by the program loader.  But this makes me wonder:

1. I haven't had a chance to look, but DS:0 must have some executable instructions for this to work, right? 
2. Is this a good way to end a program?  (Or should I use the INT 21.)
3. If I'm using int 21, should I still bother with pushing this stuff?  (It would never get used, right?)

Thanks for your answers.
Dan

MichaelW

The first word in the PSP is an Interrupt 20h instruction, placed there for compatibility with early versions of MS-DOS. It seems to me that the RET must actually be a RETF, otherwise the instruction would return to offset zero in the current code segment. And this coding would work correctly only if DS is loaded with the segment address of the PSP. For a COM file DS is normally loaded with the segment address of the PSP, but so is CS, so a JMP 0 would suffice to terminate the program. For an EXE file DS is initially loaded with the segment address of the PSP, but the normal startup code loads it with the segment address of the data segment, and the RETF instruction would return to offset zero in the data segment.

These days, you should probably use Interrupt 21h, Function 4Ch to terminate the program, and if you did these instructions would serve no useful purpose.
eschew obfuscation

BigDaddy

Having a debugger in front of you helps. 

The first two bytes of the PSP are an INT 20, so pushing of DS and zero at program startup aims the final RET at code that exits.  There's no harm in starting that way, no matter how you finally exit.