OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:42 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 38 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: My FirstOS
PostPosted: Thu Oct 27, 2011 5:20 pm 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
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:
.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:
.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


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 2:26 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Code:
jmp es:MainSetCS
That does not at all do what you think it does.


Edit: Actually it gets assembled as:
Code:
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 ]


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 6:19 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 7:42 am 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
I can't find it on the wiki. I searched and browsed, no luck.


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 8:27 am 
Offline
Member
Member
User avatar

Joined: Sat Jul 17, 2010 12:45 am
Posts: 487
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 !


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 8:38 am 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
Where would I find that information?


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 8:55 am 
Offline
Member
Member

Joined: Sat Oct 09, 2010 3:35 am
Posts: 274
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.


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 8:58 am 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
I meant, where would I find the processor manual?


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 9:01 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
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


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 9:18 am 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
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.


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 12:48 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2010 4:53 pm
Posts: 1150
Location: Scotland
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


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 2:00 pm 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
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.


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 3:34 pm 
Offline
Member
Member

Joined: Sun Jan 16, 2011 6:58 pm
Posts: 43
Location: United States
berkus wrote:
Also note that MASM license explicitly forbids to use it for OSdev.

Not according to the wiki page for MASM.


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 3:35 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
@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)

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
 Post subject: Re: My FirstOS
PostPosted: Fri Oct 28, 2011 3:46 pm 
Offline
Member
Member

Joined: Mon Oct 17, 2011 7:44 am
Posts: 45
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 31 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group