home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 21.ddi / START16.PAK / C0W.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-12-02  |  13.5 KB  |  390 lines

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