include libmacro.inc

beglib


;from DoS
item	'putchar','*',1
	mov	dl, al
	mov	ah, 02
	int	21h
	pop 	ax

item	'cr','*',1
	push	ax
	mov	ah, 02
	mov	dl, 10
	int	21h
	mov	dl, 13
	int	21h
	pop 	ax

item	'bl','*',1
	push	ax
	mov	ah, 02
	mov	dl, 32
	int	21h
	pop 	ax


item	'putstring','*',1
	mov	bh, 02 
	xchg	ax, bx
_bg:	mov	dl, [bx]
	or	dl, dl
	jz short _ex
	int	21h
	inc	bx
	jmp short _bg
_ex:	pop	ax

item	'putdigit','*',1
	mov	dx, ax
	mov	ah, 02h	
	add	dl, 30h	
	int	21h
	pop	ax


item	'strequal','*',1
	mov	di, ax
	pop	bx
qagain:	mov	al, [bx]
	cmp	al, [di]
	jne	qfail
	cmp	al, 0
 	je	qsucc
	inc	di
	inc	bx
	jmp	qagain
qfail:	xor	ax, ax
	jmp	qexit
qsucc:	mov	ax, 0FFFFh
qexit:	


item	'concat','*',1
	mov	di, ax
	pop	bx
	dec	bx
	xor	cx, cx
skipf:	inc	bx
	inc	cx
	mov	al, [bx]
	cmp	al, 0
	jnz	skipf
nextf:	cmp	cx, 80
	jz	exf
	mov	al, [di]
	cmp	al, 0
	jz	exf
	mov	[bx], al
	inc	bx
	inc	di
	inc	cx
	jmp	nextf
exf:	xor	al, al
	mov	[bx], al
	pop	ax


item	'length','*',1
	mov	dx, ax
	mov	di, dx
	mov	cx, -1
	xor	al, al
	repnz	scasb
	mov	ax, -2
	sub	ax, cx

item	'copy','*',1
	mov	bx, ax
	pop	di
nextc:	mov	al, [bx]
	mov	[di], al
	inc	bx
	inc	di
	cmp	al, 0
	jnz	nextc
	pop	ax


item	'getstring','*',1
	mov	dx, ax
	mov	ah, 3Fh
	mov	bx, 0
	mov	cx, 80
	int	21h
	mov	bx, ax
	add	bx, dx
	dec	bx
	mov	byte ptr [bx], 0
	pop	ax


;{ converts subroutines }

;{ hex string 2 integer }
item		'atoh','*',1
  		mov 	di, ax
  		push 	di
		pushf
		cld
		push	cx
		xor	cx, cx
hCnvrtLp:	mov	al, [di]
		inc	di
		cmp	al, 'a'
		jb	hSkipConvert
		and	al, 5fh
hSkipConvert:	xor	al, '0'
		cmp	al, 10
		jb	hGotDigit
		add	al, 89h				
		cmp	al, 0fah
		jb	hDone
		and	al, 0fh				
hGotDigit:	shl	cx, 1				
		jc	hOverflow			
		shl	cx, 1
                jc	hOverflow
		shl	cx, 1
                jc	hOverflow
		shl	cx, 1
		jc	hOverflow
		or	cl, al				
		jmp	hCnvrtLp
hOverflow:	stc
		jmp	hWasError
hDone:		clc
hWasError:	mov	ax, cx
		pop	cx
		popf
		pop di

;{ ATOI-	Converts the string pointed at by ES:DI to a signed integer value
;
;	Returns with the carry flag clear if no error, set if overflow.
;}
item		'atoi','*',1
		mov	di, ax
		push	di
		push	cx
		push	dx
		xor	cx, cx
		mov	ah, ch				
		cmp	byte ptr es:[di], '-'
		jne	iDoAtoI
		inc	di				
		mov	ah, 1				
iDoAtoI:	pushf
		cld
ilp:		mov	al, es:[di]		
		inc	di
		xor	al, '0'
		cmp	al, 10
		ja	iNotDigit
		shl	cx, 1
		jc	iError
		mov	dx, cx
		shl	cx, 1
		jc	iError
		shl	cx, 1
		jc	iError
		add	cx, dx
		jc	iError
		add	cl, al
		adc	ch, 0
		jc	iError
		jmp	ilp
iNotDigit:	popf
		clc
		jmp	iAtoiEx
iError:		popf
		stc
iAtoiEx:
;--------------------------------------------------}
		jc	iWasError			;Quit if error.}
		cmp	ah, 0
		je	iIsPositive
		neg	cx
		clc
		jmp	iWasError			;Not really an error.}

iIsPositive:	or	cx, cx				;See if overflow}
		clc
		jns	iWasError			;Not an error}
                stc					;Error if negative.}
iWasError:	mov	ax, cx
		pop	dx
		pop	cx
;---------------------------------------------}
		pop	di
;		push	ax


item		'htoa','*',1
		mov	di, ax
		pop	ax
		xchg	al, ah
		call	hextoa
		xchg	al, ah
		call	hextoa
		jmp	wtoaEx
hextoa:		push	ax
		mov	ah, al
		shr	al, 1
		shr	al, 1
		shr	al, 1
		shr	al, 1
		add	al, 90h
		daa
		adc	al, 40h
		daa
		mov 	es:[di], al
                inc	di
		mov	al, ah
		and	al, 0fh
		add	al, 90h
		daa
		adc	al, 40h
		daa
		mov	es:[di], al
		inc	di
		pop	ax
		ret
wtoaEx:		xor 	al, al
		mov 	es:[di], al
		pop	ax

item		'itoa','*',1
		mov	di, ax
		pop 	ax
		cmp	ax, 0
		jge	iaDoIt
		push	ax
		mov	al, '-'
		mov	es:[di],al
 		inc	di
		pop	ax
		neg	ax
iaDoIt:		call	puti2
		jmp	iaExit

puti2:		mov	bx, 10
		xor	dx, dx
		div	bx
		or	ax, ax		;See if ax=0}
		jz	iaDone
		push	dx
		call	puti2
		pop	dx
iaDone:		mov	al, dl
		or	al, '0'
		mov	es:[di],al
		inc	di
		ret

iaExit:		xor	al,al
		mov	es:[di],al
		pop	ax

item		'utoa','*',1
		mov	di, ax
		pop 	ax
		call	puti2u
		jmp	utoaEx
puti2u:		mov	bx, 10
		xor	dx, dx
		div	bx
		or	ax, ax		;See if ax=0}
		jz	iaDoneu
		push	dx
		call	puti2u
		pop	dx
iaDoneu:	mov	al, dl
		or	al, '0'
		mov	es:[di],al
		inc	di
		ret
utoaEx:		xor	al,al
		mov	es:[di],al
		pop	ax


endlib