[comp.sys.ibm.pc] MSC 5.0 Local Stacks -- HERE IS HOW TO DO IT

Devin_E_Ben-Hur@cup.portal.com (05/12/88)

James H King posts some stack-swapping routines for MS-C:
> [in _StackADJ:]
>	mov	ax,ds
>	mov	ss,ax
>	mov	ax,offset LocalStack+4024d	;load top of local stack
>	mov	sp,ax				;reset stack pointer
>
> [in _StackRES]
>	mov	ax,Orig_SS
>	mov	ss,ax
>	mov	ax,Orig_SP
>	mov	sp,ax

  The problem here is that there's no protection against interrupts
comming in between the setting of SS and SP (ie. there's a new SS but
the old SP, so the stack used by the interrupt will be bogus). The
80x86 does provide a one instruction lock preventing interrupts after
a mov to SS, so these instructions should be re-ordered like so:

 [in _StackADJ:]
	mov	ax,ds
	mov	bx,offset LocalStack+4024d	;load top of local stack
	mov	ss,ax
	mov	sp,bx				;reset stack pointer

 [in _StackRES]
	mov	ax,Orig_SS
	mov	bx,Orig_SP
	mov	ss,ax
	mov	sp,bx

  On some truely ancient 8088 chip masks, there was a bug which
didn't lock interrupts after setting SS, so to be 100% safe one
should surround the previous critical stack-switching instructions
with CLI and STI to disable and enable interrupts:

	CLI
	mov	ss,ax
	mov	sp,bx
	STI

  It may seem like an infinatisimal (sp?) possibility that an
interrupt will come in during the the critical two instructions when
the SS:SP values are bogus, but our old pal Murphy always has the
last word.

ucbvax!sun!portal!devin.e.ben-hur%cupertino.pcc