	.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 