Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Debug and the stack - Newbi question. 2

Status
Not open for further replies.

BagelBob

Programmer
Apr 13, 2001
7
0
0
US
I'm writing some practice programs for 16 bit NASM and begin
my housekeeping as follows:

...

mov ax,initdata ;move segment address of initalized
mov ds,ax ;data through ax to ds.
mov ax,stackseg ;move segment address of stack through
mov ss,ax ;ax to ss.
mov sp,stackptr ;put the offset of the byte after the
;reserved bytes into sp.
...

This real mode, segmented model program assembles, links and
runs.

When I load my program into DEBUG and immediately look at
the status of the regesters I find that the stack segment
and the stack pointer have already been correctly loaded,
i.e., before a single instruction has been executed. I've
varied the number of bytes reserved for the stack and it's
always correct, right there in sp.
AND, when I trace through the program my instruction
mov sp,stackptr
is not there.

What's going on here?
 
By any chance, did you get an STI (or is that a CLI... always confuse those two) instruction instead of mov sp,stackptr? Or did it completely totally disappear when you unassemble using 'u'? If so that would be a problem with NASM or your linker...

What processor directive did you use? The 8086/8088 had a small chance to crash if you happen to interrupt it while it was changing the stack pointer. Most modern assemblers get around this by first disabling, then re-enabling the interrupts.

sti
mov sp,stackptr
cli

Anyway, the phenomenon you saw wherein SS:SP was initialized before you even start the program is not really strange. DOS initializes SS:SP for your program if you define a segment with STACK combine type. This is because Link automatically combines all STACK segments into a single segment, then stores the total length and the offset of the segment in the file into a section of the MZ header labelled 'Initial SS:SP.' In fact it is due to this that Link will warn you that you have no stack segment defined, if it can't find one it will load 0:0 in initial SS:SP and DOS will load that (adding the offset of your program in memory to the SS) before passing control to the program.

My suggestion is, just define your stack segment with a STACK combine type and let DOS worry about it.
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Hi,
The EXE file structure of DOS has fields reserved
for StackPointer. So, ur initial SP would already
be loaded by the EXE file loader. Moreover the loader
loads the Segment register with value of ur PSP.
(Program segment prefix ). hence the observation

Sarnath
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top