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