2021-06-20 10:48:13 +00:00
|
|
|
;Read a string ending in a null of at most AL characters to DI until a return.
|
2021-06-04 17:36:46 +00:00
|
|
|
|
|
|
|
readstr:
|
|
|
|
|
2021-06-06 08:18:15 +00:00
|
|
|
;Store the initial registers in the stack
|
2021-06-10 15:16:14 +00:00
|
|
|
push ax
|
|
|
|
push bx
|
|
|
|
push cx
|
|
|
|
push dx
|
2021-06-21 08:39:06 +00:00
|
|
|
push di
|
2021-06-06 08:18:15 +00:00
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Setup
|
2021-06-04 17:36:46 +00:00
|
|
|
;Store the input length in the stack
|
|
|
|
mov ah, 0
|
|
|
|
push ax
|
|
|
|
;Initialise the destination with spaces
|
|
|
|
mov cx, ax
|
|
|
|
mov al, 0x20
|
|
|
|
rep stosb
|
|
|
|
pop ax
|
|
|
|
push ax
|
|
|
|
sub di, ax
|
2021-06-16 22:03:00 +00:00
|
|
|
;Initialise the cursor pointer in BL and clear BH
|
|
|
|
mov bx, 0x1
|
2021-06-04 17:36:46 +00:00
|
|
|
|
|
|
|
.loop:
|
|
|
|
|
|
|
|
;Read a keypress
|
|
|
|
mov ah, 0x0
|
|
|
|
int 0x16
|
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Check for special keys and non-printing characters
|
2021-06-04 17:36:46 +00:00
|
|
|
;Check for return
|
|
|
|
cmp al, 0xd
|
|
|
|
je .return
|
|
|
|
;Check for backspace
|
|
|
|
cmp al, 0x8
|
|
|
|
je .backspace
|
|
|
|
;Check for input end
|
|
|
|
pop dx
|
|
|
|
push dx
|
|
|
|
cmp bl, dl
|
|
|
|
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
|
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Store and print a character
|
2021-06-08 12:25:17 +00:00
|
|
|
.character:
|
2021-06-04 17:36:46 +00:00
|
|
|
;Store the character
|
|
|
|
stosb
|
|
|
|
;Print the character
|
|
|
|
mov ah, 0xe
|
|
|
|
int 0x10
|
|
|
|
;Move the cursor pointer
|
|
|
|
inc bl
|
|
|
|
jmp .loop
|
|
|
|
|
|
|
|
;Replace the cursor position with a space
|
2021-06-16 22:03:00 +00:00
|
|
|
.erase:
|
2021-06-04 17:36:46 +00:00
|
|
|
mov al, 0x20
|
2021-06-08 12:25:17 +00:00
|
|
|
jmp .character
|
2021-06-04 17:36:46 +00:00
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move the cursor forward
|
2021-06-04 17:36:46 +00:00
|
|
|
.space:
|
|
|
|
call .nextchar
|
|
|
|
inc di
|
|
|
|
inc bl
|
|
|
|
jmp .loop
|
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move the cursor backward
|
2021-06-04 17:36:46 +00:00
|
|
|
.backspace:
|
|
|
|
;Check for the input beginning
|
|
|
|
cmp bl, 0x1
|
|
|
|
je .loop
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move the cursor
|
2021-06-04 17:36:46 +00:00
|
|
|
call .prevchar
|
|
|
|
dec di
|
|
|
|
dec bl
|
|
|
|
jmp .loop
|
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Finish reading the string
|
2021-06-04 17:36:46 +00:00
|
|
|
.return:
|
|
|
|
|
2021-06-08 12:25:17 +00:00
|
|
|
;Find and remove trailing spaces
|
2021-06-04 17:36:46 +00:00
|
|
|
;Go to the end of the input
|
|
|
|
pop ax
|
|
|
|
mov bh, 0x0
|
|
|
|
sub ax, bx
|
|
|
|
push di
|
|
|
|
add di, ax
|
|
|
|
.findtrailing:
|
|
|
|
;Check for a trailing space
|
|
|
|
cmp byte [di], 0x20
|
|
|
|
je .deltrailing
|
|
|
|
jmp .end
|
|
|
|
.deltrailing:
|
|
|
|
;Delete a trailing space
|
|
|
|
mov al, 0x0
|
|
|
|
stosb
|
|
|
|
sub di, 0x2
|
|
|
|
jmp .findtrailing
|
|
|
|
|
|
|
|
;Move the cursor to the end of the input string
|
2021-06-08 12:25:17 +00:00
|
|
|
.end:
|
2021-06-04 17:36:46 +00:00
|
|
|
pop di
|
|
|
|
.findend:
|
|
|
|
cmp byte [di], 0x0
|
|
|
|
jne .notend
|
|
|
|
jmp .done
|
|
|
|
.notend:
|
|
|
|
call .nextchar
|
|
|
|
inc di
|
|
|
|
inc bl
|
|
|
|
jmp .findend
|
|
|
|
|
|
|
|
.done:
|
2021-06-06 08:18:15 +00:00
|
|
|
|
|
|
|
;Load the initial registers from the stack
|
2021-06-21 08:39:06 +00:00
|
|
|
pop di
|
2021-06-10 15:16:14 +00:00
|
|
|
pop dx
|
|
|
|
pop cx
|
|
|
|
pop bx
|
|
|
|
pop ax
|
2021-06-06 08:18:15 +00:00
|
|
|
|
2021-06-18 18:59:45 +00:00
|
|
|
iret
|
2021-06-04 17:36:46 +00:00
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move the cursor forward
|
|
|
|
|
|
|
|
;Move forward within a line
|
2021-06-04 17:36:46 +00:00
|
|
|
.nextchar:
|
|
|
|
;Get the cursor position
|
|
|
|
mov ah, 0x3
|
|
|
|
int 0x10
|
2021-06-16 22:03:00 +00:00
|
|
|
;Check for the end of the line
|
|
|
|
cmp dl, 0x4f
|
2021-06-04 17:36:46 +00:00
|
|
|
je .nextln
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move
|
2021-06-04 17:36:46 +00:00
|
|
|
inc dl
|
|
|
|
mov ah, 0x2
|
|
|
|
int 0x10
|
|
|
|
ret
|
2021-06-16 22:03:00 +00:00
|
|
|
|
|
|
|
;Move to the beginning of the next line
|
2021-06-04 17:36:46 +00:00
|
|
|
.nextln:
|
2021-06-16 22:03:00 +00:00
|
|
|
;Check if the current line is the last on screen
|
|
|
|
cmp dh, 0x18
|
|
|
|
je .scroll
|
|
|
|
;Move
|
2021-06-04 17:36:46 +00:00
|
|
|
mov ah, 0x2
|
|
|
|
inc dh
|
2021-06-16 22:03:00 +00:00
|
|
|
mov dl, 0x0
|
|
|
|
int 0x10
|
|
|
|
ret
|
|
|
|
|
|
|
|
;Scroll the screen up by one line
|
|
|
|
.scroll:
|
|
|
|
;Scroll
|
|
|
|
mov ah, 0x6
|
|
|
|
mov al, 0x1
|
|
|
|
mov bh, 0x7
|
|
|
|
mov ch, 0x0
|
|
|
|
mov cl, 0x0
|
|
|
|
mov dh, 0x18
|
|
|
|
mov dl, 0x4f
|
|
|
|
int 0x10
|
|
|
|
;Move to the beginning of the new line
|
|
|
|
mov ah, 0x2
|
|
|
|
mov bh, 0x0
|
|
|
|
mov dl, 0x0
|
2021-06-04 17:36:46 +00:00
|
|
|
int 0x10
|
|
|
|
ret
|
|
|
|
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move the cursor backward
|
|
|
|
|
|
|
|
;Move backward within a line
|
2021-06-04 17:36:46 +00:00
|
|
|
.prevchar:
|
|
|
|
;Get the cursor position
|
|
|
|
mov ah, 0x3
|
|
|
|
int 0x10
|
2021-06-16 22:03:00 +00:00
|
|
|
;Check for the beginning of the line
|
|
|
|
cmp dl, 0x0
|
2021-06-04 17:36:46 +00:00
|
|
|
je .prevln
|
2021-06-16 22:03:00 +00:00
|
|
|
;Move
|
2021-06-04 17:36:46 +00:00
|
|
|
dec dl
|
|
|
|
mov ah, 0x2
|
|
|
|
int 0x10
|
|
|
|
ret
|
2021-06-16 22:03:00 +00:00
|
|
|
|
|
|
|
;Move to the end of the previous line
|
2021-06-04 17:36:46 +00:00
|
|
|
.prevln:
|
|
|
|
mov ah, 0x2
|
|
|
|
dec dh
|
2021-06-16 22:03:00 +00:00
|
|
|
mov dl, 0x4f
|
2021-06-04 17:36:46 +00:00
|
|
|
int 0x10
|
|
|
|
ret
|