                 .ORIG x0530
;; trap 30
;; this trap reads a string into a buffer
;; buffer address in R0 
;; returns count of chars in R1
;;


        ST    R0 save0	;save registers
        ST    R4 save4
	ST    R5 save5
        ST    R6 save6

        AND   R1 R1 #0	;zero out the count of characters

input   LDI   R4 kbsr	;wait for the keystroke
        BRZP  input
        LDI   R5 kbdr	;get the character
echo    LDI   R4 dsr	;wait until the display is ready
        BRZP  echo
        STI   R5 ddr	;send character to display to be printed

        LD    R6 negNL	;check if character was NL
        ADD   R4 R5 R6
        BRZ   foundNL	;yes it was

        STR   R5 R0 #0	;no it wasn't, store char in buffer
        ADD   R0 R0 #1	;increment buffer pointer for next character
        ADD   R1 R1 #1	;increment count of characters processed
        BRNZP input	;go get another character

foundNL AND   R5 R5 #0	;store ASCII zero instead of NL
        STR   R5 R0 #0

        LD    R0 save0	;restore registers
        LD    R4 save4
        LD    R5 save5
        LD    R6 save6

        RET   		;return to caller

save0   .FILL x0000
save4   .FILL x0000
save5   .FILL x0000
save6   .FILL x0000
dsr     .FILL xFE04
ddr     .FILL xFE06
kbsr    .FILL xFE00
kbdr    .FILL xFE02
negNL   .FILL xFFF6     ; 2's complement of ASCII newline
	.end
