Began moving to C. (No C yet)

If you want to build the assembly version, run the buildAsm.sh script
Currently just setting up the bootloader.
This commit is contained in:
2024-01-02 14:40:32 +01:00
parent 845e33479a
commit 726247a4f2
15 changed files with 365 additions and 74 deletions
-342
View File
@@ -1,342 +0,0 @@
;
; Loads a file from the filetable into a memory location
; Params:
; dl - Disk number
; Filename (Null terminated)
; Destination location segment
; Destination location offset
;
; Returns:
; al - Exit code : 0 - Success; 1 - File not found; 2 - Disk error
;
loadFile:
; Func prologue
push bp
mov bp, sp
push bx
push fs
push cx
push dx
push si
push es
mov byte [diskNum], dl
mov bx, [bp+4] ; dest Offset
mov fs, [bp+6] ; dest Segment
mov si, [bp+8] ; Filename
mov cx, 10
mov di, fileName
.getFilename:
lodsb ; mov byte al, [si]; inc si
cmp al, 0 ; End of fileName
je .filenamePad
stosb
loop .getFilename
jmp .filenameDone
.filenamePad:
dec si ; Fill the rest with Null
.filenamePadLoop:
stosb
loop .filenamePadLoop
.filenameDone:
mov ax, FILETAB_LOC
mov es, ax
xor di, di ; es:di - Filetable location
mov si, fileName
.fileNameCheck:
mov al, [es:di]
cmp al, 0 ; End of filetable
je .notFound ; File doesn't exist, create new one
cmp al, [si]
je .beginCmp
add di, 16
jmp .fileNameCheck
.beginCmp:
push di
mov cl, 10
.cmpLoop:
mov al, [es:di]
inc di
cmp al, [si]
jne .redoCheck
dec cl
jz .found
inc si
jmp .cmpLoop
.redoCheck:
mov si, fileName
pop di
add di, 16
jmp .fileNameCheck
.notFound:
mov byte [exitCode], NOT_FOUND
jmp .done
.found:
mov al, [es:di]
inc di
cmp al, 0 ; Skip NULLs untill extension
je .found
add di, 3 ; Skip extension
inc di ; Skip Number of entries
xor ax, ax ; Reset disk
mov dl, [diskNum]
int 0x13
mov ax, fs
mov es, ax
mov ch, 0 ; Cylinder
mov dh, 0 ; Head
mov cl, [es:di] ; Sector
inc di
mov al, [es:di] ; Length in sectors
mov ah, 2 ; Read from disk
int 0x13
jnc .done
mov byte [exitCode], DISK_ERROR
.done:
; Func epilogue
pop es
pop si
pop dx
pop cx
pop fs
pop bx
mov sp, bp
pop bp
mov byte al, [exitCode]
ret
;
; Saves a file to the disk and updates filetable
; dl - Disk number
; Params:
; Filename (Null terminated)
; Filetype (Null terminated)
; Filesize (Byte)
; Source location segment
; Source location offset
;
; Returns:
; al - Exit code : 0 - Success; 1 - File not found; 2 - Disk error
;
saveFile:
; Func prologue
push bp
mov bp, sp
push bx
push fs
push cx
push dx
push si
push es
mov byte [diskNum], dl
mov bx, [bp+4] ; src Offset
mov fs, [bp+6] ; src Segment
mov cx, [bp+8] ; Filesize
mov ax, [bp+10] ; Filetype
push bx
mov byte [filesize], cl
mov si, ax
mov di, filetype
mov cx, 3
rep movsb
mov si, [bp+12] ; Filename
mov cx, 10
mov di, fileName
.getFilename:
lodsb ; mov byte al, [si]; inc si
cmp al, 0 ; End of fileName
je .filenamePad
stosb
loop .getFilename
jmp .filenameDone
.filenamePad:
dec si ; Fill the rest with Null
.filenamePadLoop:
stosb
loop .filenamePadLoop
.filenameDone:
mov ax, FILETAB_LOC
mov es, ax
xor di, di ; es:di - Filetable location
mov si, fileName
.fileNameCheck:
mov byte al, [es:di+14] ; Starting sector
cmp al, 0 ; No file, create one
je createFile
add byte al, [es:di+15] ; Filesize
mov byte [availableSector], al
mov al, [es:di]
cmp al, 0 ; End of filetable
je createFile ; File doesn't exist, create new one
cmp al, [si]
je .beginCmp
add di, 16
jmp .fileNameCheck
.beginCmp:
push di
mov cl, 10
.cmpLoop:
mov al, [es:di]
inc di
cmp al, [si]
jne .redoCheck
dec cl
jz .found
inc si
jmp .cmpLoop
.redoCheck:
mov si, fileName
pop di
add di, 16
jmp .fileNameCheck
.found: ; File already exists (override)
; TODO: Ask user if they want to override file
mov al, [es:di]
inc di
cmp al, 0 ; Skip NULLs untill extension
je .found
add di, 3 ; Skip extension
inc di ; Skip Number of entries
xor ax, ax ; Reset disk
mov dl, [diskNum]
int 0x13
; TODO: Override current file
; push fs
; mov ch, 0 ; Cylinder
; mov dh, 0 ; Head
; mov cl, [es:di] ; Sector
; inc di
; mov al, [es:di] ; Length in sectors
; mov ah, 3 ; Write to disk
; pop es ; es:bx - Destination
; int 0x13
; jnc returnToCaller
mov byte [exitCode], DISK_ERROR
jmp return ; TODO: Fix this
createFile: ; Appends a file to the filetable.
mov di, FILETAB_LOC
mov es, di
xor di, di
.filetabEnd: ; Move to the end of filetable
cmp byte [es:di], 0
je .filetabEndDone
add di, 16
jmp .filetabEnd
.filetabEndDone:
mov cx, 10
mov si, fileName
rep movsb ; Append the fileName
mov cx, 3
mov si, filetype
rep movsb ; Append the extension
mov byte [es:di], 0 ; Number of entries
inc di
mov byte al, [availableSector]
stosb ; Starting sector
mov byte al, [filesize]
stosb ; File length in sectors
; Write changed filetable to disk
;! If filetable size changes, this will have to change (probably)
mov bx, FILETAB_LOC
mov es, bx
xor bx, bx ; es:bx - Filetable location
mov dl, [diskNum]
mov ah, 3 ; Write to disk
mov al, 1 ; Write 1 sector
mov ch, 0 ; Cylinder
mov dh, 0 ; Head
mov cl, 2 ; Sector
int 0x13
jnc writeFileData
mov byte [exitCode], DISK_ERROR
jmp returnToCaller
writeFileData: ; Writes file data to the disk
mov ax, 0x8000 ; Source segment
mov es, ax
pop bx ; es:bx - Source
mov ah, 3 ; Write to disk
mov byte al, [filesize]
mov dl, [diskNum]
mov ch, 0 ; Cylinder
mov dh, 0 ; Head
mov byte cl, [availableSector]
int 0x13
jnc returnToCaller
mov byte [exitCode], DISK_ERROR ; Exit code - Disk Error
returnToCaller:
; Func epilogue
pop es
pop si
pop dx
pop cx
pop fs
pop bx
mov sp, bp
pop bp
mov byte al, [exitCode]
ret
debug:
pusha
mov ah, 0x0e
mov bl, 0x1f
mov bh, 0
.loop:
lodsb ; Load [si] into al; Increment si
or al, al
jz .done
int 0x10
jmp .loop
.done:
popa
ret
;; Variables
availableSector: db 0
exitCode: db 0
diskNum: db 0
filesize: db 0
filetype: times 3 db 0
fileName: times 10 db 0
;; Constants
FILETAB_LOC equ 0x1000
; Exit codes
SUCCESS equ 0
NOT_FOUND equ 1
DISK_ERROR equ 2
-129
View File
@@ -1,129 +0,0 @@
;
; Prints a string
; si - Pointer to NULL terminated string
;
print:
pusha
mov ah, 0x0e
mov bl, 0x1f
mov bh, 0
.loop:
lodsb ; Load [si] into al; Increment si
or al, al
jz .done
int 0x10
jmp .loop
.done:
popa
ret
;
; Prints a hex value
; dx - Value to print in Hex
;
printHex:
pusha
xor cx, cx ; Init counter
.loop:
cmp cx, 4 ; Check for loop end
je .done
mov ax, dx ; Copy dx to ax
and ax, 0x000f ; Get last nibble (4 bits) of ax
add ax, '0' ; Convert number to an ASCII number (0-9)
cmp ax, '9' ; Check if number was less than 9, meaning we can use it
jle .addChar
add ax, 0x07 ; Convert to letter (A-F)
.addChar:
mov bx, hexString + 5
sub bx, cx ; Get position of current hex character
mov [bx], al
ror dx, 4 ; Rotate bits of dx (0x1a2b -> 0xb1a2)
inc cx ; Increment the counter
jmp .loop
.done:
mov si, hexString
call print
popa
ret
;
; Prints a hex byte
; dl - Hex byte
;
printHexByte:
pusha
xor cx, cx
mov bh, 0
mov bl, 0x1f ; White on blue
.loop:
mov al, dl
and al, 0xF0
ror al, 4
add al, '0'
cmp al, '9'
jle .hexNum
add al, 7
.hexNum:
mov ah, 0x0e
int 0x10
inc cx
cmp cx, 2
je .done
ror dl, 4
jmp .loop
.done:
popa
ret
hexString: db "0x0000", 0
;
; Clears the screen in text mode
; Assumes VGA text 3 - 80x25, 16 color
;
clearText:
pusha
push es
mov ax, 0x0b800
mov es, ax
xor di, di ; es:di - b800:0000
mov al, ' ' ; Fill with spaces
mov ah, 0x17 ; White on blue
mov cx, 80*25
rep stosw
mov ah, 2
xor dx, dx ; Set cursor to 0,0
int 0x10
pop es
popa
ret
;
; Writes a string into video memory
; di - Video memory offset
; si - string
;
drawString:
pusha
push es ; For some reason doesn't get stored in pusha
mov ax, VIDMEM
mov es, ax
.loop:
cmp byte [si], 0
je .after
movsb
mov byte [es:di], 0x1f ; White on blue
inc di
jmp .loop
.after:
pop es
popa
ret
; Usefull definitions
%define ENDL 0x0a, 0x0d
VIDMEM equ 0x0b800
newline: db ENDL, 0 ; CRLF
-24
View File
@@ -1,24 +0,0 @@
resetTextMode:
pusha
mov ah, 0 ; Set mode
mov al, 0x03 ; 25x80, Text mode
int 0x10
mov ah, 0x0b ; Change color/palette
mov bh, 0x00 ; Change BG color
mov bl, 0x01 ; Blue BG
int 0x10
popa
ret
resetGraphicsMode:
pusha
mov ah, 0 ; Set mode
mov al, 0x13 ; 320x200, 256 color graphics mode
int 0x10
popa
ret