;Reads a string of at most al characters to di. readstr: ;Store the length mov [.length], al ;Initialise the destination with spaces mov cx, [.length] mov al, 0x20 rep stosb sub di, [.length] ;Initialise the cursor pointer mov bl, 0x0 ;Initialise the last input line pointer mov ah, 0x3 int 0x10 mov byte [.lastln], dh .loop: ;Read a keypress mov ah, 0x0 int 0x16 ;Check for null cmp al, 0x0 je .loop ;Check for return cmp al, 0xd je .return ;Check for backspace cmp al, 0x8 je .backspace ;Check for input end mov cl, bl inc cl cmp cl, [.length] je .loop ;Check for space cmp al, 0x20 je .space ;Check for erase cmp al, 0x9 je .erase ;Check for non-printing characters cmp al, 0x1f jle .loop cmp al, 0x7f je .loop .char: ;Store the character stosb ;Print the character mov ah, 0xe int 0x10 ;Move the cursor pointer inc bl .checkln: ;Get the cursor position mov ah, 0x3 int 0x10 ;Check if the current line is the last one of the input cmp dh, [.lastln] jle .loop ;If it is mark it as so mov byte [.lastln], dh jmp .loop .erase: ;Replace the cursor position with a space mov al, 0x20 jmp .char .space: ;Get the cursor position mov ah, 0x3 int 0x10 ;More from the end of a line to the beginning of the next one cmp dl, [cpl] je .nextln ;Move forward within a line inc dl mov ah, 0x2 int 0x10 inc di inc bl jmp .loop .nextln: mov ah, 0x2 inc dh mov dl, 0x1 int 0x10 inc di inc bl ;Go to the last input line check jmp .checkln .backspace: ;Check for the input beginning cmp bl, 0x0 je .loop ;Get the cursor position mov ah, 0x3 int 0x10 ;More from the beginning of a line to the end of the previous one cmp dl, 0x1 je .prevln ;Move backward within a line dec dl mov ah, 0x2 int 0x10 dec di dec bl jmp .loop .prevln: mov ah, 0x2 dec dh mov dl, [cpl] int 0x10 dec di dec bl jmp .loop .return: ;Go to the end of the input mov bh, 0 sub di, bx add di, [.length] .findtrailing: ;Check for a trailing space dec di cmp byte [di], 0x20 je .deltrailing jmp .done .deltrailing: ;Delete a trailing space mov al, 0x0 stosb dec di jmp .findtrailing .done: ;Move the cursor to the last line of input mov ah, 0x2 mov dh, [.lastln] int 0x10 ;Print a newline mov si, .newline call printstr ret .length: db 0x0 .cursor: db 0x0 .lastln: db 0x0 .newline: db 0xd, 0xa, 0x0