; 
;   Copyright 1994-2003 Free Software Foundation, Inc.
;
;   This library is free software; you can redistribute it and/or
;   modify it under the terms of the GNU Lesser General Public
;   License as published by the Free Software Foundation; either
;   version 2.1 of the License, or (at your option) any later version.
;
;   This library is distributed in the hope that it will be useful,
;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;   Lesser General Public License for more details.
;
;   You should have received a copy of the GNU Lesser General Public
;   License along with this library; if not, write to the Free Software
;   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 
;   USA
;
;   You may contact the author at:
;
;   mailto::camille@bluegrass.net
;
;   or by snail mail at:
;
;   David Lindauer
;   850 Washburn Ave Apt 99
;   Louisville, KY 40222
;
	.386

;
; Section inits
;
_TEXT	        SEGMENT DWORD PUBLIC USE32 'CODE'
	extrn	__argv_arr,__argc,__environ,__cmdline
	extrn	_main:PROC
	public  __rexit
		ENDS
cstartup        SEGMENT DWORD PUBLIC USE32 'INITDATA'
InitStart       label dword
                ENDS
_STARTUPEND_       SEGMENT DWORD PUBLIC USE32 'INITDATA'
InitEnd         label dword
                ENDS
crundown        SEGMENT DWORD PUBLIC USE32 'EXITDATA'
ExitStart       label dword
                ENDS
_RUNDOWNEND_       SEGMENT DWORD PUBLIC USE32 'EXITDATA'
ExitEnd         label dword
                ENDS
_CPP_           SEGMENT DWORD PUBLIC USE32 'CPPDATA'
CppStart        label dword
		dd	cpproutine
                ENDS
_CPPEND_        SEGMENT DWORD PUBLIC USE32 'CPPDATA'
CppEnd          label dword
		ENDS
_DATA   	SEGMENT DWORD PUBLIC USE32 'DATA'
		db	"Copyright (c) 1996 LADsoft C runtime library (i386)"
                ENDS
_BSS	        SEGMENT DWORD PUBLIC USE32 'BSS'
BssStart	label dword
                ENDS
_BSSEND	        SEGMENT DWORD PUBLIC USE32 'BSS'
BssEnd		label dword
                ENDS

DGROUP group cstartup,_STARTUPEND_,crundown,_RUNDOWNEND_,_CPP_,_CPPEND_,_DATA,_BSS,_BSSEND
_TEXT		SEGMENT
		assume cs:_TEXT,ds:DGROUP
;
; Main program
;
start:
;
; Assumes ESP is set
;
; Set up dummy command line
;
; Clear BSS
;
		mov	edi,offset BssStart
		mov	ecx,offset BssEnd
		sub	ecx,edi
		sub	eax,eax
		cld
		rep	stosb
;
; Execute startup routines
;
		mov	ecx,offset InitStart
		mov	edx,offset InitEnd
		call	sexproc
;
; Call main
;
		push	__environ
		push	__argv_arr
		push	__argc
		call	_main
		add	esp,12
;
; exit/abort comes here
;
__rexit:
;
; Execute rundown routines
;
		push	eax
		mov	ecx,offset ExitStart
		mov	edx,offset ExitEnd
		call	sexproc
		pop	eax
		ret			; exit to os
;
; Handle startup/rundown routines
;
sexproc:
		cmp	ecx,edx		
		jz	short sexpdone	
		mov	edi,ecx
		sub	ebx,ebx
		sub	eax,eax
spl2:
		cmp	edi,edx
		jz	short spo
		test	dword ptr [edi+4],-1
		jz	short spl3
		cmp	eax,[edi+4]
		jnc	short spl3
		mov	ebx,edi
		mov	eax,[edi+4]
spl3:
		add	edi,8
		jmp	spl2
spo:
		or	ebx,ebx
		jz	short sexpdone
		mov	dword ptr [ebx+4],0
		mov	ebx,[ebx]
		push	edx
		push	ecx
		call	ebx
		pop	ecx
		pop	edx
		jmp	sexproc
sexpdone:
		ret

; This is called as the first thing from the C++ main routine
cpproutine:
		ret
_TEXT		ends
		end 