home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 7.ddi / STARTUP.ZIP / C0D.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-06-10  |  13.7 KB  |  387 lines

  1. ;[]------------------------------------------------------------[]
  2. ;|      C0D.ASM -- Start Up Code For Windows DLLs               |
  3. ;[]------------------------------------------------------------[]
  4.  
  5. ;
  6. ;       C/C++ Run Time Library - Version 5.0
  7. ;       Copyright (c) 1991, 1992 by Borland International
  8. ;       All Rights Reserved.
  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.  
  21.                 extrn LIBMAIN:far       ;the C routine to be called
  22.                 extrn LOCALINIT:far     ;Windows heap init routine
  23.                 extrn LOCKSEGMENT:far
  24.                 extrn UNLOCKSEGMENT:far
  25.                 extrn GETWINFLAGS:far
  26.                 extrn __WEP:far
  27.                 extrn __cexit:DIST
  28.                 extrn __setupio:near    ;required!
  29.  
  30.                 public LibEntry         ;Entry point for the DLL
  31.                 publicdll WEP
  32.  
  33. NULL            segment
  34.                 db      16 dup (?)
  35.                 ends
  36.  
  37. _CVTSEG         segment
  38.                 public __RealCvtVector
  39. __RealCvtVector label word
  40.                 ends
  41.  
  42. _SCNSEG         segment
  43.                 public __ScanTodVector
  44. __ScanTodVector label word
  45.                 ends
  46.  
  47. _FPSEG          segment
  48.                 public __FPVector
  49. __FPVector      dd      0
  50.                 ends
  51.  
  52. _DATA           segment
  53.                 public _errno
  54. _errno          dw      0
  55.                 public __protected
  56. __protected     dw      0
  57.                 public __8087
  58. __8087          dw      0
  59.                 public __StartTime
  60. __StartTime     dd      0
  61.                 public __version
  62. __version       label word
  63.                 public __osversion
  64. __osversion     label word
  65.                 public __osmajor
  66. __osmajor       db      0
  67.                 public __osminor
  68. __osminor       db      0
  69.                 public __osmode         ;Used for OS/2 protected mode by MS,
  70. __osmode        db      0               ;currently set to 0 under Windows
  71.                 public __hInstance
  72. __hInstance     dw      0
  73.                 public __WinAllocFlag   ;Used by malloc for additional flags
  74. __WinAllocFlag  dw      0               ;to pass to GlobalAlloc (used in DLLs)
  75.                 public __LockWIN87EM    ;Used do lock down WIN87EM to avoid
  76. __LockWIN87EM   dw      1               ;DLL unload ordering problem
  77.  
  78. _abend          dw      1               ;If LibEntry is called gets set to
  79.                                         ;normal state (0).  If it is 1 then
  80.                                         ;exit routines are not performed
  81.                                         ;Gets set to 1 if DLL is terminated
  82.                                         ;by a call to abort() or _exit().
  83.  
  84. CopyRight       db      'Borland C++ - Copyright 1991 Borland Intl.',0
  85.                 ends
  86.  
  87. _TEXT           segment
  88.  
  89. LibEntry        proc far
  90.                 mov     __hInstance, di ;save SI and DI
  91.                 push    si
  92.  
  93.                 push    di              ;handle of the module instance
  94.                 push    ds              ;library data segment
  95.                 push    cx              ;heap size
  96.                 push    es              ;command line segment
  97.                 push    si              ;command line offset
  98.  
  99.                 ;if we have some heap then initialize it
  100.                 jcxz    @@Init          ;jump if no heap specified
  101.  
  102.                 ;call the Windows function LocalInit() to set up the heap
  103.                 ;LocalInit((LPSTR)start, WORD cbHeap);
  104.  
  105.                 push    ds              ;Heap segment
  106.                 xor     ax,ax
  107.                 push    ax              ;Heap start offset in segment
  108.                 push    cx              ;Heap end offset in segment
  109.                 call    LOCALINIT
  110.                 xchg    ax,cx
  111.                 jcxz    @@JmpExit       ;quit if it failed
  112.                 jmp     short @@Init
  113. @@JmpExit:      jmp     @@Exit
  114.  
  115. @@Init:
  116.  
  117. IF LDATA EQ false
  118.                 mov     ax,-1
  119.                 push    ax
  120.                 call    LOCKSEGMENT
  121. ENDIF
  122.  
  123.                 ;Clear _BSS, uninitialized data area
  124.  
  125.                 xor     ax, ax
  126.                 push    ds
  127.                 pop     es
  128.                 mov     di,offset DGROUP:BeginBSS
  129.                 mov     cx,offset DGROUP:EndBSS
  130.                 sub     cx,di
  131.                 cld
  132.                 rep
  133.                 stosb
  134.  
  135. ;Save start time of DLL
  136.  
  137.                 mov     ah, 0
  138.                 int     1ah             ; get current BIOS time in ticks
  139.                 mov     word ptr __StartTime,dx ; save it for clock() fn
  140.                 mov     word ptr __StartTime+2,cx
  141.                 or      al,al                   ; was midnight flag set?
  142.                 jz      @@NotMidnight
  143.                 mov     ax,40h                  ; set BIOS midnight flag
  144.                 mov     es,ax                   ;  at 40:70
  145.                 mov     bx,70h
  146.                 mov     byte ptr es:[bx],1
  147. @@NotMidnight:
  148.  
  149. ;Determine DOS version
  150.  
  151.                 mov     ah, 30h
  152.                 int     21h
  153.                 mov     __version, ax   ; save minor and major revision
  154.  
  155. ;Determine whether we are in protected mode
  156.  
  157.                 call    GETWINFLAGS
  158.                 test    ax,1            ; WF_PMODE = 1
  159.                 jz      @@realmode      ; Note:  GETWINFLAGS returns a long,
  160.                                         ; so if WF_PMODE changed it could be
  161.                                         ; in the high word.
  162.                 mov     __protected, 8  ; Eight is for convenience.
  163. @@realmode:
  164.  
  165. ;Test for 8087 presence
  166.  
  167.                 test    ax,0400h        ; WF_8087 = 0x0400
  168.                 jz      @@no8087
  169.                 mov     __8087, 1
  170. @@no8087:
  171.  
  172. ;Call our initialization functions, including C++ static constructors.
  173.  
  174.                 mov     ax,ds
  175.                 mov     es,ax
  176.                 mov     si,offset DGROUP:InitStart      ;si = start of table
  177.                 mov     di,offset DGROUP:InitEnd        ;di = end of table
  178.  
  179.                 mov     __WinAllocFlag, 2000h           ; GMEM_SHARE
  180.                 call    Initialize
  181.                 mov     __WinAllocFlag, 0
  182.                 mov     _abend, 0                       ; Set LibEntry called
  183.  
  184.                 ;invoke the C routine to do any special initialization
  185. @@Main:         call    LIBMAIN         ;invoke the 'C' routine (result in AX)
  186.                 mov     di, __hInstance ;restore SI and DI
  187.                 pop     si
  188.                 ret
  189.  
  190. @@Exit:         mov ax, 0               ;set return code
  191.                 pop si                  ;remove arguments to LIBMAIN
  192.                 pop es                  ;  since we didn't call it.
  193.                 pop cx
  194.                 pop ds
  195.                 pop di
  196.                 pop si                  ;restore saved SI.  DI is restored
  197.                 ret                     ;  by removing arguments to LIBMAIN
  198.                 endp
  199.  
  200. ;---------------------------------------------------------------------------
  201. ;       _cleanup()      call all #pragma exit cleanup routines.
  202. ;       _checknull()    check for null pointer zapping copyright message
  203. ;       _terminate(exitcode, quick)     exit program with error code
  204. ;       _restorezero()  restore interrupt vectors
  205. ;
  206. ;       These functions are called by exit(), _exit(), _cexit(),
  207. ;       and _c_exit().
  208. ;---------------------------------------------------------------------------
  209.  
  210. ;       Call cleanup routines
  211.  
  212. __cleanup       PROC    DIST
  213.                 PUBLIC  __cleanup
  214.  
  215.                 mov     ax,ds
  216.                 mov     es,ax
  217.                 push    si
  218.                 push    di
  219.                 mov     si,offset DGROUP:ExitStart
  220.                 mov     di,offset DGROUP:ExitEnd
  221.                 call    Cleanup
  222.                 pop     di
  223.                 pop     si
  224.                 ret
  225. __cleanup       ENDP
  226.  
  227. ;       Check for null pointers before exit.  NO-OP on Windows.
  228.  
  229. __checknull     PROC    DIST
  230.                 PUBLIC  __checknull
  231.                 ret
  232. __checknull     ENDP
  233.  
  234. ;       Restore grabbed interrupt vectors.  NO-OP on Windows.
  235.  
  236. __restorezero     PROC    DIST
  237.                 PUBLIC  __restorezero
  238.                 ret
  239. __restorezero     ENDP
  240.  
  241. ;       Exit to DOS
  242. ;
  243. ; Usage:        void _terminate(int exitcode, int quick);
  244.  
  245. __terminate     PROC    DIST
  246.                 PUBLIC  __terminate
  247.  
  248.                 mov     bp,sp
  249.                 mov     al,[bp+cPtrSize]        ; get exitcode
  250.                 mov     ah,4ch
  251.                 int     21h
  252.                 ret
  253.                 endp
  254.  
  255. WEP             proc    windows pascal far nParam:WORD
  256.                 push    si
  257.                 push    di
  258.  
  259.                 cmp     _abend, 0
  260.                 jne     @@error
  261.                 push    nParam
  262.                 call    __WEP
  263.                 push    ax
  264.                 call    __cexit         ; perform cleanup without exiting
  265.  
  266. @@unlock:
  267.  
  268. IF LDATA EQ false
  269.                 mov     ax,-1
  270.                 push    ax
  271.                 call    UNLOCKSEGMENT
  272. ENDIF
  273.  
  274.                 pop     ax
  275.                 pop     di
  276.                 pop     si
  277.                 ret
  278.  
  279. @@error:
  280.                 push    1
  281.                 jmp     @@unlock
  282.  
  283.                 endp
  284.  
  285. ;       Return default data segment in AX
  286.  
  287. __GetDGROUP     PROC    FAR
  288.                 PUBLIC  __GetDGROUP
  289.                 mov     ax, DGROUP
  290.                 ret
  291.                 endp
  292.  
  293. ;------------------------------------------------------------------
  294. ;  Loop through a startup/exit (SE) table,
  295. ;  calling functions in order of priority.
  296. ;  ES:SI is assumed to point to the beginning of the SE table
  297. ;  ES:DI is assumed to point to the end of the SE table
  298. ;  First 64 priorities are reserved by Borland
  299. ;------------------------------------------------------------------
  300. PNEAR           EQU     0
  301. PFAR            EQU     1
  302. NOTUSED         EQU     0ffh
  303.  
  304. SE              STRUC
  305. calltype        db      ?                       ; 0=near,1=far,ff=not used
  306. priority        db      ?                       ; 0=highest,ff=lowest
  307. addrlow         dw      ?
  308. addrhigh        dw      ?
  309. SE              ENDS
  310.  
  311. Initialize      proc near
  312. @@Start:        mov     ax,100h                 ;start with lowest priority
  313.                 mov     dx,di                   ;set sentinel to end of table
  314.                 mov     bx,si                   ;bx = start of table
  315.  
  316. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  317.                 je      @@EndOfTable            ;yes, exit the loop
  318.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  319.                 je      @@Next
  320.                 mov     cl, es:[bx.priority]    ;move priority to CX
  321.                 xor     ch, ch
  322.                 cmp     cx,ax                   ;check the priority
  323.                 jae     @@Next                  ;too high?  skip
  324.                 mov     ax,cx                   ;keep priority
  325.                 mov     dx,bx                   ;keep index in dx
  326. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  327.                 jmp     @@TopOfTable
  328.  
  329. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  330.                 je      @@Done                  ;yes, quit
  331.                 mov     bx,dx                   ;bx = highest priority item
  332.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  333.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  334.                 push    es                      ;save es
  335.                 je      @@NearCall
  336.  
  337. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  338.                 pop     es                      ;restore es
  339.                 jmp     short @@Start
  340.  
  341. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  342.                 pop     es                      ;restore es
  343.                 jmp     short @@Start
  344.  
  345. @@Done:         ret
  346.                 endp
  347.  
  348. Cleanup         proc near
  349. @@Start:        mov     ah,0                    ;start with highest priority
  350.                 mov     dx,di                   ;set sentinel to end of table
  351.                 mov     bx,si                   ;bx = start of table
  352.  
  353. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  354.                 je      @@EndOfTable            ;yes, exit the loop
  355.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  356.                 je      @@Next
  357.                 cmp     es:[bx.priority],ah     ;check the priority
  358.                 jb      @@Next                  ;too low?  skip
  359.                 mov     ah,es:[bx.priority]     ;keep priority
  360.                 mov     dx,bx                   ;keep index in dx
  361. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  362.                 jmp     @@TopOfTable
  363.  
  364. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  365.                 je      @@Done                  ;yes, quit
  366.                 mov     bx,dx                   ;bx = highest priority item
  367.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  368.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  369.                 push    es                      ;save es
  370.                 je      @@NearCall
  371.  
  372. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  373.                 pop     es                      ;restore es
  374.                 jmp     short @@Start
  375.  
  376. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  377.                 pop     es                      ;restore es
  378.                 jmp     short @@Start
  379.  
  380. @@Done:         ret
  381.                 endp
  382.  
  383.                 ends
  384.                 end LibEntry
  385.