My FirstOS

This forums is for OS project announcements including project openings, new releases, update notices, test requests, and job openings (both paying and volunteer).
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

My FirstOS

Post by kendfrey »

Hey anyone who wants to feel free to test this bootloader. I made it from the ground up. I would be particularly interested in ways to improve the code. It comes with a simple kernel that prints a message and hangs. You may certainly substitute your own kernel. Tested in Bochs, not on real hardware yet.

Outline:
KERNEL.BIN loads and begins execution at 0x1000:0x0000 in real mode. ds and es are 0x1000, ax, bx, cx, dx, di and si are 0x0000. The screen is in text mode, and filled with spaces with color attribute 0x07. The cursor is at the top left corner of the screen.

Floppy image: floppy.img

Bootloader (MASM):

Code: Select all

.186
.model small
.code

kernel segment at 1000h
org 0
start label far
kernel ends

org 7c00h

first:
jmp short Main
nop

OSName            db "FIRSTOS "
BytesPerSector    dw 512
SectorsPerCluster db 1
ReservedSectors   dw 1
NumberOfFATs      db 2
RootDirEntries    dw 224
NumberOfSectors   dw 2880
MediaType         db 0f0h
SectorsPerFAT     dw 9
SectorsPerTrack   dw 18
NumberOfHeads     dw 2
HiddenSectors     dd 0
DWSectors         dd 0
DriveNumber       db 0
Reserved          db 0
BootSignature     db 29h
SerialNumber      dd 0
VolumeLabel       db "FOSBOOTDISK"
FileSystem        db "FAT12   "

Main:
; set up segments & stack
cli
xor ax, ax
mov ds, ax
mov es, ax
jmp es:MainSetCS
MainSetCS:
mov ss, ax
mov sp, 0f000h
sti
; load root directory
mov ax, 0500h
mov es, ax
mov cx, 4
MainRetryRoot:
push cx
xor ax, ax
mov al, [NumberOfFATs]
mul [SectorsPerFAT]
add ax, [ReservedSectors]
mov [LBAFirstData], ax
call LBAToCHS
mov ax, 0020h
push dx
mul [RootDirEntries]
div [BytesPerSector]
pop dx
add [LBAFirstData], ax
mov ah, 02h
xor bx, bx
int 13h
pop cx
jnc MainSuccessRoot
loop MainRetryRoot
int 18h
MainSuccessRoot:
; read root directory
mov cx, [RootDirEntries]
xor di, di
MainNextEntry:
push cx
mov cx, 11
mov si, offset KernelFile
push di
repe cmpsb
pop di
pop cx
je MainEntryFound
add di, 0020h
loop MainNextEntry
int 18h
MainEntryFound:
add di, 26
mov ax, es:[di]
mov [Cluster], ax
; load FAT
mov cx, 4
MainRetryFAT:
push cx
mov ax, [ReservedSectors]
call LBAToCHS
mov ax, [SectorsPerFAT]
mov ah, 02h
int 13h
pop cx
jnc MainSuccessFAT
loop MainRetryFAT
int 18h
MainSuccessFAT:
; read FAT and load clusters
MainReadCluster:
mov ax, 1000h
mov es, ax
mov cx, 4
MainRetryCluster:
push cx
mov ax, [Cluster]
sub ax, 2
xor dx, dx
mov dl, [SectorsPerCluster]
mul dx
add ax, [LBAFirstData]
call LBAToCHS
mov al, [SectorsPerCluster]
mov ah, 02h
int 13h
pop cx
jnc MainNextCluster
loop MainRetryCluster
int 18h
MainNextCluster:
mov ax, 0500h
mov es, ax
mov ax, [Cluster]
mov dx, ax
shr ax, 1
add ax, dx
push bx
mov bx, ax
mov ax, es:[bx]
pop bx
test dx, 0001h
jnz MainOddCluster
and ax, 0fffh
jmp MainClusterNumber
MainOddCluster:
shr ax, 4
MainClusterNumber:
add bx, [BytesPerSector]
mov [Cluster], ax
cmp ax, 0ff0h
jb MainReadCluster
; clear screen and move cursor
mov ax, 0b800h
mov es, ax
mov ax, 0720h
xor bx, bx
MainNextChar:
mov es:[bx], ax
add bx, 2
cmp bx, 4000
jb MainNextChar
xor bx, bx
xor dx, dx
mov ah, 02h
int 10h
; set up registers and call kernel
mov ax, kernel
mov ds, ax
mov es, ax
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
xor di, di
jmp kernel:start

LBAToCHS:
xor dx, dx
div [SectorsPerTrack]
mov cl, dl
inc cl
xor dx, dx
div [NumberOfHeads]
mov ch, al
mov dh, dl
mov dl, [DriveNumber]
ret

KernelFile db "KERNEL  BIN"
LBAFirstData dw 0
Cluster dw 0
;db (510 - ($ - first)) dup (0)
;dw 0aa55h
end
Kernel (MASM):

Code: Select all

.model tiny
.code
mov si, offset msg
Next:
lodsb
or al, al
jz Done
mov ah, 0eh
int 10h
jmp Next
Done:
jmp $

msg db "In the kernel! :)", 0
end
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: My FirstOS

Post by Combuster »

Code: Select all

jmp es:MainSetCS
That does not at all do what you think it does.


Edit: Actually it gets assembled as:

Code: Select all

jmp MainSetCS
nop 
nop
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

You're right! e9 02 00 90 90
How do I set the CS register then?
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

I can't find it on the wiki. I searched and browsed, no luck.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: My FirstOS

Post by Chandra »

kendfrey wrote:You're right! e9 02 00 90 90
How do I set the CS register then?
Read the processor manual. There are different ways to do this.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

Where would I find that information?
shikhin
Member
Member
Posts: 274
Joined: Sat Oct 09, 2010 3:35 am
Libera.chat IRC: shikhin
Contact:

Re: My FirstOS

Post by shikhin »

Hi,
Chandra wrote:Read the processor manual. There are different ways to do this.
kendfrey wrote:Where would I find that information?
Perhaps the processor manual?

Regards,
Shikhin
http://shikhin.in/

Current status: Gandr.
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

I meant, where would I find the processor manual?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: My FirstOS

Post by bluemoon »

kendfrey wrote:I meant, where would I find the processor manual?
Is that so difficult to search the f***ing manual?
http://www.lmgtfy.com/?q=intel%20manual
http://www.lmgtfy.com/?q=amd%20manual

Are you sure to engage for OS development mission? Check this out: Getting_Started
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

I googled and didn't find anything that really said "This is the manual". I was kinda confused. I will download the 5 volume manual from Intel's software development manuals page and hope I can find something in there.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: My FirstOS

Post by DavidCooper »

kendfrey wrote:I googled and didn't find anything that really said "This is the manual". I was kinda confused. I will download the 5 volume manual from Intel's software development manuals page and hope I can find something in there.
What you really need is a manual for MASM rather than for the processor, and someone at http://www.masm32.com/board/index.php? should be able to help you find it or maybe even take the trouble to solve your problem directly. For help using your tools, always ask at forums related to those tools first and save OSDev for issues related to actual OS development.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

I couldn't find any such thing in the license. I did see that it says no open source software, but not anything about developing OS's.
Darwin
Member
Member
Posts: 43
Joined: Sun Jan 16, 2011 6:58 pm
Location: United States

Re: My FirstOS

Post by Darwin »

berkus wrote:Also note that MASM license explicitly forbids to use it for OSdev.
Not according to the wiki page for MASM.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: My FirstOS

Post by JAAman »

@berkus, kendfrey
you are both wrong, because both of you have been reading the wrong license

this is a common myth... there is nothing in the MASM license forbidding its use for OSdev

there is a section in the MASM32 license forbidding the use of MASM32 in OSdev (#4 under the heading: what can you not do with the MASM32 project) but those are in fact, 2 completely separate products, created by 2 completely separate companies:

MASM: the Microsoft Asembler, used by MS compilers, and also a free download from the Microsoft website, nothing in its license forbidding its use for anything other than simple redistributing itself (basically, you cant make a program that is nothing more than the code MS provides for you, and distribute that as your own product -- also, the downloadable version also forbids commercial use, for that, use the version that comes with the for-pay VS Pro)


MASM32: a non-Microsoft add-on product sometimes used with MASM for developing windows programs in assembly, includes a license forbidding its use in developing anything not intended to run under windows, and forbidding its use to create GPL software (note that the masm32 website states that it forbids "open source" but that is not true, it only forbids open source software using certain licenses)
kendfrey
Member
Member
Posts: 45
Joined: Mon Oct 17, 2011 7:44 am

Re: My FirstOS

Post by kendfrey »

I don't see how MASM32 forbids developing operating systems. It forbids developing "software for Non-Microsoft Operating Systems". I am not doing that. I am not developing software thats runs in an operating system, therefore I am not developing software that runs in a non-Microsoft operating system. (Since a non-MS OS is an OS.) The only thing I can see that would be restricting me is using MASM32 for developing software for my operating system.
Post Reply