home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 1.ddi / STARTUP.386 / CSTART3S.ASM < prev   
Encoding:
Assembly Source File  |  1992-05-28  |  10.6 KB  |  504 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. ;%     Copyright (C) 1991-1992, by WATCOM Systems Inc. All rights%
  138. ;%     reserved. No part of this software may be reproduced       %
  139. ;%     in any form or by any means - graphic, electronic or       %
  140. ;%     mechanical, including photocopying, recording, taping       %
  141. ;%     or information storage and retrieval systems - except       %
  142. ;%     with the written permission of WATCOM Systems Inc.       %
  143. ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  144. ;
  145. ; startup code for WATCOM C 386 Version 9.0
  146. ;
  147. ;    This must be assembled using the following command:
  148. ;        386asm cstart
  149. ;
  150. ;   NOTE: All C library data should be defined in CRWDATA.ASM -- That way
  151. ;      it's also available to ADS applications (who use ADSSTART.ASM).
  152. ;
  153.     name    cstart
  154.  
  155. .387
  156. .386p
  157.     assume    nothing
  158.  
  159.     extrn    __CMain     : near
  160.     extrn    __InitRtns    : near
  161.     extrn    __FiniRtns    : near
  162.     extrn    __DOSseg__    : near
  163.  
  164.     extrn    _edata        : byte        ; end of DATA (start of BSS)
  165.     extrn    _end        : byte        ; end of BSS (start of STACK)
  166.  
  167.     extrn     _dynend    : dword
  168.     extrn     _curbrk    : dword
  169.     extrn     _psp        : word
  170.     extrn     _osmajor    : byte
  171.     extrn     _osminor    : byte
  172.     extrn     _STACKLOW    : dword
  173.     extrn     _STACKTOP    : dword
  174.     extrn     _child     : dword
  175.     extrn    __no87        : word
  176.     extrn     _Extender    : byte
  177.     extrn     _Envptr    : dword
  178.     extrn     _Envseg    : word
  179.     extrn    __FPE_handler    : dword
  180.     extrn  ___FPE_handler    : dword
  181.     extrn     _LpCmdLine    : dword
  182.     extrn     _LpPgmName    : dword
  183.  
  184. DGROUP group _NULL,_AFTERNULL,CONST,_DATA,DATA,XIB,XI,XIE,YIB,YI,YIE,_BSS,STACK
  185.  
  186. ; this guarantees that no function pointer will equal NULL
  187. ; (WLINK will keep segment 'BEGTEXT' in front)
  188. ; This segment must be at least 4 bytes in size to avoid confusing the
  189. ; signal function.
  190.  
  191. BEGTEXT  segment use32 para public 'CODE'
  192.     assume    cs:BEGTEXT
  193. forever: jmp    short forever
  194.     nop    ;2
  195.     nop    ;3
  196.     nop    ;4
  197.     nop    ;5
  198.     nop    ;6
  199.     nop    ;7
  200.     nop    ;8
  201.     nop    ;9
  202.     nop    ;A
  203.     nop    ;B
  204.     nop    ;C
  205.     nop    ;D
  206.     nop    ;E
  207.     nop    ;F
  208.     assume    cs:nothing
  209. BEGTEXT  ends
  210.  
  211. _TEXT    segment use32 word public 'CODE'
  212.  
  213.     assume    ds:DGROUP
  214.  
  215. _NULL    segment para public 'BEGDATA'
  216. __nullarea label word
  217.     db    01h,01h,01h,00h
  218.     public    __nullarea
  219. _NULL    ends
  220.  
  221. _AFTERNULL segment word public 'BEGDATA'
  222. _AFTERNULL ends
  223.  
  224. CONST    segment word public 'DATA'
  225. CONST    ends
  226.  
  227. XIB    segment word public 'DATA'
  228. XIB    ends
  229. XI    segment word public 'DATA'
  230. XI    ends
  231. XIE    segment word public 'DATA'
  232. XIE    ends
  233.  
  234. YIB    segment word public 'DATA'
  235. YIB    ends
  236. YI    segment word public 'DATA'
  237. YI    ends
  238. YIE    segment word public 'DATA'
  239. YIE    ends
  240.  
  241.  
  242. _DATA     segment dword public 'DATA'
  243. X_ERGO        equ    0
  244. X_RATIONAL    equ    1
  245. X_PHARLAP_V2    equ    2
  246. X_PHARLAP_V3    equ    3
  247. X_PHARLAP_V4    equ    4
  248. X_INTEL     equ    5
  249. __GDAptr   dd 0         ; IGC and Intel Code Builder GDA address
  250. __D16Infoseg   dw    0020h    ; DOS/4G kernel segment
  251.  
  252.     public    __GDAptr
  253.     public    __D16Infoseg
  254. _DATA     ends
  255.  
  256.  
  257. DATA    segment word public 'DATA'
  258. DATA    ends
  259.  
  260. _BSS          segment word public 'BSS'
  261. _BSS          ends
  262.  
  263. STACK_SIZE    equ    1000h
  264.  
  265. STACK    segment para stack 'STACK'
  266.     db    (STACK_SIZE) dup(?)
  267. STACK    ends
  268.  
  269.  
  270.     assume    nothing
  271.     public    _cstart_
  272.     public     __exit
  273.  
  274.     assume    cs:_TEXT
  275.  
  276. _cstart_ proc near
  277.     jmp    short around
  278.  
  279. ;
  280. ; copyright message
  281. ;
  282.     db    "WATCOM C 386 Run-Time system. "
  283.     db    "(c) Copyright by WATCOM Systems Inc. 1989, 1992."
  284.     db    " All rights reserved."
  285. ;
  286. ; miscellaneous code-segment messages
  287. ;
  288. ConsoleName    db    "con",00h
  289. __saved_DS  dw    0        ; save area for DS for interrupt routines
  290.  
  291. around: sti                ; enable interrupts
  292.  
  293.     assume    ds:DGROUP
  294.  
  295. PSP_SEG equ    24h
  296. ENV_SEG equ    2ch
  297.  
  298.     and    esp,0fffffffch        ; make sure stack is on a 4 byte bdry
  299.     mov    ebx,esp         ; get sp
  300.     mov     _STACKTOP,ebx        ; set stack top
  301.     mov     _curbrk,ebx        ; set first available memory location
  302.     mov    ax,PSP_SEG        ; get segment address of PSP
  303.     mov     _psp,ax        ; save segment address of PSP
  304. ;
  305. ;    get DOS & Extender version number
  306. ;
  307.     mov    ebx,'PHAR'        ; set ebx to 0
  308.     sub    eax,eax         ; set eax to 0
  309.     mov    ah,30h
  310.     int    21h            ; modifies eax,ebx,ecx,edx
  311.     mov     _osmajor,al
  312.     mov     _osminor,ah
  313.     mov    ecx,eax         ; remember DOS version number
  314.     sub    esi,esi         ; offset 0 for environment strings
  315.     mov    edi,81H         ; DOS command buffer es:edi
  316.     shr    eax,16            ; get top 16 bits of eax
  317.     cmp    ax,'DX'         ; if top 16 bits = "DX"
  318.     jne    not_pharlap        ; then its pharlap
  319.     sub    bl,'0'            ; - save major version number
  320.     mov    al,bl            ; - (was in ascii)
  321.     mov    bx,ds            ; - get value of Phar Lap data segment
  322.     mov    cx,ENV_SEG        ; - PharLap environment segment
  323.     jmp    short know_ext1     ; else
  324. not_pharlap:                ; - see if Intel Code Builder
  325.     cmp    ax,'BC'         ; - if Intel Code Builder
  326.     jne    not_Intel        ; - ... then
  327. GDA_PSPA equ    16            ; - offset into GDA to PSP address
  328. GDA_LDPT equ    28            ; - offset into GDA to load address
  329.     mov    __GDAptr,edx        ; - save address of GDA
  330.     mov    esi,edx         ; - get address of GDA
  331.     mov    edx,GDA_LDPT[esi]    ; - get application load point address
  332.     mov    ebx,esp         ; - calc amount of memory to keep
  333.     sub    ebx,edx         ; - ...
  334.     mov    ah,4Ah            ; - resize to minimum memory
  335.     int    21h            ; - ...
  336.     mov    bx,ds            ; - just use ds (FLAT model)
  337.     mov     _psp,ds        ; - save segment address of PSP
  338.     mov    eax,GDA_PSPA[esi]    ; - get address of PSP
  339.     add    edi,eax         ; - add address of PSP
  340.     sub    esi,esi         ; - zero esi
  341.     mov    si,02ch[eax]        ; - get environment segment into si
  342.     shl    esi,4            ; - convert to flat address
  343.     mov    cx,ds            ; - segment to access environment area
  344.     mov    al,X_INTEL        ; - indicate Intel Code Builder
  345. know_ext1:jmp    short know_extender    ; else
  346. not_Intel:                ; -
  347.     mov    dx,78h            ; - see if Rational DOS/4G
  348.     mov    ax,0FF00h        ; - ...
  349.     int    21h            ; - ...
  350.     cmp    al,0            ; - ...
  351.     je    short not_DOS4G     ; - quit if not Rational DOS/4G
  352.     mov    ax,gs            ; - get segment address of kernel
  353.     cmp    ax,0            ; - if not zero
  354.     je    short rat9        ; - then
  355.     mov    __D16Infoseg,ax     ; - - remember it
  356. rat9:                    ; - endif
  357.     mov    al,X_RATIONAL        ; - indicate Rational 32-bit Extender
  358.     mov    bx,ds            ; - just use ds (FLAT model)
  359.     mov     _psp,es        ; - save segment address of PSP
  360.     mov    cx,es:[02ch]        ; - get environment segment into cx
  361.     jmp    short know_extender    ; else
  362. not_DOS4G:                ; -
  363.     mov    dx,ds            ; - save ds
  364.     mov    cx,PSP_SEG        ; - get PSP segment descriptor
  365.     mov    ds,cx            ; - ... into ds
  366.     mov    cx,ds:[02ch]        ; - get environment segment into cx
  367.     mov    ds,dx            ; - restore ds
  368.     mov    bx,17h            ; - get writeable code segment for Ergo
  369.     mov    al,X_ERGO        ; - indicate Ergo OS/386
  370. know_extender:                ; endif
  371.     mov     _Extender,al        ; record extender type
  372.     mov    es,bx            ; get access to code segment
  373.     mov    es:__saved_DS,ds    ; save DS value
  374.     mov     _Envptr,esi        ; save address of environment strings
  375.     mov     _Envseg,cx        ; save segment of environment area
  376.     push    esi            ; save address of environment strings
  377. ;
  378. ;    copy command line into bottom of stack
  379. ;
  380.     mov    es, _psp        ; point to PSP
  381.     mov    edx,offset DGROUP:_end
  382.     add    edx,0FH
  383.     and    dl,0F0H
  384.     sub    ecx,ecx
  385.     mov    cl,es:[edi-1]        ; get length of command
  386.     cld                ; set direction forward
  387.     mov    al,' '
  388.     rep    scasb
  389.     lea    esi,-1[edi]
  390.     mov    edi,edx
  391.     mov    bx,es
  392.     mov    dx,ds
  393.     mov    ds,bx
  394.     mov    es,dx            ; es:edi is destination
  395.     je    noparm
  396.     inc    ecx
  397.     rep    movsb
  398. noparm: sub    al,al
  399.     stosb                ; store NULLCHAR
  400.     stosb                ; assume no pgm name
  401.     pop    esi            ; restore address of environment strings
  402.     dec    edi            ; back up pointer 1
  403.     push    edi            ; save pointer to pgm name
  404.     push    edx            ; save ds(stored in dx)
  405.     mov    ds,es: _Envseg        ; get segment addr of environment area
  406.     sub    ebp,ebp         ; assume "no87" env. var. not present
  407. L1:    mov    eax,[esi]        ; get first 4 characters
  408.     or    eax,20202020h        ; map to lower case
  409.     cmp    eax,'78on'        ; check for "no87"
  410.     jne    short L2        ; skip if not "no87"
  411.     cmp    byte ptr 4[esi],'='    ; make sure next char is "="
  412.     jne    short L2        ; no
  413.     inc    ebp            ; - indicate "no87" was present
  414. L2:    cmp    byte ptr [esi],0    ; end of string ?
  415.     lodsb
  416.     jne    L2            ; until end of string
  417.     cmp    byte ptr [esi],0    ; end of all strings ?
  418.     jne    L1            ; if not, then skip next string
  419.     lodsb
  420.     inc    esi            ; point to program name
  421.     inc    esi            ; . . .
  422. ;
  423. ;    copy the program name into bottom of stack
  424. ;
  425. L3:    cmp    byte ptr [esi],0    ; end of pgm name ?
  426.     movsb                ; copy a byte
  427.     jne    L3            ; until end of pgm name
  428.     pop    ds            ; restore ds
  429.     pop    esi            ; restore address of pgm name
  430.     mov    ebx,esp         ; end of stack in data segment
  431.  
  432.     assume    ds:DGROUP
  433.     mov    __no87,bp        ; set state of "no87" enironment var
  434.     mov     _STACKLOW,edi        ; save low address of stack
  435.     mov     _dynend,ebx        ; set top of dynamic memory area
  436.  
  437.     mov    ecx,offset DGROUP:_end    ; end of _BSS segment (start of STACK)
  438.     mov    edi,offset DGROUP:_edata; start of _BSS segment
  439.     sub    ecx,edi         ; calc # of bytes in _BSS segment
  440.     mov    dl,cl            ; save bottom 2 bits of count in edx
  441.     shr    ecx,2            ; calc # of dwords
  442.     sub    eax,eax         ; zero the _BSS segment
  443.     rep    stosd            ; ...
  444.     mov    cl,dl            ; get bottom 2 bits of count
  445.     and    cl,3            ; ...
  446.     rep    stosb            ; ...
  447.  
  448.     mov    eax,offset DGROUP:_end    ; cmd buffer pointed at by EAX
  449.     add    eax,0FH
  450.     and    al,0F0H
  451.     mov     _LpCmdLine,eax        ; save command line address
  452.     mov     _LpPgmName,esi        ; save program name address
  453.     call    __InitRtns        ; call initializer routines
  454.     sub    ebp,ebp            ; ebp=0 indicate end of ebp chain
  455.     call    __CMain
  456. _cstart_ endp
  457.  
  458. ;    don't touch AL in __exit_, it has the return code
  459.  
  460.  __exit  proc near
  461.     jmp    short    ok
  462.  
  463.     public     __exit_with_msg
  464.  
  465. ; input: EAX - pointer to message to print
  466. ;     EDX - exit code
  467.  
  468.  __exit_with_msg:
  469.     push    edx            ; save return code
  470.     push    eax            ; save address of msg
  471.     mov    edx,offset ConsoleName
  472.     mov    ax,03d01h        ; write-only access to screen
  473.     int    021h
  474.     mov    bx,ax            ; get file handle
  475.     pop    edx            ; restore address of msg
  476.     mov    esi,edx         ; get address of msg
  477.     cld                ; make sure direction forward
  478. L4:    lodsb                ; get char
  479.     cmp    al,0            ; end of string?
  480.     jne    L4            ; no
  481.     mov    ecx,esi         ; calc length of string
  482.     sub    ecx,edx         ; . . .
  483.     dec    ecx            ; . . .
  484.     mov    ah,040h         ; write out the string
  485.     int    021h            ; . . .
  486.     pop    eax            ; restore return code
  487. ok:
  488.     push    eax            ; save return code
  489.     call    __FiniRtns        ; call finializer routines
  490.     pop    eax            ; restore return code
  491.     mov    ah,04cH         ; DOS call to exit with return code
  492.     int    021h            ; back to DOS
  493.  __exit  endp
  494.  
  495.     public    __GETDS
  496. __GETDS proc    near
  497.     mov    ds,cs:__saved_DS    ; load saved DS value
  498.     ret                ; return
  499. __GETDS endp
  500.  
  501. _TEXT    ends
  502.  
  503.     end    _cstart_
  504.