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

  1. ;[]------------------------------------------------------------[]
  2. ;|      C0W.ASM -- Start Up Code For Windows                    |
  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           WINMAIN:DIST
  24. extrn           INITAPP:far
  25. extrn           INITTASK:far
  26. extrn           FATALEXIT:far
  27. extrn           WAITEVENT:far
  28. extrn           LOCKSEGMENT:far
  29. extrn           UNLOCKSEGMENT:far
  30. extrn           GETWINFLAGS:far
  31. extrn           _exit:DIST
  32. extrn           __exitbuf:DIST
  33. extrn           __exitfopen:DIST
  34. extrn           __exitopen:DIST
  35.  
  36. NULL            segment
  37.                 db      16 dup (?)
  38.                 ends
  39.  
  40. _CVTSEG         segment
  41.                 public __RealCvtVector
  42. __RealCvtVector label word
  43.                 ends
  44.  
  45. _SCNSEG         segment
  46.                 public __ScanTodVector
  47. __ScanTodVector label word
  48.                 ends
  49.  
  50. _FPSEG          segment
  51.                 public __FPVector
  52. __FPVector      dd      0
  53.                 ends
  54.  
  55. _DATA           segment
  56.                 public _errno
  57. _errno          dw      0
  58.                 public __protected
  59. __protected     dw      0
  60.                 public __8087
  61. __8087          dw      0
  62.                 public __psp
  63. __psp           dw      0
  64.                 public __hInstance
  65. __hInstance     dw      0
  66.                 public __hPrev
  67. __hPrev         dw      0
  68.                 public __pszCmdline
  69. __pszCmdline    dw      0
  70.                 public __cmdShow
  71. __cmdShow       dw      0
  72.                 public __StartTime
  73. __StartTime     dd      0
  74.                 public __version
  75. __version       label word
  76.                 public __osversion
  77. __osversion     label word
  78.                 public __osmajor
  79. __osmajor       db      0
  80.                 public __osminor
  81. __osminor       db      0
  82.                 public __osmode         ; Used for OS/2 protected mode by MS, 
  83. __osmode        db      0               ; currently set to 0 under Windows
  84.                 public __WinAllocFlag   ;Used by malloc to get additional flags
  85. __WinAllocFlag  dw      0               ;to pass to GlobalAlloc (for DLL use)
  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.  
  155. ;Determine DOS version
  156.  
  157.                 mov     ah, 30h
  158.                 int     21h
  159.                 mov     __version, ax   ; save minor and major revision
  160.  
  161. ;Determine whether we are in protected mode
  162.  
  163.                 call    GETWINFLAGS
  164.                 test    ax,1            ; WF_PMODE = 1
  165.                 jz      @@realmode      ; Note:  GETWINFLAGS returns a long,
  166.                                         ; so if WF_PMODE changed it could be 
  167.                                         ; in the high word.
  168.                 mov     __protected, 8  ; Eight is for convenience.
  169. @@realmode:
  170.  
  171. ;Test for 8087 presence
  172.  
  173.                 test    dx,04h          ; WF_8087 = 0x0400
  174.                 jz      @@no8087
  175.                 mov     __8087, 1
  176. @@no8087:
  177.  
  178. ;Call our initialization functions, including C++ static constructors.
  179.  
  180.                 mov     ax,ds
  181.                 mov     es,ax
  182.                 mov     si,offset DGROUP:InitStart      ;si = start of table
  183.                 mov     di,offset DGROUP:InitEnd        ;di = end of table
  184.                 call    Initialize
  185.  
  186. ;Set up and call WinMain
  187.  
  188.                 push    __hInstance
  189.                 push    __hPrev
  190.                 push    __psp
  191.                 push    word ptr __pszCmdline
  192.                 push    __cmdShow
  193.                 call    WINMAIN
  194.                 push    ax
  195.                 call    _exit
  196.  
  197. ;Call our termination functions, including C++ static destructors.
  198.  
  199. __exitclean     label   DIST
  200.  
  201.                 mov     ax,ds
  202.                 mov     es,ax
  203.                 mov     si,offset DGROUP:ExitStart
  204.                 mov     di,offset DGROUP:ExitEnd
  205.                 call    Cleanup
  206. IF LPROG
  207.                 call    dword ptr [__exitbuf]
  208.                 call    dword ptr [__exitfopen]
  209.                 call    dword ptr [__exitopen]
  210. ELSE
  211.                 call    word ptr [__exitbuf]
  212.                 call    word ptr [__exitfopen]
  213.                 call    word ptr [__exitopen]
  214. ENDIF
  215.  
  216. __exit          label   DIST
  217.  
  218. IF LDATA EQ false
  219.                 mov     ax,-1
  220.                 push    ax
  221.                 call    UNLOCKSEGMENT
  222. ENDIF
  223.                 mov     al,[bp+cPtrSize]
  224.                 mov     ah,4ch                  ;exit
  225.                 int     21h
  226.  
  227. @@Fail:         mov     al,0ffh
  228.                 push    ax
  229.                 call    _exit
  230.  
  231.                 mov     ah,4ch                  ;exit
  232.                 int     21h
  233.                 endp
  234.  
  235.  
  236. ;------------------------------------------------------------------
  237. ;  Loop through a startup/exit (SE) table, 
  238. ;  calling functions in order of priority.
  239. ;  ES:SI is assumed to point to the beginning of the SE table
  240. ;  ES:DI is assumed to point to the end of the SE table
  241. ;  First 64 priorities are reserved by Borland
  242. ;------------------------------------------------------------------
  243. PNEAR           EQU     0
  244. PFAR            EQU     1
  245. NOTUSED         EQU     0ffh
  246.  
  247. SE              STRUC
  248. calltype        db      ?                       ; 0=near,1=far,ff=not used
  249. priority        db      ?                       ; 0=highest,ff=lowest
  250. addrlow         dw      ?
  251. addrhigh        dw      ?
  252. SE              ENDS
  253.  
  254. Initialize      proc near
  255. @@Start:        mov     ah,0ffh                 ;start with lowest priority
  256.                 mov     dx,di                   ;set sentinel to end of table
  257.                 mov     bx,si                   ;bx = start of table
  258.  
  259. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  260.                 je      @@EndOfTable            ;yes, exit the loop
  261.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  262.                 je      @@Next
  263.                 cmp     es:[bx.priority],ah     ;check the priority
  264.                 ja      @@Next                  ;too high?  skip
  265.                 mov     ah,es:[bx.priority]     ;keep priority
  266.                 mov     dx,bx                   ;keep index in dx
  267. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  268.                 jmp     @@TopOfTable
  269.  
  270. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  271.                 je      @@Done                  ;yes, quit
  272.                 mov     bx,dx                   ;bx = highest priority item
  273.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  274.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  275.                 push    es                      ;save es
  276.                 je      @@NearCall
  277.  
  278. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  279.                 pop     es                      ;restore es
  280.                 jmp     short @@Start
  281.  
  282. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  283.                 pop     es                      ;restore es
  284.                 jmp     short @@Start
  285.  
  286. @@Done:         ret
  287.                 endp
  288.  
  289. Cleanup         proc near
  290. @@Start:        mov     ah,0                    ;start with highest priority
  291.                 mov     dx,di                   ;set sentinel to end of table
  292.                 mov     bx,si                   ;bx = start of table
  293.  
  294. @@TopOfTable:   cmp     bx,di                   ;and the end of the table?
  295.                 je      @@EndOfTable            ;yes, exit the loop
  296.                 cmp     es:[bx.calltype],NOTUSED;check the call type
  297.                 je      @@Next
  298.                 cmp     es:[bx.priority],ah     ;check the priority
  299.                 jb      @@Next                  ;too low?  skip
  300.                 mov     ah,es:[bx.priority]     ;keep priority
  301.                 mov     dx,bx                   ;keep index in dx
  302. @@Next:         add     bx,SIZE SE              ;bx = next item in table
  303.                 jmp     @@TopOfTable
  304.  
  305. @@EndOfTable:   cmp     dx,di                   ;did we exhaust the table?
  306.                 je      @@Done                  ;yes, quit
  307.                 mov     bx,dx                   ;bx = highest priority item
  308.                 cmp     es:[bx.calltype],PNEAR  ;is it near or far?
  309.                 mov     es:[bx.calltype],NOTUSED;wipe the call type
  310.                 push    es                      ;save es
  311.                 je      @@NearCall
  312.  
  313. @@FarCall:      call    DWORD PTR es:[bx.addrlow]
  314.                 pop     es                      ;restore es
  315.                 jmp     short @@Start
  316.  
  317. @@NearCall:     call    WORD PTR es:[bx.addrlow]
  318.                 pop     es                      ;restore es
  319.                 jmp     short @@Start
  320.  
  321. @@Done:         ret
  322.                 endp
  323.  
  324.                 ends
  325.  
  326.                 end Main
  327.