Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!pacbell.com!decwrl!mcnc!rti!bcw From: bcw@rti.rti.org (Bruce Wright) Newsgroups: comp.os.msdos.programmer Subject: Re: Porting UNIX apps. to MS-DOS Summary: Stack segments and interrupts Message-ID: <1990Nov10.223333.4628@rti.rti.org> Date: 10 Nov 90 22:33:33 GMT References: <90296.205211DLV101@psuvm.psu.edu> <1990Oct26.223541.26634@NCoast.ORG> <1990Oct31.190520.11526@Octopus.COM> Organization: Research Triangle Institute, RTP, NC Lines: 48 In article <1990Oct31.190520.11526@Octopus.COM>, stever@Octopus.COM (Steve Resnick ) writes: > In article <9449@jarthur.Claremont.EDU> dfoster@jarthur.Claremont.EDU (Derek R. Foster) writes: > > >void StackSwapFunction(void) > >{ > > unsigned oldSS; > > > > oldSS = _SS; > > You *MUST* disable interrupts during this operation. If an interrupt > occures during the stack segment transition, you will not return > from the interrupt correctly and crash the machine. > > > > _SS = FP_SEG(ABuffer+sizeof(ABuffer)); This is misleading. It's not necessary to disable interrupts when swapping stack segments if you do it right! The point is that the 80x86 processors disable interrupts for one instruction after loading the SS register (some of them disable interrupts for one instruction after loading ANY segment register, but later processors limited it to SS - I forget the exact details of which processors behave which way because it's usually not important). So if the next instruction IMMEDIATELY after loading the new value into SS loads the new value into SP, then you're golden and don't need to mess around with manually disabling interrupts. Look in any of the processor handbooks for the Intel processors. I don't know enough about Turbo C++ to know whether it gets in your way and inserts other instructions after the assignment to SS, but it does work fine in assembler. On the subject of swapping the stack, another place that it's often needed is for interrupt service routines running in real mode - in this mode the 80x86 processor does not perform the interrupt on a separate stack but on whatever stack the application program is using. An amazing number of application programs have allocated EXACTLY the amount of stack that they are going to be using, and there isn't enough room to even save the registers for the interrupt service routine. (Note that they may have taken into account the amount of stack space required by any BIOS calls that they make, so that your interrupt may be OK until you interrupt a BIOS call). Anyway, if you are writing a real mode interrupt handler it's a real good idea to swap stacks to a local stack while using as little of the application program's stack as possible; it can be useful to know that when swapping in either direction you don't have to worry about whether you have 80x86 interrupts enabled when you move the segment as long as you do things in the proper order. Bruce C. Wright