flat assembler
Message board for the users of flat assembler.

Index > OS Construction > 16Bits OS Bootloader and Kernel ? Or Stage 2 here?

Author
Thread Post new topic Reply to topic
Kitsune



Joined: 17 Apr 2023
Posts: 8
Kitsune 19 Nov 2023, 23:15
Hi,

I have fond some code online and assembly it in 2 files (Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,

It boot from a floppy for now..And I want a way to put this kind of Kernel on a disk and make it bootable but I don't know how to do it and I need a tool first for format hard disk before copying it I think if the disk is not formated...
...So I don't find a tutorial about this kind of manipulation in assembly. Is there a book I can read it's talking about OS development through assembly language (16bits for now) It can be an old school book.

Here the files tested on PCEm:

Bootloader.asm
Code:
use16
ORG 0x7C00

mov si, text_string
call print_string

jmp $

text_string db 'Launching KitsuneOS. Loading Kernel...Please Wait.',13,10,0

print_string:
        mov ah, 0x0E ; int 10h 'print char' function
_repeat:
        lodsb
        cmp al,0
        je loadkern     ; If char is zero, end of string
        int 10h                 ; Otherwise, print it
        jmp _repeat

loadkern: 
cld                ; Clear direction flag to ensure instructions like
                   ;    lodsb use forward movement.

xor ax, ax         ; AX=0 (use for segment)
mov ss, ax
mov sp, 0x7c00     ; Set up stack to grow down from 0x0000:0x07c00
                   ;    in region below the bootloader

mov es, ax         ; Read sector starting at 0x0000:0x7e00 (ES:BX)
mov bx, 0x7e00

mov ah, 2
mov al, 1
mov ch, 0
mov cl, 2
mov dh, 0
; mov dl, 0        ; Don't force to zero. Use value in DL passed by BIOS
                   ;     to our bootloader
int 13h

jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00

times 510 - ($-$$) db 0
dw 0xAA55
    


Kernel.asm
Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
                   ;     0x07e0:0x0000 also represents phys addres 0x07e00
                   ;     0x07e0<<4 + 0x0000 = 0x7e00

; jmp main         ; Not necessary since main is next instruction

main:
mov ax, 0x07e0
mov ds, ax         ; Make sure segments are 0x07e0
mov es, ax

mov si, msg
call print
cli                ; Do CLI/HLT after we return from print
hlt                ;    Prevent code from getting beyond this point

print:
lodsb
cmp al, 0
je done
mov ah, 0eh
int 10h
jmp print
done:
ret

;Place data here. msg is a message we want to print
msg: db "Kernel Loaded. Welcome to Kitsune OS!"
    


Compile it on Linux with:
Code:
fasm bootloader.asm boot.bin
fasm kernel.asm kernel.bin
dd if=boot.bin of=KitsuneOS.img bs=512 conv=notrunc
dd if=kernel.bin of=KitsuneOS.img bs=512 conv=notrunc seek=1
    

_________________
Kitsune
Post 19 Nov 2023, 23:15
View user's profile Send private message Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 338
Location: Poland
macgub 20 Nov 2023, 20:16
Just curious - what hardware you have???
If you are cd/dvd owner - you can burn cd in floppy emulation mode.. Many software tools have this option (for sure ex. CD burner XP WIndows tool).
Post 20 Nov 2023, 20:16
View user's profile Send private message Visit poster's website Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 66
Location: Belgium
SeproMan 20 Nov 2023, 21:15
You found this code online, but I doubt it's a good example!

Code:
cld                ; Clear direction flag to ensure instructions like
                   ;    lodsb use forward movement.

xor ax, ax         ; AX=0 (use for segment)
mov ss, ax
mov sp, 0x7c00     ; Set up stack to grow down from 0x0000:0x07c00
                   ;    in region below the bootloader

mov es, ax
    


The above instructions really belong to directly below the 'ORG 0x7C00' directive. Also the 'mov ds, ax' instruction is still missing, and the print_string procedure relies on it being set up properly!



Do notice that the first comment in KERNEL.ASM

Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
    


does not match the far jump in the BOOTLOADER.ASM

Code:
jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00
    



Quote:
Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,


The bootloader code must fit in only 512 bytes. If your kernel is to be any serious, then it won't be possible to stay within those 512 bytes. So yes, you need 2 files.

_________________
Real Address Mode.
Post 20 Nov 2023, 21:15
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 8
Kitsune 20 Nov 2023, 21:50
macgub wrote:
Just curious - what hardware you have???
If you are cd/dvd owner - you can burn cd in floppy emulation mode.. Many software tools have this option (for sure ex. CD burner XP WIndows tool).


@macgub
I have a laptop computer with Debian Linux installed on it for compile the asm code and create floppy from command line, and PCem as a virtual machine software.


SeproMan wrote:

...
The above instructions really belong to directly below the 'ORG 0x7C00' directive. Also the 'mov ds, ax' instruction is still missing, and the print_string procedure relies on it being set up properly!

Do notice that the first comment in KERNEL.ASM

Code:
use16
org 0x0000         ; Since CS will be 0x07e0 we need an origin of 0x0000
    


does not match the far jump in the BOOTLOADER.ASM

Code:
jmp 0x0000:0x7e00  ; Jump to physical address 0x07e00 by setting CS
                   ;     to 0 and IP to 0x7e00
    



Quote:
Bootloader and "Kernel" or Stage 2 don't really know if I must use a second boot file really,


The bootloader code must fit in only 512 bytes. If your kernel is to be any serious, then it won't be possible to stay within those 512 bytes. So yes, you need 2 files.



@SeproMan
Have you ressources I can learn about 16bits OS creation in assembly?

This source code works despite what you said about the jump to adress command
I have tested it.

In the meantime, I found a Youtube "tutorial" on creating an OS, maybe that's a better way to learn it.

But if you have good books about this kind of knowlege (16bits OS in assembly in DOS era) it's welcome!

_________________
Kitsune
Post 20 Nov 2023, 21:50
View user's profile Send private message Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 66
Location: Belgium
SeproMan 20 Nov 2023, 22:21
Good online resources include:

A more theoretical approach: https://wiki.osdev.org/Bootloader

A more hands-on approach: https://stackoverflow.com/questions/tagged/assembly
Then use the search function filling in [Bootloader][x86-16]
You'll see that many of the best answers there were contributed by users Michael Petch and Sep Roland, (amongst others of course).
On the stackoverflow site you can also ask for help with your programming troubles.


Quote:
This source code works despite what you said about the jump to adress command
I have tested it.


I did not say that anything was wrong with the far jump itself. It's the comments that go along with it that are contradicting each other. Wink

_________________
Real Address Mode.
Post 20 Nov 2023, 22:21
View user's profile Send private message Reply with quote
Kitsune



Joined: 17 Apr 2023
Posts: 8
Kitsune 20 Nov 2023, 23:03
The above code is from this page:
https://forum.osdev.org/viewtopic.php?f=1&t=32452
It's on the same website you mentionned.

I will look at these links.
Thanks you for the ressources and help.

Good night.
Post 20 Nov 2023, 23:03
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.