	include	masmdefs.hsm
	include model.hsm
	include	tinymacs.hsm

codedef WPACK
datadef

cstart  WPACK

	cextrn	install_wpack
	cextrn	end_wpack
public	__acrtused		; trick to force in startup
	__acrtused = 9876h	; funny value not easily matched in SYMDEB

cproc   wpack

	mov	ax, cs

	CLI				;disable interrupts for stack switch
	MOV	SS,AX
	MOV	SP,OFFSET STACK		;change to local stack
	STI

	push	ds			;save psp pointer
	mov	es, ax
	mov	di, offset cmdline
	mov	si, 080h
	mov	cl,[si]
	xor	ch, ch
	or	cx, cx
	jz	nocmd
	inc	si
	rep	movsb
nocmd:
	mov	ds, ax
	mov	ax, offset cmdline
	push	ax
	call	_install_wpack
	add	sp, 2
	pop	bx				;get back psp pointer

	or	ax, ax				;did it work
	jz	okw
	mov	ah,04Ch
	int 	21h
okw:
	mov	DX,CS
	mov	AX,word ptr cs:_end_wpack		;load all of the registers
	mov	CL,4
	shr	AX,CL
	add	DX,AX
	add	DX,2

	mov	DS,BX				;the psp
	sub	DX,BX

	push	DX
	mov	SI,02Ch
	mov	AX,word ptr DS:[SI]
	mov	ES,AX
	mov	AH,049h
	int	021h
	pop	DX

	mov	AL,0
	mov	AH,031h
	int	021h
	DB	10 DUP("----------------Stack Space Here") 
STACK	DW	0
cmdline	DB	256 DUP (0)
cendp   wpack
cend    WPACK

	end	_wpack
