From 88716c0b64cd710bc33dfafafffdc05fefad7e1a Mon Sep 17 00:00:00 2001 From: deadvey Date: Sun, 8 Mar 2026 22:07:21 +0000 Subject: [PATCH] Fixed some bugs with inputs --- data/hello.bas | 5 +- source/kernel/data.asm | 10 +- source/kernel/features/basic.asm | 10 +- source/kernel/features/check.asm | 29 ++++++ source/kernel/features/cli.asm | 1 + source/kernel/features/disk.asm | 9 +- source/kernel/features/keyboard.asm | 12 ++- source/kernel/features/strings.asm | 10 +- source/kernel/features/utils.asm | 6 +- source/kernel/programs/ed.asm | 139 ++++++++++++++++++++++++++-- 10 files changed, 199 insertions(+), 32 deletions(-) create mode 100644 source/kernel/features/check.asm diff --git a/data/hello.bas b/data/hello.bas index 8018bd4..eb44550 100644 --- a/data/hello.bas +++ b/data/hello.bas @@ -1,2 +1,5 @@ -PRINT "Hello World" +PRINT "Hello, what is your name?" +INPUT $1 +PRINT "Hello " ; +PRINT $1 END diff --git a/source/kernel/data.asm b/source/kernel/data.asm index 6da61a7..d737896 100644 --- a/source/kernel/data.asm +++ b/source/kernel/data.asm @@ -26,7 +26,7 @@ ebr_system_id: db 'FAT12 ' ; must be 8 bytes fat12_file_name: db ' ' ; 11 free bytes to store a filename in boot_message: db 'OK] Kernel successfully loaded!\n"HELP" to see a list of available commands\n', 0 user_input: times 20 db 0 -prompt_length: db 20 +prompt_length: dw 20 prompt: db 'sh > ', 0 help_string: db 'HELP', 0 clear_string: db 'CLEAR', 0 @@ -35,7 +35,7 @@ basic_string: db 'BAS', 0 cat_string: db 'CAT', 0 ls_string: db 'LS', 0 ed_string: db 'ED', 0 -help_text: db 'This is for Cowards:\n"LS" to list the directory,\n"CAT" to output the contents of a file,\n"BAS" to run a basic script,\n"HELP" for this helpful text,\n"CLEAR" to clear the screen,\n"REBOOT" or esc to reboot\n', 0 +help_text: db 'This is for Cowards:\n"LS" to list the directory,\n"CAT " to output the contents of a file,\n"BAS " to run a basic script,\n"HELP" for this helpful text,\n"CLEAR" to clear the screen,\n"REBOOT" or esc to reboot\n', 0 basic_text: db 'BASIC PROGRAM BEGUN:\n', 0 command_result_text: db 'You typed: ', 0 unknown_command: db 'Error: Unkown Command..\n ', 0 @@ -48,12 +48,6 @@ file_not_found: db 'File not found\n', 0 too_long_filename: db 'Filename too long for Fat12\n', 0 file_found: db 'File found\n', 0 loading_root: db 'Loading root diretory\n', 0 -read_only: db 'Read Only', 0 ; 1 -hidden: db ' Hidden ', 0 ; 2 -system: db ' System ', 0 ; 4 -volume: db ' Volume ', 0 ; 8 -directory: db 'Directory', 0 ; 10 -archive: db ' Archive ', 0 ; 20 file_name_length: db 0 file_cluster: dw 0 file_length: dd 0 diff --git a/source/kernel/features/basic.asm b/source/kernel/features/basic.asm index 55d0334..8a13874 100644 --- a/source/kernel/features/basic.asm +++ b/source/kernel/features/basic.asm @@ -1950,8 +1950,9 @@ do_input: .number_var: mov ax, .tmpstring ; Get input from the user - mov bx, 6 + mov bx, 20 call keyboard_display_input + jc .end mov ax, .tmpstring call string_length @@ -1973,8 +1974,6 @@ do_input: call os_print_newline jmp mainloop - - .string_var: mov ax, 128 mul bx @@ -1987,8 +1986,9 @@ do_input: jmp mainloop - - .tmpstring times 6 db 0 + .tmpstring times 20 db 0 +.end + ret ; ----------------------------------------------------------- diff --git a/source/kernel/features/check.asm b/source/kernel/features/check.asm new file mode 100644 index 0000000..3b42515 --- /dev/null +++ b/source/kernel/features/check.asm @@ -0,0 +1,29 @@ +check_is_number: + cmp al, 48 + jl .not_number + cmp al, 57 + jg .not_number + stc + ret +.not_number: + clc + ret + +check_is_letter: + cmp al, 65 + jl .not_letter + cmp al, 122 + jg .not_letter + cmp al, 90 + jg .maybe_not_letter + stc + ret +.maybe_not_letter: + cmp al, 97 + jl .not_letter + stc + ret +.not_letter: + clc + ret + diff --git a/source/kernel/features/cli.asm b/source/kernel/features/cli.asm index 153950f..5036b3c 100644 --- a/source/kernel/features/cli.asm +++ b/source/kernel/features/cli.asm @@ -11,6 +11,7 @@ os_start_cli: mov ax, user_input call keyboard_display_input + jc power_reboot jmp os_read_cli diff --git a/source/kernel/features/disk.asm b/source/kernel/features/disk.asm index de184a7..77244c2 100644 --- a/source/kernel/features/disk.asm +++ b/source/kernel/features/disk.asm @@ -171,6 +171,7 @@ disk_clear_output_buffer: ; OUT ; data_buffer: file contents ; TODO use predefined data for calculations +; carry flag: set if failed to load file disk_load_file: pusha call string_length ; cl = string length @@ -227,11 +228,15 @@ disk_load_file: .file_not_found: mov si, file_not_found call os_print_string - jmp .done + jmp .done_fail .filename_too_long: mov si, too_long_filename call os_print_string - jmp .done + jmp .done_fail +.done_fail: + popa + stc + ret .done: popa ret diff --git a/source/kernel/features/keyboard.asm b/source/kernel/features/keyboard.asm index edaef1c..ec4067d 100644 --- a/source/kernel/features/keyboard.asm +++ b/source/kernel/features/keyboard.asm @@ -15,6 +15,7 @@ os_read_input: ; AX = key pressed ; Returns straight away +; carry flag is set when escape is pressed and data is returned straight away keyboard_check_key: xor ax,ax mov ah, 11h ; BIOS call to check for key @@ -27,11 +28,11 @@ keyboard_check_key: ; IN: ; AX = output address ; BX = max length - keyboard_display_input: pusha mov di, ax - mov ax, bx + xor ax,ax + mov [prompt_length], bx .check_key_pressed: call os_read_input @@ -54,7 +55,9 @@ keyboard_display_input: jmp .check_key_pressed .esc_key: - call power_reboot + stc + popa + ret .enter_key: mov al, 0 @@ -63,7 +66,7 @@ keyboard_display_input: ret ; Return to the parent function (whatever that may be) .backspace: - cmp bx, 20 ; Cannot backspace if the cursor is at the start + cmp bx, [prompt_length] ; Cannot backspace if the cursor is at the start jb .move_cursor_back ; then .move_cursor_back jmp .check_key_pressed ; Else check the next key @@ -86,6 +89,7 @@ keyboard_display_input: mov ah, 0Eh int 10h jmp .check_key_pressed + .input_length db 0 keyboard_get_cursor_pos: ret diff --git a/source/kernel/features/strings.asm b/source/kernel/features/strings.asm index 7ce4df8..384c8ef 100644 --- a/source/kernel/features/strings.asm +++ b/source/kernel/features/strings.asm @@ -107,7 +107,7 @@ string_join: ; ------------------------------------------------------------------------ ; TODO -; Converts a null terminated string to an integer +; Converts a string to an integer ; IN: SI = string location (max 5 chars, up to '65536') ; OUT: AX = number string_cast_to_int: @@ -116,8 +116,12 @@ string_cast_to_int: .loop: xor ax,ax lodsb - cmp al, 0 - je .finish + + cmp al, 30h + jl .finish + cmp al, 39h + jg .finish + sub al, 30h mov bx, ax mov ax, 10 diff --git a/source/kernel/features/utils.asm b/source/kernel/features/utils.asm index 308cd22..edb3943 100644 --- a/source/kernel/features/utils.asm +++ b/source/kernel/features/utils.asm @@ -7,10 +7,12 @@ util_cat: add si, 4 ; Move si to after 'CAT' call disk_load_file + jc .done ; if file failed to load, just quit + mov si, file_buffer mov cx, [file_length] call text_print_raw - +.done popa ret @@ -34,6 +36,7 @@ util_basic: ; TODO make this more consistent to account for double spaces ;add si, 3 ; Move si to after 'BAS' call disk_load_file + jc .done mov si, file_buffer @@ -41,6 +44,7 @@ util_basic: mov si, 0 call basic_run_basic +.done popa ret diff --git a/source/kernel/programs/ed.asm b/source/kernel/programs/ed.asm index 5e6805f..f3e8f4b 100644 --- a/source/kernel/programs/ed.asm +++ b/source/kernel/programs/ed.asm @@ -1,18 +1,19 @@ ; Planned commands: ; i insert ; a append +; d delete ; l prints lines ; w save to disk ; q quit ed: pusha - mov si, ed_welcome - call os_print_string + call .determine_line_count .main_loop: mov ax, ed_prompt mov bx, 40 call keyboard_display_input ; AX = string location, BX = max prompt length mov si, ed_prompt + mov bx, 0 ; Keeps track of whether comma has passed jmp .get_token ; Types of tokens: @@ -20,16 +21,138 @@ ed: ; Doller (refers to the last line) $ ; Comma , ; Command: eg i - ; 123 + ; eg 123,127i .get_token: - xor ax,ax lodsb - call check_is_number - jmp .exit - .exit: + ; COMMA + cmp al, ',' + je .comma + + ; NUMBER + call check_is_number + jc .get_number_token + + ; COMMAND + call check_is_letter + jc .get_letter_token + + .error: ; Output ? + mov ah, 0Eh + + mov al, '?' + int 10h + mov al, 0Ah + int 10h + mov al, 0Dh + int 10h + + jmp .main_loop + + ; Assign a number to either the start or end int (start,end) + .get_number_token: + push si + dec si + call string_cast_to_int ; AX = integer + cmp bx, 0 + je .assign_start_int + jg .assign_end_int + .assign_start_int: + mov [ed_start_int], ax + jmp .after_int_assign + .assign_end_int: + mov [ed_end_int], ax + .after_int_assign: + pop si + jmp .get_token + + ; Determines a command + .get_letter_token: + cmp al, 'd' + je .delete + cmp al, 'q' + je .quit + jmp .get_token + + ; Changes the value from start int to end int + .comma: + mov bx, 1 + jmp .get_token + + .determine_line_count: + mov si, file_buffer + mov dx, file_length ; STORES THE LENGTH OF THE FILE IN BYTES + xor cx,cx + .line_count_loop: + lodsb + cmp al, 0Ah + je .increment_line_count + dec dx + cmp dx, 0 + jg .line_count_loop + mov [ed_line_count], cx + jmp .main_loop + .increment_line_count: + inc cx + jmp .line_count_loop + + ; COMMANDS + .delete: + ; set up registers to shift data left + mov bx, ed_end_int + inc bx + call get_byte_of_line ; CX = byte of start of new line (relative to file buffer) + mov [ed_shift_from], cx + + mov bx, ed_start_int + call get_byte_of_line + mov ax, ed_start_int + sub ax, cx + mov [ed_shift_by], ax + + .quit: popa ret +shift_data_left: + mov si, ed_shift_from + mov cx, ed_shift_by + mov di, ed_shift_from + sub di, cx + mov dx, file_length ; STORES THE LENGTH OF THE FILE IN BYTES + .shift_left_loop: + lodsb + stosb + cmp al, 0 + jne .shift_left_loop + jmp ed.main_loop + +; BX = target line +get_byte_of_line: + push dx + mov si, file_buffer + xor dx,dx + xor cx,cx + jmp .increment_line_count + .line_loop: + lodsb + inc cx + cmp al, 0Ah + je .increment_line_count + .increment_line_count: + inc dx + cmp dx, bx + jne .line_loop + inc cx + pop dx + ret + +ed_data_start: db "ED IS COOL" ed_prompt: times 40 db 0 -ed_welcome: db "This is ED\n", 0 +ed_start_int: dw 0 +ed_end_int: dw 0 + +ed_line_count: dw 0 + +ed_shift_from: dw 0 +ed_shift_by: dw 0