	TITLE	speed.asm - some asm routines for speed
	NAME	speed
;; C Durland	Public Domain
	INCLUDE	DOSd.MAC

	DSEG
beol	DB 0
PUBLIC	beol
	ENDDS

	PSEG			; begin program segment

; blkccpy(to,from,tn,fn)
; Always appends a '\0' to to.
; Returns: bytes copied
;  sets beol: 0 (EoL or copied tn bytes), 1 (copied fn bytes),
;    2(CR is last byte of from)

	BEGIN	blkccpy
	PUSH	BP
	MOV 	BP,SP

	push DS
	push ES
	push DI
	push SI

	mov  DI,[BP+04H]	; to
	MOV  ES,[BP+06H]

	MOV  SI,[BP+08H]	; from
	MOV  DS,[BP+0AH]

	MOV  CX,[BP+0CH]	; tn
	MOV  DX,[BP+0EH]	; fn

	xor  BX,BX		; bytes read (beol==0) else bytes copied

	CLD
bk10:		; copy bytes until CRLF or LF or copied n bytes
	cmp  DX,0	; done if nothing left to copy
	jE   bk70
	cmp  CX,0	; done if filled to
	jE   bk80
	LODSB		; AL = *from++
	inc  BX		; read a byte
	dec  DX		; out of from
	cmp  AL,LF	; a LF?
	jE  bk80	; yes - done with this line
	cmp AL,CR	; a CR?
	jE bk30		; yes
bk20:
	stosb		; *buf++ = AL
	dec  CX		; copied a byte
	jmp  bk10
bk30:
	cmp  DX,0	; at end of from?
	jNE bk40	; nope
	  mov beol,2	; a CR is the last byte of from
	  dec BX	; ignore that last byte
	  jmp bk90
bk40:
	cmp  byte ptr [SI],LF	; is next character a LF?
	jNE  bk20	; no
	  inc BX	; skip the LF
	  jmp bk80


bk70:
	mov beol,1	; copied all of from
	jmp bk90
bk80:
	mov beol,0	; filled to or found EoL
bk90:
	mov  AL,0	; terminate string
	stosb
	mov AX,BX	; number of bytes copied

	pop SI
	pop DI
	pop ES
	pop DS
	POP BP
	RET
blkccpy	ENDP

;
; blkmov(to,from,n)
;
	BEGIN	blkmov
	PUSH	BP
	MOV 	BP,SP

	push DS
	push ES
	push DI
	push SI

	mov  DI,[BP+04H]	; to
	MOV  ES,[BP+06H]

	MOV  SI,[BP+08H]	; from
	MOV  DS,[BP+0AH]

	MOV  CX,[BP+0CH]	; n

	CLD
	rep movsb

	pop SI
	pop DI
	pop ES
	pop DS
	POP BP		; restore frame pointer
	RET
blkmov	ENDP

	ENDPS				; end the program segment
	END				; end the source file
