SteveOS for 16 bit processors (286)

textedit.asm 3.5KB

    ;; Steve16 text editor sedit_init: pusha mov bx, SEDIT_INIT_MSG ;Init msg call print mov [SEDIT_EDITOR_BUFFER], byte 0 ;; set the video mode, clearing the screen mov ax, 0x03 int 0x10 ;; read in from disk mov dh, 0 mov cx, 0xD ;sect & cyl mov al, 2 mov bx, SEDIT_EDITOR_BUFFER call read_sector ;; print it out call print mov [SEDIT_CURSOR_POSITION], byte 0 ;; increment cursor by correct amount mov bx, SEDIT_EDITOR_BUFFER sedit_cursor_inc: cmp [bx], byte 0 je sedit_loop inc bx inc byte [SEDIT_CURSOR_POSITION] jmp sedit_cursor_inc sedit_loop: ;; Read text in mov ah, 0x00 int 0x16 ;Read kb cmp al, 0x1B ;Esc pressed je sedit_exit cmp al, 0xD ;Enter key je sedit_enter cmp al, 0x8 ;Backspace je sedit_backspace ;; Compare with other special characters cmp al, ' ' jl sedit_loop ;al is less than space cmp al, 0x7F jg sedit_loop ;al is greater than ~ ;; At this point, we know it's a regular character mov bx, SEDIT_EDITOR_BUFFER add bx, [SEDIT_CURSOR_POSITION] mov [bx], al ;Append character to text buffer call print_char ;pretty temporary ;; Increment cursor position add [SEDIT_CURSOR_POSITION], byte 1 jmp sedit_loop sedit_enter: mov bx, SEDIT_EDITOR_BUFFER add bx, [SEDIT_CURSOR_POSITION] mov [bx], byte 0xD add bx, byte 1 mov [bx], byte 0xA ;Add 0xD and 0xA to buffer add [SEDIT_CURSOR_POSITION], byte 2 ;Increment pos mov al, 0xD call print_char mov al, 0xA call print_char ;Print out the enter jmp sedit_loop sedit_backspace: ;; Backspace the teletype cmp [SEDIT_CURSOR_POSITION], byte 0 ;don't bs if empty buffer je sedit_loop ;; remove character from buffer sub [SEDIT_CURSOR_POSITION], byte 1 mov bh, 0 mov ah, 0x03 int 0x10 ;Get the cursor's position cmp dl, 0 ;In the first column jne sedit_skip_upline ;; At this point, the cursor is at the start of the line ;; 2 options - either last character is a newline ;; -> go up a line and as far over as necessary ;; or last character isn't ;; -> go up a line, far right ;; check last char mov bx, SEDIT_EDITOR_BUFFER add bx, [SEDIT_CURSOR_POSITION] dec bx cmp [bx], byte 0xD je sedit_backspace_newline cmp [bx], byte 0xA je sedit_backspace_newline mov ah, 0x02 mov dl, 79 ;80 column display sub dh, 1 ;One row up int 0x10 mov ah, 0x0E mov al, ' ' int 0x10 ;Print a space, putting us back where we were before mov ah, 0x02 int 0x10 ;And back up we go jmp sedit_loop sedit_backspace_newline: ;; TODO: dont assume that there's 2 newline characters(0xD and 0xA) sub [SEDIT_CURSOR_POSITION], byte 1 call sedit_refresh_screen jmp sedit_loop sedit_skip_upline: ;; bs, space, bs mov al, 0x8 call print_char mov al, ' ' call print_char mov al, 0x8 call print_char jmp sedit_loop sedit_refresh_screen: ;redraws screen from buffer ;; set the video mode, clearing the screen mov ax, 0x03 int 0x10 mov bx, SEDIT_EDITOR_BUFFER add bx, [SEDIT_CURSOR_POSITION] mov [bx], byte 0 ;add a null terminator mov bx, SEDIT_EDITOR_BUFFER call print ret sedit_exit: ;; null terminator mov bx, SEDIT_EDITOR_BUFFER add bx, [SEDIT_CURSOR_POSITION] mov [bx], byte 0 ;; save buffer mov dh, 0 mov cx, 0xD ;sect & cyl mov al, 2 mov bx, SEDIT_EDITOR_BUFFER call write_sector ;; print & exit mov bx, SEDIT_EXIT_MSG call print popa ret ;; Varibles SEDIT_CURSOR_POSITION: dw 0 ;; Messages SEDIT_INIT_MSG: db "Steve16 Text Editor", 0xD, 0xA, 0 SEDIT_EXIT_MSG: db "Goodbye!", 0xD, 0xA, 0