home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 1.ddi / STARTUP.ZIP / C0D.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-02-13  |  11.6 KB  |  326 lines

  1. ;[]------------------------------------------------------------[]
  2. ;|      C0D.ASM -- Start Up Code For Windows DLLs               |
  3. ;|                                                              |
  4. ;|      Turbo C++ Run Time Library                              |
  5. ;|                                                              |
  6. ;|      Copyright (c) 1987, 1991 by Borland International Inc.  |
  7. ;|      All Rights Reserved.                                    |
  8. ;[]------------------------------------------------------------[]
  9.  
  10.                 locals
  11.  
  12.                 __C0__ = 1
  13. include         RULES.ASI
  14.  
  15.                 ASSUME CS:_TEXT, DS:DGROUP
  16.  
  17.                 public  __acrtused              ;satisfy MS for now
  18. __acrtused      equ     0
  19.  
  20.                 public __exitclean
  21.                 public __exit
  22.  
  23.                 extrn LIBMAIN:far       ;the C routine to be called
  24.                 extrn LOCALINIT:far     ;Windows heap init routine
  25.                 extrn LOCKSEGMENT:far
  26.                 extrn UNLOCKSEGMENT:far
  27.                 extrn GETWINFLAGS:far
  28.                 extrn __WEP:far
  29.  
  30.                 extrn __exitbuf:DIST
  31.                 extrn __exitfopen:DIST
  32.                 extrn __exitopen:DIST
  33.  
  34.                 public LibEntry         ;Entry point for the DLL
  35.                 publicdll WEP
  36.  
  37. NULL            segment
  38.                 db      16 dup (?)
  39.                 ends
  40.  
  41. _CVTSEG         segment
  42.                 public __RealCvtVector
  43. __RealCvtVector label word
  44.                 ends
  45.  
  46. _SCNSEG         segment
  47.                 public __ScanTodVector
  48. __ScanTodVector label word
  49.                 ends
  50.  
  51. _FPSEG          segment
  52.                 public __FPVector
  53. __FPVector      dd      0
  54.                 ends
  55.  
  56. _DATA           segment
  57.                 public _errno
  58. _errno          dw      0
  59.                 public __protected
  60. __protected     dw      0
  61.                 public __8087
  62. __8087          dw      0
  63.                 public __version
  64. __version       label word
  65.                 public __osversion
  66. __osversion     label word
  67.                 public __osmajor
  68. __osmajor       db      0
  69.                 public __osminor
  70. __osminor       db      0
  71.                 public __osmode         ;Used for OS/2 protected mode by MS, 
  72. __osmode        db      0               ;currently set to 0 under Windows
  73.                 public __hInstance
  74. __hInstance     dw      0
  75.                 public __WinAllocFlag   ;Used by malloc for additional flags
  76. __WinAllocFlag  dw      0               ;to pass to GlobalAlloc (used in DLLs)
  77.  
  78. _abend          dw      0               ;gets set to 1 if DLL is terminated
  79.                                         ;by a call to abort() or _exit().
  80.  
  81. CopyRight       db      'Borland C++ - Copyright 1991 Borland Intl.',0
  82.                 ends
  83.  
  84. _TEXT           segment
  85.  
  86. LibEntry        proc far
  87.                 mov     __hInstance, di ;save SI and DI
  88.                 push    si
  89.  
  90.                 push    di              ;handle of the module instance
  91.                 push    ds              ;library data segment
  92.                 push    cx              ;heap size
  93.                 push    es              ;command line segment
  94.                 push    si              ;command line offset
  95.  
  96.                 ;if we have some heap then initialize it
  97.                 jcxz    @@Init          ;jump if no heap specified
  98.  
  99.                 ;call the Windows function LocalInit() to set up the heap
  100.                 ;LocalInit((LPSTR)start, WORD cbHeap);
  101.  
  102.                 push    ds              ;Heap segment
  103.                 xor     ax,ax
  104.                 push    ax              ;Heap start offset in segment
  105.                 push    cx              ;Heap end offset in segment
  106.                 call    LOCALINIT
  107.                 xchg    ax,cx
  108.                 jcxz    @@Exit          ;quit if it failed
  109.  
  110. @@Init:
  111.  
  112. IF LDATA EQ false
  113.                 mov     ax,-1
  114.                 push    ax
  115.                 call    LOCKSEGMENT
  116. ENDIF
  117.  
  118.                 ;Clear _BSS, uninitialized data area
  119.  
  120.                 xor     ax, ax
  121.                 push    ds
  122.                 pop     es
  123.                 mov     di,offset DGROUP:BeginBSS
  124.                 mov     cx,offset DGROUP:EndBSS
  125.                 sub     cx,di
  126.                 cld
  127.                 rep
  128.                 stosb
  129.  
  130. ;Determine DOS version
  131.  
  132.                 mov     ah, 30h
  133.                 int     21h
  134.                 mov     __version, ax   ; save minor and major revision
  135.  
  136. ;Determine whether we are in protected mode
  137.  
  138.                 call    GETWINFLAGS
  139.                 test    ax,1            ; WF_PMODE = 1
  140.                 jz      @@realmode      ; Note:  GETWINFLAGS returns a long,
  141.                                         ; so if WF_PMODE changed it could be 
  142.                                         ; in the high word.
  143.                 mov     __protected, 8  ; Eight is for convenience.
  144. @@realmode:
  145.  
  146. ;Test for 8087 presence
  147.  
  148.                 test    dx,04h          ; WF_8087 = 0x0400
  149.                 jz      @@no8087
  150.                 mov     __8087, 1
  151. @@no8087:
  152.  
  153. ;Call our initialization functions, including C++ static constructors.
  154.  
  155.                 mov     ax,ds
  156.                 mov     es,ax
  157.                 mov     si,offset DGROUP:InitStart      ;si = start of table
  158.                 mov     di,offset DGROUP:InitEnd        ;di = end of table
  159.  
  160.                 mov     __WinAllocFlag, 2000h           ; GMEM_SHARE
  161.                 call    Initialize
  162.                 mov     __WinAllocFlag, 0
  163.  
  164.                 ;invoke the C routine to do any special initialization
  165. @@Main:         call    LIBMAIN         ;invoke the 'C' routine (result in AX)
  166.                 mov     di, __hInstance ;restore SI and DI
  167.                 pop     si
  168.                 ret
  169.  
  170. @@Exit:         mov ax, 0               ;set return code
  171.                 pop si                  ;remove arguments to LIBMAIN
  172.                 pop es                  ;  since we didn't call it. 
  173.                 pop cx
  174.                 pop ds
  175.                 pop di                  
  176.                 pop si                  ;restore saved SI.  DI is restored 
  177.                 ret                     ;  by removing arguments to LIBMAIN
  178.                 endp
  179.  
  180. __exit          proc
  181.                 mov _abend, 1
  182. __exitclean:
  183.                 mov al,[bp+cPtrSize]
  184.                 mov ah,4ch
  185.                 int 21h
  186.                 ret
  187.                 endp
  188.  
  189. WEP             proc    windows pascal far nParam:WORD
  190.                 push    si
  191.                 push    di
  192.  
  193.                 cmp     _abend, 0
  194.                 jne     @@error
  195.                 push    nParam
  196.                 call    __WEP
  197.                 push    ax
  198.  
  199.                 mov     ax,ds
  200.                 mov     es,ax
  201.                 mov     si,offset DGROUP:ExitStart
  202.                 mov     di,offset DGROUP:ExitEnd
  203.                 call    Cleanup
  204.  
  205. IF LPROG
  206.                 call    dword ptr [__exitbuf]
  207.                 call    dword ptr [__exitfopen]
  208.                 call    dword ptr [__exitopen]
  209. ELSE
  210.                 call    word ptr [__exitbuf]
  211.                 call    word ptr [__exitfopen]
  212.                 call    word ptr [__exitopen]
  213. ENDIF
  214.  
  215. @@unlock:
  216.  
  217. IF LDATA EQ false
  218.                 mov     ax,-1
  219.                 push    ax
  220.                 call    UNLOCKSEGMENT
  221. ENDIF
  222.  
  223.                 pop     ax  
  224.                 pop     di
  225.                 pop     si
  226.                 ret
  227.  
  228. @@error:
  229.                 push    1
  230.                 jmp     @@unlock
  231.                 
  232.                 endp
  233.  
  234.  
  235. ;------------------------------------------------------------------
  236. ;  Loop through a startup/exit (SE) table, 
  237. ;  calling functions in order of priority.
  238. ;  ES:SI is assumed to point to the beginning of the SE table
  239. ;  ES:DI is assumed to point to the end of the SE table
  240. ;  First 64 priorities are reserved by Borland
  241. ;------------------------------------------------------------------
  242. PNEAR           EQU     0
  243. PFAR            EQU     1
  244. NOTUSED         EQU     0ffh
  245.  
  246. SE              STRUC
  247. calltype        db      ?                       ; 0=near,1=far,ff=not used
  248. priority        db      ?                       ; 0=highest,ff=lowest
  249. addrlow         dw      ?
  250. addrhigh        dw      ?
  251. SE              ENDS
  252.  
  253. Initialize      proc near
  254. @@Start:        mov     ah,0ffh                 ;start with lowest priority
  255.                 mov     dx,di                   ;set sentinel to end of table
  256.                 mov     bx,si                   ;bx = start of table
  257.  
  258. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  259.                 je      @@EndOfTable            ;yes, exit the loop
  260.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  261.                 je      @@Next
  262.                 cmp     es:[bx.priority],ah     ;check the priority
  263.                 ja      @@Next                  ;too high?  skip
  264.                 mov     ah,es:[bx.priority]     ;keep priority
  265.                 mov     dx,bx                   ;keep index in dx
  266. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  267.                 jmp     @@TopOfTable
  268.  
  269. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  270.                 je      @@Done                  ;yes, quit
  271.                 mov     bx,dx                   ;bx = highest priority item
  272.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  273.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  274.                 push    es                      ;save es
  275.                 je      @@NearCall
  276.  
  277. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  278.                 pop     es                      ;restore es
  279.                 jmp     short @@Start
  280.  
  281. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  282.                 pop     es                      ;restore es
  283.                 jmp     short @@Start
  284.  
  285. @@Done:         ret
  286.                 endp
  287.  
  288. Cleanup         proc near
  289. @@Start:        mov     ah,0                    ;start with highest priority
  290.                 mov     dx,di                   ;set sentinel to end of table
  291.                 mov     bx,si                   ;bx = start of table
  292.  
  293. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  294.                 je      @@EndOfTable            ;yes, exit the loop
  295.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  296.                 je      @@Next
  297.                 cmp     es:[bx.priority],ah     ;check the priority
  298.                 jb      @@Next                  ;too low?  skip
  299.                 mov     ah,es:[bx.priority]     ;keep priority
  300.                 mov     dx,bx                   ;keep index in dx
  301. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  302.                 jmp     @@TopOfTable
  303.  
  304. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  305.                 je      @@Done                  ;yes, quit
  306.                 mov     bx,dx                   ;bx = highest priority item
  307.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  308.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  309.                 push    es                      ;save es
  310.                 je      @@NearCall
  311.  
  312. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  313.                 pop     es                      ;restore es
  314.                 jmp     short @@Start
  315.  
  316. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  317.                 pop     es                      ;restore es
  318.                 jmp     short @@Start
  319.  
  320. @@Done:         ret
  321.                 endp
  322.  
  323.                 ends
  324.                 end LibEntry
  325.  
  326.