Real mode assembly I: Difference between revisions

no edit summary
[unchecked revision][unchecked revision]
(Added "use16" line. Fixed segments, so memory below the bootloader can be used)
No edit summary
 
(10 intermediate revisions by 7 users not shown)
Line 1:
{{Tone}}
{{FirstPerson}}
{{You}}
{{Warning|Please see the [[Real Mode OS Warning]] page before continuing.}}
{{Rating|1}}
 
Line 4 ⟶ 8:
 
== Overview ==
 
'''EDIT #2: I've put a little "series box" at the bottom of each page to allow quick access to the previous and next tutorial in the series.'''
 
You're probably going to sigh and dismiss yet ''another'' tutorial on writing operating systems in [[x86]] assembly language, especially since this one uses real mode. But there's a catch to this one; it actually does more than printing "Hello World" to the screen and halting.
Line 15 ⟶ 17:
 
== So what's it going to look like? ==
Well, there will be a single source file, the kernel. What about a [[bootloader]]? This is such a small kernel, we're not going to use a filesystem at all, we're just going to put the kernel into the first few sectors of the floppydisk image!
 
The system will have a string printing call (of course), keyboard input, and a strcmp call similar to that of C, all packaged into less than a sector.
Line 25 ⟶ 27:
Here you go, go wild.
 
<sourcesyntaxhighlight lang="asm">
org 0x7C00 ; add 0x7C00 to label addresses
use16 bits 16 ; tell the assembler we want 16 bit code
 
mov ax, 0 ; set up segments
Line 33 ⟶ 35:
mov es, ax
mov ss, ax ; setup stack
mov sp, 0xFFF00x7C00 ; stack grows downwards from 0xFFF00x7C00
mov si, welcome
Line 181 ⟶ 183:
times 510-($-$$) db 0
dw 0AA55h ; some BIOSes require this signature
</syntaxhighlight>
</source>
 
To assemble on Windows:
 
<sourcesyntaxhighlight lang="dosbat">
nasm kernel.asm -f bin -o kernel.bin
partcopy kernel.bin 0 200 -f0
</syntaxhighlight>
</source>
 
Or on Linux:
<sourcesyntaxhighlight lang="bash">
nasm kernel.asm -f bin -o kernel.bin
dd if=kernel.bin of=/dev/fd0sda
</syntaxhighlight>
</source>
 
Those commands assemble your kernel binary and write them to the first floppydisk (sda might be your system disk, so use sdb, sdc etc. according to your configuration. USB-sticks also appear as one of the "sd" devices). Go ahead and test out your operating system now!
 
== What next? ==
Why, that's up to you of course! You could add more commands, expand your OS to another sector and learn to use the BIOS floppysector load functions, add a stack and improve the calls, etc.
 
Have fun with your OS, however you decide to write it!
Line 205 ⟶ 207:
'''EDIT on December 12 2008: I've written a second part to this tutorial at [[Real mode assembly II]]. This and future parts will have less code to copy and paste and more theory!'''
 
[[Category:Assembly]]
[[Category:Real mode assembly]]
<- none | [[Real mode assembly II]] ->