include	clib.inc
include	iost.inc

	public	owrite

_TEXT	SEGMENT

owrite  PROC
	assert	cx,0,jne,"owrite"
	push	si
	push	di
	mov	si,bx
	mov	di,es
	mov	dx,STDO.ios_i
	mov	ax,STDO.ios_size
	sub	ax,dx
	cmp	ax,cx
	jb	owrite_slow
	add     STDO.ios_i,cx
	mov	ax,word ptr STDO.ios_bp+2
	add	dx,word ptr STDO.ios_bp
	mov	ds,di
	xchg	di,dx
	mov	es,ax
	cld
	shr	cx,1
	rep	movsw
	jc	owrite_bytealign
    owrite_wend:
	mov	ax,ss
	mov	ds,ax
	mov	es,dx
    owrite_end:
	mov	bx,si
	pop	di
	pop	si
	ret
    owrite_slow:
	mov	al,es:[si]
	call	oputc
	mov	es,di
	jz	owrite_end
	inc	si
	dec	cx
	jnz	owrite_slow
	test	ax,ax
	jmp	owrite_end
    owrite_bytealign:
	movsb
	jmp	owrite_wend
owrite	ENDP

_TEXT	ENDS

	END
