;
;	Additional runtime support routines for T3X under DOS.
;	Copyright (C) 1997,1998 Nils M Holm.
;	Some of these routines supersede procedures already
;	defined in lib.c.
;	See the file LICENSE for conditions of use.
;
	.model small
	.code
	public	_pack_, _unpack_
	public	__exit, _length_
	public	_creat, _open, _close, _read, _write, _unlink, _lseek
	public	_rename, _memmove, _memcmp
_pack_:	push	bp
	mov	bp,sp
	push	si
	push	di
	mov	si,[bp+6]
	mov	di,[bp+4]
	mov	dx,di
	cld
pknext:	lodsw
	stosb
	or	ax,ax
	jnz	pknext
	sub	di,dx
	inc	di
	shr	di,1
	mov	ax,di
	pop	di
	pop	si
	pop	bp
	ret
_unpack_:
	push	bp
	mov	bp,sp
	push	si
	push	di
	mov	si,[bp+6]
	mov	di,[bp+4]
	mov	dx,si
	cld
upknext:lodsb
	xor	ah,ah
	stosw
	or	ax,ax
	jnz	upknext
	sub	si,dx
	mov	ax,si
	pop	di
	pop	si
	pop	bp
	ret
__exit:	mov	bp,sp
	mov	ax,[bp+2]
	mov	ah,4Ch
	int	21h
;	ret		; we'll hopefully never get here...
_length_:push	bp
	mov	bp,sp
	push	di
	mov	di,[bp+4]
	mov	dx,di
	cld
	xor	ax,ax
	xor	cx,cx
	dec	cx
	repnz	scasw
	sub	di,dx
	shr	di,1
	dec	di
	mov	ax,di
	pop	di
	pop	bp
	ret
_creat:	push	bp
	mov	bp,sp
	mov	ax,3C00h
	mov	dx,[bp+4]
	xor	cx,cx
	int	21h
	jc	ioerr
	pop	bp
	ret
_open:	push	bp
	mov	bp,sp
	mov	ax,[bp+6]
	or	ax,3D00h
	mov	dx,[bp+4]
	int	21h
	jc	ioerr
	pop	bp
	ret
_close:	push	bp
	mov	bp,sp
	mov	ax,3E00h
	mov	bx,[bp+4]
	int	21h
	jc	ioerr
	xor	ax,ax
	pop	bp
	ret
_unlink:push	bp
	mov	bp,sp
	mov	ax,4100h
	mov	dx,[bp+4]
	int	21h
	jc	ioerr
	pop	bp
	ret
_read:	push	bp
	mov	bp,sp
	mov	ax,3F00h
	mov	bx,[bp+4]
	mov	dx,[bp+6]
	mov	cx,[bp+8]
	int	21h
	jc	ioerr
	pop	bp
	ret
_write:	push	bp
	mov	bp,sp
	mov	ax,4000h
	mov	bx,[bp+4]
	mov	dx,[bp+6]
	mov	cx,[bp+8]
	int	21h
	jc	ioerr
	pop	bp
	ret
ioerr:	mov	ax,-1
	pop	bp
	ret
_lseek:	push	bp
	mov	bp,sp
	mov	ax,[bp+10]
	mov	ah,42h
	mov	bx,[bp+4]
	mov	dx,[bp+6]
	mov	cx,[bp+8]
	int	21h
	jc	ioerr
	pop	bp
	ret
_rename:push	bp
	mov	bp,sp
	push	di
	mov	ax,5600h
	mov	dx,[bp+4]
	mov	di,[bp+6]
	int	21h
	jc	ioerr
	xor	ax,ax
	pop	di
	pop	bp
	ret
_memmove:
	push	bp
	mov	bp,sp
	push	di
	push	si
	mov	di,[bp+4]	;; dest
	mov	si,[bp+6]	;; src
	mov	cx,[bp+8]	;; size
	cld
	cmp	di,si
	jz	endmove		;; src == dest
domove:	jb	nmove		;; src < dest ==> normal move
	std
	add	si,cx		;; start at the end
	add	di,cx		;; for reverse copy
	dec	si
	dec	di
nmove:	rep
	movsb		;; doit
endmove:pop	si
	pop	di
	pop	bp
	ret
_memcmp:push	bp
	mov	bp,sp
	push	si
	push	di
	mov	di,[bp+4]
	mov	si,[bp+6]
	mov	cx,[bp+8]
	cmp	si,di
	jnz	docmp
	xor	ax,ax
	jmp	short cmpret
docmp:	inc	cx
	cld
	rep cmpsb
	or	cx,cx
	jnz	notequ
	xor	ax,ax
	jmp	short cmpret
notequ:	mov	al,[di-1]
	sub	al,[si-1]
	cbw
cmpret:	pop	di
	pop	si
	pop	bp
	ret
	end
