Listing 3 The assembler routine for changing stacks

; ctxtswc will do a contextswitch by changing stacks.
; the stack in use will be stored on the heap, and the
; new stack which is previously stored on the heap,
; will be loaded.
   .MODEL  COMPACT
   .CODE
   EXTRN   _stklen:word
   EXTRN   _PREVIOUS:dword
   EXTRN   _CURRENT:dword
   PUBLIC  @ctxtswc$qv
@ctxtswc$qv PROC NEAR
   push    bp                    ; save bp
   mov     cx,_stklen            ; put nmbr of bytes
   sub     cx,sp                 ; in cx
   les     bx,dword ptr DGROUP:_PREVIOUS
   mov     word ptr es:[bx+4],cx ; save sz of usedstk
   mov     di,word ptr es:[bx+2] ; set adr for stk
   mov     es,word ptr es:[bx]   ; copy in di and es
   mov     ax,ds                 ; save ds
   mov     bx,ss                 ; set adr of stk in
   mov     ds,bx                 ; ds
   mov     si,sp                 ; and si
   rep     movsb                 ; do copy
   mov     ds,ax                 ; reset ds
   les     bx,dword ptr DGROUP:_CURRENT
   mov     cx,word ptr es:[bx+4] ; get sz of stk copy
   mov     ax,ds                 ; save ds
   mov     sp,_stklen
   sub     sp,cx                 ; set stkPtr
   mov     si,word ptr es:[bx+2] ; set adr of stored
   mov     ds,word ptr es:[bx]   ; stk in si and ds
   mov     di,sp                 ; set dest for copy
   mov     dx,ss                 ; di=stkPtr
   mov     es,dx                 ; and es=stkSeg
   rep     movsb                 ; do copy
   mov     ds,ax                 ; reset ds
   pop     bp                    ; reset bp
   ret
@ctxtswc$qv ENDP
   END
; End of File