home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vp21beta.zip / ARTLSRC.RAR / SYS / THUNK.ASM < prev    next >
Assembly Source File  |  2000-08-15  |  4KB  |  98 lines

  1.                 .386
  2.  
  3.                 EXTRN   DosFlatToSel:Proc
  4.  
  5. FLAT        GROUP    CODE32
  6.  
  7. CODE32          SEGMENT DWORD PUBLIC USE32 'CODE'
  8.                 ASSUME  CS:FLAT,DS:FLAT
  9.  
  10. @Uses           equ     6*4                     ; ds,es,ebp,ebx,esi,edi
  11.  
  12. System@__Far16Pas PROC  Near
  13.                 PUBLIC  System@__Far16Pas
  14.                 push    ds es ebp ebx esi edi
  15.                 mov     ebp,esp
  16.                 mov     edx,[esp+@Uses+4]       ; Parameter Data
  17.                 mov     eax,[esp+@Uses+8]       ; Far16Proc @
  18.                 lea     esi,[esp+@Uses+12]      ; Last parameter offset
  19.                 mov     ebx,edx
  20.                 test    edx,edx
  21.                 jz      @@NoParms
  22.               @@1:
  23.                 add     esi,4
  24.                 shr     edx,2
  25.                 jnz     @@1
  26. ; esi = First parameter offset
  27. ; Setup thunk stack
  28.               @@NoParms:
  29.                 cmp     sp,4*1024       ; 4K = Thunk minimum stack
  30.                 jae     @@2
  31.                 xor     sp,sp
  32.               @@2:
  33.                 Call    DosFlatToSel    ; eax = Far16Proc@
  34.                 push    eax             ; Save Far16Proc@
  35.                 mov     edi,esp         ; edi = @ Far16Proc@
  36.                 push    ebp             ; Code to save EBP, ESP, and SS
  37.                 push    esi             ; Stack position after call(with removed parameters)
  38.                 mov     eax,esp
  39.                 push    ss              ; Save FLATSS:ESP
  40.                 push    eax
  41.               @@3:
  42.                 mov     ecx,ebx
  43.                 and     ecx,11b
  44.                 jz      @@ParmDone
  45.                 shr     ebx,2
  46.                 sub     esi,4
  47.                 mov     eax,[esi]
  48.                 jmp     DWord Ptr @@JmpTable[ecx*4-4]
  49. ALIGN 4
  50. @@JmpTable      dd      OFFSET FLAT:@@Word
  51.                 dd      OFFSET FLAT:@@DWord
  52.                 dd      OFFSET FLAT:@@Addr
  53.               @@Word:
  54.                 push    ax
  55.                 jmp     @@3
  56. ALIGN 4
  57.               @@Addr:
  58.                 Call    DosFlatToSel
  59.               @@DWord:
  60.                 push    eax
  61.                 jmp     @@3
  62. ALIGN 4
  63.               @@ParmDone:
  64.                 mov     eax,esp                 ; Convert SS:ESP to
  65.                 Call    DosFlatToSel            ; SS:SP for a 0:32->16:16 thunk.
  66.                 push    eax
  67.                 lss     sp,[esp]                ; Here is the far (16:16) jump
  68.                 jmp     Far Ptr Thunk_16        ; to the function's 16 bit thunk.
  69. ALIGN 4
  70. Back_To_32:
  71.                 and     esp,0FFFFh
  72.                 lss     esp,[esp]
  73.                 shl     eax,16                  ; Convert return value:
  74.                 shrd    eax,edx,16              ; DX:AX -> EAX
  75.                 pop     ecx                     ; ESP after call
  76.                 pop     esp                     ; ESP after pushing used registers
  77.                 pop     edi esi ebx ebp es ds   ; Restore used registers
  78.                 pop     edx                     ; Return@
  79.                 mov     esp,ecx                 ; Restore ESP
  80.                 jmp     edx                     ; Return to caller
  81. System@__Far16Pas ENDP
  82.  
  83. CODE32          ENDS
  84.  
  85.  
  86. CODE16          SEGMENT WORD PUBLIC USE16 'CODE'
  87.                 ASSUME  CS:CODE16
  88. Thunk_16:
  89.                 mov     ax,ss
  90.                 mov     ds,ax
  91.                 mov     es,ax
  92.                 Call    DWord Ptr [di]
  93.                 jmp     Far Ptr Back_To_32
  94.  
  95. CODE16          ENDS
  96.  
  97.                 END
  98.