
ReceiveString:
	mov	r0,#RxBuffer        	; Initialize receiver pointer

GetNextChar:				; Come here to get each new character
	call	RxChar			; Go get a character
	jb      acc.7,GetNextChar	; It's unprintable (>= 80h) - ignore it

	cjne    a,#20h,$+3		; Is it a control character (<20h)?
	jc      ControlChars		;  Yes - process separately
	cjne    r0,#BUFFER_END,SaveChar	;  No - go store if no overflow

	mov     a,#BEEP_CHR		; Buffer overflow if we get here
	call	TxChar			; Make the terminal beep
	sjmp	GetNextChar		; Go get next character

SaveChar:				; Printable character if we get here
	mov     @r0,a			; Put character in buffer	
	inc     r0			; Advance buffer pointer
	call	TxChar			; Echo character to terminal
	sjmp	GetNextChar		; Go get next character

ControlChars:				; Control character if we get here
	cjne    a,#BS_CHR,NotBackSpace	; Not backspace - press on
	cjne    r0,#RxBuffer,EatChar	; Is backspace - go eat if no underflow 
	
	mov     a,#BEEP_CHR		; Buffer underflow if we get here
	call	TxChar			; Make the terminal beep
	sjmp	GetNextChar		; Go get next character

EatChar:				; Process backspace here
	mov	a,#BS_CHR		; Send BS-SPACE-BS to terminal to erase
	call	TxChar			;  the most recently typed character
	mov	a,#SPACE_CHR
	call	TxChar
	mov	a,#BS_CHR
	call	TxChar
	dec     r0			; Remove the character from the buffer
	sjmp    GetNextChar		; Go get next character

NotBackSpace:				; Control char, but not backspace
	cjne    a,#CR_CHR,GetNextChar	; If not CR, go get next character

	mov     @r0,#0			; Got CR - terminate the string
	ret				; Done
