home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c495 / watcm951.arj / STARTUP.386 / CSTART3S.ASM < prev   
Encoding:
Assembly Source File  |  1993-02-16  |  11.6 KB  |  552 lines

  1.  
  2.  
  3.      
  4.      
  5.      
  6.      
  7.      
  8.      
  9.      
  10.  
  11.    
  12.    
  13.    
  14.    
  15.    
  16.    
  17.    
  18.    
  19.    
  20.    
  21.    
  22.    
  23.    
  24.    
  25.    
  26.    
  27.    
  28.    
  29.    
  30.    
  31.    
  32.    
  33.    
  34.    
  35.    
  36.    
  37.    
  38.    
  39.    
  40.    
  41.    
  42.    
  43.    
  44.    
  45.    
  46.    
  47.    
  48.    
  49.    
  50.    
  51.    
  52.    
  53.    
  54.    
  55.    
  56.    
  57.    
  58.    
  59.    
  60.  
  61.    
  62.    
  63.    
  64.    
  65.    
  66.    
  67.    
  68.    
  69.    
  70.    
  71.    
  72.    
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.    
  93.    
  94.    
  95.    
  96.    
  97.    
  98.    
  99.    
  100.    
  101.    
  102.    
  103.    
  104.    
  105.    
  106.    
  107.    
  108.    
  109.    
  110.    
  111.    
  112.    
  113.    
  114.    
  115.    
  116.    
  117.    
  118.    
  119.    
  120.    
  121.    
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.    
  130.    
  131.    
  132.    
  133.    
  134.    
  135.    
  136.    
  137.    
  138.    
  139.    
  140.    
  141.    
  142.    
  143.    
  144.    
  145.    
  146.    
  147.    
  148.    
  149.    
  150.    
  151.    
  152.  
  153. ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  154. ;%  Copyright (C) 1988-1993 by WATCOM International Corp. All    %
  155. ;%  rights reserved. No part of this software may be reproduced %
  156. ;%  in any form or by any means - graphic, electronic or    %
  157. ;%  mechanical, including photocopying, recording, taping    %
  158. ;%  or information storage and retrieval systems - except    %
  159. ;%  with the written permission of WATCOM International Corp.    %
  160. ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  161. ;
  162. ; startup code for WATCOM C/C++32 Version 9.5
  163. ;
  164. ;    This must be assembled using the following command:
  165. ;        386asm cstart
  166. ;    To create startup code for Ergo's DPMI DOS extender
  167. ;        386asm cstart -define ERGO_DPMI
  168. ;
  169. ;   NOTE: All C library data should be defined in CRWDATA.ASM -- That way
  170. ;      it's also available to ADS applications (who use ADSSTART.ASM).
  171. ;
  172.     name    cstart
  173.  
  174. .387
  175. .386p
  176.     assume    nothing
  177.  
  178.     extrn    __CMain     : near
  179.     extrn    __InitRtns    : near
  180.     extrn    __FiniRtns    : near
  181.     extrn    __DOSseg__    : near
  182. ifdef ERGO_DPMI
  183.     extrn    _InitAPIExtensions : near
  184. endif
  185.  
  186.     extrn    _edata        : byte        ; end of DATA (start of BSS)
  187.     extrn    _end        : byte        ; end of BSS (start of STACK)
  188.  
  189.     extrn     _dynend    : dword
  190.     extrn     _curbrk    : dword
  191.     extrn     _psp        : word
  192.     extrn     _osmajor    : byte
  193.     extrn     _osminor    : byte
  194.     extrn     _STACKLOW    : dword
  195.     extrn     _STACKTOP    : dword
  196.     extrn     _child     : dword
  197.     extrn    __no87        : word
  198.     extrn     _Extender    : byte
  199.     extrn     _Envptr    : dword
  200.     extrn     _Envseg    : word
  201.     extrn    __FPE_handler    : dword
  202.     extrn  ___FPE_handler    : dword
  203.     extrn     _LpCmdLine    : dword
  204.     extrn     _LpPgmName    : dword
  205.  
  206. DGROUP group _NULL,_AFTERNULL,CONST,_DATA,DATA,XIB,XI,XIE,YIB,YI,YIE,_BSS,STACK
  207.  
  208. ; this guarantees that no function pointer will equal NULL
  209. ; (WLINK will keep segment 'BEGTEXT' in front)
  210. ; This segment must be at least 4 bytes in size to avoid confusing the
  211. ; signal function.
  212.  
  213. BEGTEXT  segment use32 para public 'CODE'
  214.     assume    cs:BEGTEXT
  215. forever    label    near
  216.     int    3h
  217.     jmp    short forever
  218. ___begtext label byte
  219.     nop    ;3
  220.     nop    ;4
  221.     nop    ;5
  222.     nop    ;6
  223.     nop    ;7
  224.     nop    ;8
  225.     nop    ;9
  226.     nop    ;A
  227.     nop    ;B
  228.     nop    ;C
  229.     nop    ;D
  230.     nop    ;E
  231.     nop    ;F
  232.     public ___begtext
  233.     assume    cs:nothing
  234. BEGTEXT  ends
  235.  
  236. _TEXT    segment use32 dword public 'CODE'
  237.  
  238.     assume    ds:DGROUP
  239.  
  240. _NULL    segment para public 'BEGDATA'
  241. __nullarea label word
  242.     db    01h,01h,01h,00h
  243.     public    __nullarea
  244. _NULL    ends
  245.  
  246. _AFTERNULL segment word public 'BEGDATA'
  247. _AFTERNULL ends
  248.  
  249. CONST    segment word public 'DATA'
  250. CONST    ends
  251.  
  252. XIB    segment word public 'DATA'
  253. XIB    ends
  254. XI    segment word public 'DATA'
  255. XI    ends
  256. XIE    segment word public 'DATA'
  257. XIE    ends
  258.  
  259. YIB    segment word public 'DATA'
  260. YIB    ends
  261. YI    segment word public 'DATA'
  262. YI    ends
  263. YIE    segment word public 'DATA'
  264. YIE    ends
  265.  
  266.  
  267. _DATA     segment dword public 'DATA'
  268. X_ERGO        equ    0
  269. X_RATIONAL    equ    1
  270. X_PHARLAP_V2    equ    2
  271. X_PHARLAP_V3    equ    3
  272. X_PHARLAP_V4    equ    4
  273. X_PHARLAP_V5    equ    5
  274. X_INTEL     equ    9
  275. __GDAptr   dd 0         ; IGC and Intel Code Builder GDA address
  276. __D16Infoseg   dw    0020h    ; DOS/4G kernel segment
  277. __x386_zero_base_selector dw 0    ; base 0 selector for X-32VM
  278.  
  279.     public    __GDAptr
  280.     public    __D16Infoseg
  281.     public    __x386_zero_base_selector
  282. ifdef ERGO_DPMI
  283. __MEMHandle    dd    0    ; handle for linear base of image
  284.     public    __MEMHandle
  285. endif
  286. _DATA     ends
  287.  
  288.  
  289. DATA    segment word public 'DATA'
  290. DATA    ends
  291.  
  292. _BSS          segment word public 'BSS'
  293. _BSS          ends
  294.  
  295. STACK_SIZE    equ    1000h
  296.  
  297. STACK    segment para stack 'STACK'
  298.     db    (STACK_SIZE) dup(?)
  299. STACK    ends
  300.  
  301.  
  302.     assume    nothing
  303.     public    _cstart_
  304.     public     __exit
  305.  
  306.     assume    cs:_TEXT
  307.  
  308. _cstart_ proc near
  309.     jmp    short around
  310.  
  311. ;
  312. ; copyright message
  313. ;
  314.     db    "WATCOM C/C++32 Run-Time system. "
  315.         db      "(c) Copyright by WATCOM International Corp. 1988-1993."
  316.     db    " All rights reserved."
  317.     align    4
  318.     dd    ___begtext    ; make sure dead code elimination
  319.                 ; doesn't kill BEGTEXT
  320. __saved_DS  dw    0        ; save area for DS for interrupt routines
  321. ;
  322. ; miscellaneous code-segment messages
  323. ;
  324. ConsoleName    db    "con",00h
  325.  
  326. around: sti                ; enable interrupts
  327.  
  328.     assume    ds:DGROUP
  329.  
  330. PSP_SEG equ    24h
  331. ENV_SEG equ    2ch
  332.  
  333.     and    esp,0fffffffch        ; make sure stack is on a 4 byte bdry
  334.     mov    ebx,esp         ; get sp
  335.     mov     _STACKTOP,ebx        ; set stack top
  336.     mov     _curbrk,ebx        ; set first available memory location
  337. ifdef ERGO_DPMI
  338.     mov    ax,es            ; get segment address of PSP
  339.     mov     _psp,ax        ; save segment address of PSP
  340.     mov    __MEMHandle,esi        ;
  341.     call    _InitAPIExtensions    ;
  342.     mov    al,X_ERGO        ; indicate Ergo DOS Extender
  343.     mov    bx,ds            ; force bx = DGROUP
  344.     sub    esi,esi         ; offset 0 for environment strings
  345.     mov    cx,es:[02Ch]        ; get segment for environment strings
  346.     mov    edi,81H         ; DOS command buffer _psp:edi
  347. else
  348.     mov    ax,PSP_SEG        ; get segment address of PSP
  349.     mov     _psp,ax        ; save segment address of PSP
  350. ;
  351. ;    get DOS & Extender version number
  352. ;
  353.     mov    ebx,'PHAR'        ; set ebx to 0
  354.     sub    eax,eax         ; set eax to 0
  355.     mov    ah,30h
  356.     int    21h            ; modifies eax,ebx,ecx,edx
  357.     mov     _osmajor,al
  358.     mov     _osminor,ah
  359.     mov    ecx,eax         ; remember DOS version number
  360.     sub    esi,esi         ; offset 0 for environment strings
  361.     mov    edi,81H         ; DOS command buffer es:edi
  362.     shr    eax,16            ; get top 16 bits of eax
  363.     cmp    ax,'DX'         ; if top 16 bits = "DX"
  364.     jne    not_pharlap        ; then its pharlap
  365.     sub    bl,'0'            ; - save major version number
  366.     mov    al,bl            ; - (was in ascii)
  367.     mov    bx,ds            ; - get value of Phar Lap data segment
  368.     mov    cx,ENV_SEG        ; - PharLap environment segment
  369.     jmp    short know_ext1     ; else
  370. not_pharlap:                ; - see if Intel Code Builder
  371.     cmp    ax,'BC'         ; - if Intel Code Builder
  372.     jne    not_Intel        ; - ... then
  373. GDA_PSPA equ    16            ; - offset into GDA to PSP address
  374. GDA_LDPT equ    28            ; - offset into GDA to load address
  375.     mov    __GDAptr,edx        ; - save address of GDA
  376.     mov    esi,edx         ; - get address of GDA
  377.     mov    edx,GDA_LDPT[esi]    ; - get application load point address
  378.     mov    ebx,esp         ; - calc amount of memory to keep
  379.     sub    ebx,edx         ; - ...
  380.     mov    ah,4Ah            ; - resize to minimum memory
  381.     int    21h            ; - ...
  382.     mov    bx,ds            ; - just use ds (FLAT model)
  383.     mov     _psp,ds        ; - save segment address of PSP
  384.     mov    eax,GDA_PSPA[esi]    ; - get address of PSP
  385.     add    edi,eax         ; - add address of PSP
  386.     sub    esi,esi         ; - zero esi
  387.     mov    si,02ch[eax]        ; - get environment segment into si
  388.     shl    esi,4            ; - convert to flat address
  389.     mov    cx,ds            ; - segment to access environment area
  390.     mov    al,X_INTEL        ; - indicate Intel Code Builder
  391. know_ext1:jmp    short know_extender    ; else
  392. not_Intel:                ; -
  393.     mov    dx,78h            ; - see if Rational DOS/4G
  394.     mov    ax,0FF00h        ; - ...
  395.     int    21h            ; - ...
  396.     cmp    al,0            ; - ...
  397.     je    short not_DOS4G     ; - quit if not Rational DOS/4G
  398.     mov    ax,gs            ; - get segment address of kernel
  399.     cmp    ax,0            ; - if not zero
  400.     je    short rat9        ; - then
  401.     mov    __D16Infoseg,ax     ; - - remember it
  402. rat9:                    ; - endif
  403.     mov    al,X_RATIONAL        ; - indicate Rational 32-bit Extender
  404.     mov    bx,ds            ; - just use ds (FLAT model)
  405.     mov     _psp,es        ; - save segment address of PSP
  406.     mov    cx,es:[02ch]        ; - get environment segment into cx
  407.     jmp    short know_extender    ; else
  408. not_DOS4G:                ; -
  409.     mov    dx,ds            ; - save ds
  410.     mov    cx,PSP_SEG        ; - get PSP segment descriptor
  411.     mov    ds,cx            ; - ... into ds
  412.     mov    cx,ds:[02ch]        ; - get environment segment into cx
  413.     mov    ds,dx            ; - restore ds
  414.     mov    bx,17h            ; - get writeable code segment for Ergo
  415.     mov    al,X_ERGO        ; - indicate Ergo OS/386
  416. know_extender:                ; endif
  417. endif
  418.     mov     _Extender,al        ; record extender type
  419.     mov    es,bx            ; get access to code segment
  420.     mov    es:__saved_DS,ds    ; save DS value
  421.     mov     _Envptr,esi        ; save address of environment strings
  422.     mov     _Envseg,cx        ; save segment of environment area
  423.     push    esi            ; save address of environment strings
  424. ;
  425. ;    copy command line into bottom of stack
  426. ;
  427.     mov    es, _psp        ; point to PSP
  428.     mov    edx,offset DGROUP:_end
  429.     add    edx,0FH
  430.     and    dl,0F0H
  431.     sub    ecx,ecx
  432.     mov    cl,es:[edi-1]        ; get length of command
  433.     cld                ; set direction forward
  434.     mov    al,' '
  435.     rep    scasb
  436.     lea    esi,-1[edi]
  437.     mov    edi,edx
  438.     mov    bx,es
  439.     mov    dx,ds
  440.     mov    ds,bx
  441.     mov    es,dx            ; es:edi is destination
  442.     je    noparm
  443.     inc    ecx
  444.     rep    movsb
  445. noparm: sub    al,al
  446.     stosb                ; store NULLCHAR
  447.     stosb                ; assume no pgm name
  448.     pop    esi            ; restore address of environment strings
  449.     dec    edi            ; back up pointer 1
  450.     push    edi            ; save pointer to pgm name
  451.     push    edx            ; save ds(stored in dx)
  452.     mov    ds,es: _Envseg        ; get segment addr of environment area
  453.     sub    ebp,ebp         ; assume "no87" env. var. not present
  454. L1:    mov    eax,[esi]        ; get first 4 characters
  455.     or    eax,20202020h        ; map to lower case
  456.     cmp    eax,'78on'        ; check for "no87"
  457.     jne    short L2        ; skip if not "no87"
  458.     cmp    byte ptr 4[esi],'='    ; make sure next char is "="
  459.     jne    short L2        ; no
  460.     inc    ebp            ; - indicate "no87" was present
  461. L2:    cmp    byte ptr [esi],0    ; end of string ?
  462.     lodsb
  463.     jne    L2            ; until end of string
  464.     cmp    byte ptr [esi],0    ; end of all strings ?
  465.     jne    L1            ; if not, then skip next string
  466.     lodsb
  467.     inc    esi            ; point to program name
  468.     inc    esi            ; . . .
  469. ;
  470. ;    copy the program name into bottom of stack
  471. ;
  472. L3:    cmp    byte ptr [esi],0    ; end of pgm name ?
  473.     movsb                ; copy a byte
  474.     jne    L3            ; until end of pgm name
  475.     pop    ds            ; restore ds
  476.     pop    esi            ; restore address of pgm name
  477.     mov    ebx,esp         ; end of stack in data segment
  478.  
  479.     assume    ds:DGROUP
  480.     mov    __no87,bp        ; set state of "no87" enironment var
  481.     mov     _STACKLOW,edi        ; save low address of stack
  482.     mov     _dynend,ebx        ; set top of dynamic memory area
  483.  
  484.     mov    ecx,offset DGROUP:_end    ; end of _BSS segment (start of STACK)
  485.     mov    edi,offset DGROUP:_edata; start of _BSS segment
  486.     sub    ecx,edi         ; calc # of bytes in _BSS segment
  487.     mov    dl,cl            ; save bottom 2 bits of count in edx
  488.     shr    ecx,2            ; calc # of dwords
  489.     sub    eax,eax         ; zero the _BSS segment
  490.     rep    stosd            ; ...
  491.     mov    cl,dl            ; get bottom 2 bits of count
  492.     and    cl,3            ; ...
  493.     rep    stosb            ; ...
  494.  
  495.     mov    eax,offset DGROUP:_end    ; cmd buffer pointed at by EAX
  496.     add    eax,0FH
  497.     and    al,0F0H
  498.     mov     _LpCmdLine,eax        ; save command line address
  499.     mov     _LpPgmName,esi        ; save program name address
  500.     call    __InitRtns        ; call initializer routines
  501.     sub    ebp,ebp            ; ebp=0 indicate end of ebp chain
  502.     call    __CMain
  503. _cstart_ endp
  504.  
  505. ;    don't touch AL in __exit_, it has the return code
  506.  
  507.  __exit  proc near
  508.     jmp    short    ok
  509.  
  510.     public     __exit_with_msg
  511.  
  512. ; input: EAX - pointer to message to print
  513. ;     EDX - exit code
  514.  
  515.  __exit_with_msg:
  516.     push    edx            ; save return code
  517.     push    eax            ; save address of msg
  518.     mov    edx,offset ConsoleName
  519.     mov    ax,03d01h        ; write-only access to screen
  520.     int    021h
  521.     mov    bx,ax            ; get file handle
  522.     pop    edx            ; restore address of msg
  523.     mov    esi,edx         ; get address of msg
  524.     cld                ; make sure direction forward
  525. L4:    lodsb                ; get char
  526.     cmp    al,0            ; end of string?
  527.     jne    L4            ; no
  528.     mov    ecx,esi         ; calc length of string
  529.     sub    ecx,edx         ; . . .
  530.     dec    ecx            ; . . .
  531.     mov    ah,040h         ; write out the string
  532.     int    021h            ; . . .
  533.     pop    eax            ; restore return code
  534. ok:
  535.     push    eax            ; save return code
  536.     call    __FiniRtns        ; call finializer routines
  537.     pop    eax            ; restore return code
  538.     mov    ah,04cH         ; DOS call to exit with return code
  539.     int    021h            ; back to DOS
  540.  __exit  endp
  541.  
  542.     public    __GETDS
  543.     align    4
  544. __GETDS proc    near
  545.     mov    ds,cs:__saved_DS    ; load saved DS value
  546.     ret                ; return
  547. __GETDS endp
  548.  
  549. _TEXT    ends
  550.  
  551.     end    _cstart_
  552.