How can I make my kernel load files?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
stevej150
Posts: 23
Joined: Wed Jan 25, 2017 8:16 am

How can I make my kernel load files?

Post by stevej150 »

Hey guys. I'm new to the forum! 8)

I'm following a tutorial (only to start learning more about Assembly by playing around with code) which is DonkerOSYD. I want to make the kernel load a BIN file.

This is KERNEL.ASM

Code: Select all

BITS	16

Main:

cli

mov	ax, 0x0000

mov	ss, ax

mov	sp, 0xFFFF

sti



mov	ax, 2000h

mov	ds, ax

mov	es, ax

mov	fs, ax

mov	gs, ax



mov	[BootDrive], dl



setup_bg:

mov	dx, 0

call	move_cursor



mov	ah, 09h

mov	al, ''

mov	bh, 0

mov	bl, 00000111b

mov	cx, 2400

int	10h



mov	si, version

call	Print



call	newline



mov	si, copyright

call	Print



cmd:

call newline



mov	si, prompt

call	Print



mov	di, input_buffer



mov	al, 0

mov	cx, 256

rep	stosb



mov	ax, input_buffer

mov	di, input_buffer



.loop:

call	keyboard



cmp	al, 13		;ENTER

je	.done



cmp	al, 8		;BACKSPACE

je	.backspace



jmp	.character



.backspace:

mov	ah, 0Eh

mov	al, 8

int	10h

mov	al, 32

int	10h

mov	al, 8

int	10h

dec	di

jmp	.loop



.character:

mov	ah, 0Eh

int	10h

stosb

jmp	.loop



.done:

mov	ax, 0

stosb



call	newline



mov	si, input_buffer

cmp	BYTE [si], 0



je	cmd



;All built-in commands

mov	di, help_string

call	compare

jc	help



mov	di, help_uc

call	compare

jc	help



mov	di, dir_string

call	compare

jc	dir



mov	di, dir_uc

call	compare

jc	dir



mov	di, exit_string

call	compare

jc	exit



mov	di, exit_uc

call	compare

jc	exit



;what if it's not a built-in command?

mov	si, input_buffer

call	Print



mov	si, no_command

call	Print



jmp	cmd



%INCLUDE "variables.asm"

%INCLUDE "functions.asm"

%INCLUDE "cli_commands.asm"
This is functions.asm

Code: Select all

newline:

pusha

mov	ah, 0Eh

mov	al, 13

int	10h

mov	al, 10

int	10h

popa

ret



Print:

lodsb

cmp	al, 0

je	Done

mov	ah, 0eh

int	10h

jmp	Print



Done:

ret



move_cursor:

pusha

mov	ah, 02h

mov	bh, 0

int	10h

popa

ret



keyboard:

pusha

mov	ax, 0

mov	ah, 10h

int	16h

mov	[.buffer], ax

popa

mov	ax, [.buffer]

ret



.buffer	dw	0



compare:

pusha



.loop:

mov	al, [si]

mov	ah, [di]



cmp	al, ah

jne	.not_equal



cmp	al, 0

je	.equal



inc	si

inc	di

jmp	.loop



.not_equal:

popa

clc

ret



.equal:

popa

stc

ret



reset_floppy:

mov	ax, 0

mov	dl, BYTE [BootDrive]

int	13h

ret



convert_sector:

push	bx

push	ax

mov	bx, ax

mov	dx, 0

div	WORD [.SectorsPerTrack]

add	dl, 01h

mov	cl, dl

mov	ax, bx

mov	dx, 0

div	WORD [.SectorsPerTrack]

mov	dx, 0

div	WORD [.Sides]

mov	dh, dl

mov	ch, al

pop	ax

pop	bx

mov	dl, BYTE [BootDrive]

ret



.SectorsPerTrack	dw	18

.Sides	dw	2



os_string_uppercase:

	pusha



	mov si, ax			; Use SI to access string



.more:

	cmp byte [si], 0		; Zero-termination of string?

	je .done			; If so, quit



	cmp byte [si], 'a'		; In the lower case A to Z range?

	jb .noatoz

	cmp byte [si], 'z'

	ja .noatoz



	sub byte [si], 20h		; If so, convert input char to upper case



	inc si

	jmp .more



.noatoz:

	inc si

	jmp .more



.done:

	popa

	ret
And cli_commands.asm

Code: Select all

buffer	equ 24576



dir:

pusha

mov	di, dirlist



call	reset_floppy



mov	ch, 0

mov	cl, 2

mov	dh, 1

mov	bx, buffer

mov	al, 14

mov	ah, 2

pusha

load_root_dir:

int	13h

jnc	loaded_root_dir

call	reset_floppy

jmp	load_root_dir



loaded_root_dir:

popa

mov	si, buffer



compare_entry:

mov	al, [si+11]

cmp	al, 0Fh			;Windows marker

je	.skip_entry

cmp	al, 18h			;Directory marker

je	.skip_entry

cmp	al, 229			;deleted file

je	.skip_entry

cmp	al, 0

je	.done



mov	dx, si

mov	cx, 0



.save_character:

mov	BYTE al, [si]

cmp	al, ' '

je	.space

mov	BYTE [di], al

inc	di

inc	si

inc	cx

cmp	cx, 8

je	.add_dot

cmp	cx, 11

je	.string_copied

jmp	.save_character



.add_dot:

mov	BYTE [di], '.'

inc	di

jmp	.save_character



.space:

inc	si

inc	cx

cmp	cx, 8

je	.add_dot

jmp	.save_character



.string_copied:

mov	BYTE [di], ','

inc	di

mov	si, dx



.skip_entry:

add	si, 32

jmp	compare_entry



.done:

mov	si, dirlist

mov ah, 0Eh

.print:

lodsb

cmp	al, 0

je	.done_printing

cmp	al, ','

je	.comma

int	10h

jmp	.print



.comma:

call	newline

jmp	.print



.done_printing:

popa

jmp	cmd





help:

mov	si, help_message

call	Print

jmp	cmd



exit:

mov	ax, 5307h

mov	cx, 3

mov	bx, 1

int	15h
How would I make it so it loads files?

Thanks
Steve.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: How can I make my kernel load files?

Post by dozniak »

Since you're writing an application that uses BIOS, use BIOS to load files - load boot sector, detect the filesystem, load necessary sectors of the filesystem with file directory, find the file there and load file's sectors in memory, one after another.
Learn to read.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: How can I make my kernel load files?

Post by BrightLight »

You know your OS is advanced when you stop using the Intel programming guide as a reference.
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: How can I make my kernel load files?

Post by Boris »

Hi,
Linux can be booted by many protocols. However, multiboot is not the most common one.
You can look at https://github.com/torvalds/linux/blob/ ... 6/boot.txt for reference.

This may not be what you want for your kernel.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: How can I make my kernel load files?

Post by Love4Boobies »

No one is going to review that code for you. If you have a bug you can't figure out, bring it down to a test case (which should be a few lines long and written in a clear style, unlike the mess above) and maybe someone will look over it. If you're not sure how to implement something or don't understand some piece of theory, ask specific questions about it. All we can do is try to guide you a little bit in the right direction but you have to do pretty much all of the work yourself (or pay someone to do it for you).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
stevej150
Posts: 23
Joined: Wed Jan 25, 2017 8:16 am

Re: How can I make my kernel load files?

Post by stevej150 »

What code could I use to load a file?
One day in the future... computers will be holograms...
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: How can I make my kernel load files?

Post by dozniak »

stevej150 wrote:What code could I use to load a file?
Disk driver code and filesystem code. These two should be generally enough.
Learn to read.
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

Re: How can I make my kernel load files?

Post by azblue »

stevej150 wrote:What code could I use to load a file?
Look up FAT bootloaders; they're easy to find and easy to understand. If you don't want to use FAT or if you have trouble understanding it, make your own rudimentary file system. Just think through what you need: Your files need names, those names need to be stored somewhere, the files themselves need to be stored somewhere, upon finding the name of a file you're looking for, you have to have a way to find where that file is stored. Just think it through; if you know how to load sectors you can create your own filesystem and bootloader.

(And if you don't know how to load sectors, look into Ralf Brown's Interrupt List, int 13h. Be aware that LBA won't work on floppies or devices emulated as floppies).
Post Reply