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

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