Add readch and for the sake of symmetry printch, simplify make.sh, and tweak the code a bit to make it more consistent.

This commit is contained in:
CrazyEttin 2021-06-08 15:25:17 +03:00
parent cc35d72a4e
commit 9afbaae05d
14 changed files with 69 additions and 49 deletions

View File

@ -1,9 +1,9 @@
[ORG 0x7c00] [ORG 0x7c00]
jmp short start jmp start
nop nop
;1.44 MB 3.5" floppy disk description table ;1.44 MB 3.5" floppy disk description table
OEMLabel db "EttinOS " OEMLabel db "ETTINOS "
BytesPerSector dw 0x200 BytesPerSector dw 0x200
SectorsPerCluster db 0x1 SectorsPerCluster db 0x1
BootRecordSectors dw 0x1 BootRecordSectors dw 0x1
@ -19,7 +19,7 @@ LargeSectors dd 0x0
DriveNumber dw 0x0 DriveNumber dw 0x0
DriveSignature db 0x29 DriveSignature db 0x29
VolumeID dd 0x0 VolumeID dd 0x0
VolumeLabel db "EttinOS " VolumeLabel db "ETTINOS "
FileSystem db "FAT12 " FileSystem db "FAT12 "
start: start:
@ -53,7 +53,7 @@ mov al, 0xe
mov ah, 0x2 mov ah, 0x2
int 0x13 int 0x13
;Search the root FAT for the system ;Search the root FAT for the system FAT
;Set DI to the root FAT ;Set DI to the root FAT
mov ax, ds mov ax, ds
mov es, ax mov es, ax
@ -64,7 +64,7 @@ mov ax, 0x0
search: search:
;Store CX in the stack ;Store CX in the stack
push cx push cx
;Check for the binary ;Check for the system FAT
mov si, sysfile mov si, sysfile
mov cx, 0xb mov cx, 0xb
rep cmpsb rep cmpsb
@ -79,7 +79,7 @@ loop search
;Load the system FAT ;Load the system FAT
loadsysfat: loadsysfat:
;??? ;Store the first cluster
mov ax, word [es:di+0xf] mov ax, word [es:di+0xf]
mov word [cluster], ax mov word [cluster], ax
;Set the source ;Set the source

View File

@ -1,4 +1,4 @@
;Converts a byte in ah to a hex string at di. ;Converts a byte in AH to a hex string at DI.
byte2hex: byte2hex:
@ -8,16 +8,16 @@ pusha
;Set a key for the hex digits ;Set a key for the hex digits
mov si, .key mov si, .key
;Set a counter for the two characters of the hex string ;Set a counter for the two hex digits
mov cx, 0x2 mov cx, 0x2
.loop: .loop:
;Read the byte ;Read a nibble
rol ax, 0x4 rol ax, 0x4
mov bx, ax mov bx, ax
;Convert the byte to a hex digit ;Convert the nibble to a hex digit
and bx, 0xf and bx, 0xf
mov bl, [si + bx] mov bl, [si + bx]

View File

@ -1,4 +1,4 @@
;Compares strings from si and di and sets the carry flag if they are equal and clears it if not. ;Compares strings from SI and SI and sets the carry flag if they are equal and clears it if not.
cmpstr: cmpstr:
@ -40,4 +40,4 @@ stc
;Load the initial registers from the stack ;Load the initial registers from the stack
popa popa
ret ret

View File

@ -16,4 +16,4 @@ mov si, buffer
call println call println
.done: .done:
ret ret

View File

@ -8,4 +8,4 @@ call println
ret ret
.help: .help:
db "Input:", 0xd, 0xa, "* Typing a character overwrites the cursor location.", 0xd, 0xa, "* The erase (=tab) key erases the cursor location.", 0xd, 0xa, "* The space and backspace keys move the cursor.", 0xd, 0xa, "Commands:", 0xd, 0xa, "* echo: echoes its input.", 0xd, 0xa, "* hello: a hello world program.", 0xd, 0xa, "* help: you are reading it.", 0xd, 0xa, "* keycode: echoes the BIOS code of a key.", 0x0 db "Input:", 0xd, 0xa, "* Typing a character overwrites the cursor location.", 0xd, 0xa, "* The erase (=tab) key erases the cursor location.", 0xd, 0xa, "* The space and backspace keys move the cursor.", 0xd, 0xa, "Commands:", 0xd, 0xa, "* echo: echoes its input.", 0xd, 0xa, "* hello: a hello world program.", 0xd, 0xa, "* help: you are reading it.", 0xd, 0xa, "* keycode: echoes the BIOS code of a key.", 0x0

View File

@ -1,4 +1,4 @@
;Reads a keypress and prints its BIOS code. ;Reads a keypress and prints its BIOS keycode.
keycode: keycode:

8
SOURCE/PRINTCH.INC Normal file
View File

@ -0,0 +1,8 @@
;Prints a character from AL
printch:
mov ah, 0xe
int 0x10
ret

View File

@ -1,4 +1,4 @@
;Prints a string from si until a null, followed by a newline. ;Prints a string from SI until a null, followed by a newline.
println: println:

View File

@ -1,4 +1,4 @@
;Prints a string from si until a null. ;Prints a string from SI until a null.
printstr: printstr:
@ -16,7 +16,6 @@ je .done
;Print the character ;Print the character
mov ah, 0xe mov ah, 0xe
mov bx, 0x0
int 0x10 int 0x10
;Repeat ;Repeat

29
SOURCE/READCH.INC Normal file
View File

@ -0,0 +1,29 @@
;Reads a character to AL
readch:
;Store the initial registers
push bx
push ax
;Read a keypress
mov ah, 0x0
int 0x16
;Check for non-printing characters
cmp al, 0x1f
jle readch
cmp al, 0x7f
je readch
;Print the character
mov ah, 0xe
int 0x10
;Load the initial registers
pop bx
mov ah, bh
pop bx
.done:
ret

View File

@ -1,4 +1,4 @@
;Reads a string of at most al characters to di until a return and prints a newline. ;Reads a string of at most AL characters to DI until a return and prints a newline.
readln: readln:

View File

@ -1,4 +1,4 @@
;Reads a string of at most al characters to di until a return. ;Reads a string of at most AL characters to DI until a return.
readstr: readstr:
@ -29,32 +29,27 @@ int 0x16
;Check for return ;Check for return
cmp al, 0xd cmp al, 0xd
je .return je .return
;Check for backspace ;Check for backspace
cmp al, 0x8 cmp al, 0x8
je .backspace je .backspace
;Check for input end ;Check for input end
pop dx pop dx
push dx push dx
cmp bl, dl cmp bl, dl
je .loop je .loop
;Check for space ;Check for space
cmp al, 0x20 cmp al, 0x20
je .space je .space
;Check for erase ;Check for erase
cmp al, 0x9 cmp al, 0x9
je .erase je .erase
;Check for non-printing characters ;Check for non-printing characters
cmp al, 0x1f cmp al, 0x1f
jle .loop jle .loop
cmp al, 0x7f cmp al, 0x7f
je .loop je .loop
.char: .character:
;Store the character ;Store the character
stosb stosb
;Print the character ;Print the character
@ -67,7 +62,7 @@ jmp .loop
.erase: .erase:
;Replace the cursor position with a space ;Replace the cursor position with a space
mov al, 0x20 mov al, 0x20
jmp .char jmp .character
.space: .space:
call .nextchar call .nextchar
@ -86,19 +81,18 @@ jmp .loop
.return: .return:
;Find and remove trailing spaces
;Go to the end of the input ;Go to the end of the input
pop ax pop ax
mov bh, 0x0 mov bh, 0x0
sub ax, bx sub ax, bx
push di push di
add di, ax add di, ax
.findtrailing: .findtrailing:
;Check for a trailing space ;Check for a trailing space
cmp byte [di], 0x20 cmp byte [di], 0x20
je .deltrailing je .deltrailing
jmp .end jmp .end
.deltrailing: .deltrailing:
;Delete a trailing space ;Delete a trailing space
mov al, 0x0 mov al, 0x0
@ -106,8 +100,8 @@ stosb
sub di, 0x2 sub di, 0x2
jmp .findtrailing jmp .findtrailing
.end:
;Move the cursor to the end of the input string ;Move the cursor to the end of the input string
.end:
pop di pop di
.findend: .findend:
cmp byte [di], 0x0 cmp byte [di], 0x0
@ -138,7 +132,6 @@ inc dl
mov ah, 0x2 mov ah, 0x2
int 0x10 int 0x10
ret ret
.nextln: .nextln:
mov ah, 0x2 mov ah, 0x2
inc dh inc dh
@ -158,7 +151,6 @@ dec dl
mov ah, 0x2 mov ah, 0x2
int 0x10 int 0x10
ret ret
.prevln: .prevln:
mov ah, 0x2 mov ah, 0x2
dec dh dec dh

View File

@ -2,13 +2,15 @@
jmp start jmp start
;Calls ;Calls
%include "BYTE2HEX.INC"
%include "CMPSTR.INC"
%include "PRINTNL.INC" %include "PRINTNL.INC"
%include "PRINTCH.INC"
%include "PRINTSTR.INC" %include "PRINTSTR.INC"
%include "PRINTLN.INC" %include "PRINTLN.INC"
%include "READCH.INC"
%include "READSTR.INC" %include "READSTR.INC"
%include "READLN.INC" %include "READLN.INC"
%include "CMPSTR.INC"
%include "BYTE2HEX.INC"
;Commands ;Commands
%include "ECHO.INC" %include "ECHO.INC"
@ -38,13 +40,11 @@ mov di, buffer
mov al, 0xff mov al, 0xff
call readln call readln
exec:
;Identify and execute the command ;Identify and execute the command
exec:
;Check for no command ;Check for no command
cmp byte [buffer], 0x0 cmp byte [buffer], 0x0
je loop je loop
.echo: .echo:
;Check ;Check
mov si, buffer mov si, buffer
@ -54,7 +54,6 @@ jnc .hello
;Execute ;Execute
call echo call echo
jmp loop jmp loop
.hello: .hello:
;Check ;Check
mov si, buffer mov si, buffer
@ -64,7 +63,6 @@ jnc .help
;Execute ;Execute
call hello call hello
jmp loop jmp loop
.help: .help:
;Check ;Check
mov si, buffer mov si, buffer
@ -74,7 +72,6 @@ jnc .keycode
;Execute ;Execute
call help call help
jmp loop jmp loop
.keycode: .keycode:
;Check ;Check
mov si, buffer mov si, buffer
@ -84,7 +81,6 @@ jnc .error
;Execute ;Execute
call keycode call keycode
jmp loop jmp loop
.error: .error:
mov si, error mov si, error
call println call println
@ -108,7 +104,6 @@ db "hello", 0x0
db "help", 0x0 db "help", 0x0
.keycode: .keycode:
db "keycode", 0x0 db "keycode", 0x0
error: error:
db "Unknown command", 0x0 db "Unknown command", 0x0

11
make.sh
View File

@ -1,14 +1,11 @@
#!/bin/bash #!/bin/bash
cd SOURCE/ cd SOURCE/
nasm BOOT.ASM -f bin -O0 -o ../BOOT.BIN nasm BOOT.ASM -f bin -o ../BOOT.BIN
nasm SYSTEM.ASM -f bin -O0 -o ../SYSTEM.BIN nasm SYSTEM.ASM -f bin -o ../SYSTEM.BIN
cd .. cd ..
if [[ -f EttinOS.img ]] rm -f EttinOS.img
then
rm EttinOS.img
fi
mkfs.fat -C EttinOS.img 1440 mkfs.fat -C EttinOS.img 1440
mcopy -i EttinOS.img SYSTEM.BIN ::
dd if=BOOT.BIN of=EttinOS.img conv=notrunc bs=512 count=1 dd if=BOOT.BIN of=EttinOS.img conv=notrunc bs=512 count=1
mcopy -i EttinOS.img SYSTEM.BIN ::