50 Comments
- srg13, on 10/10/2007, -0/+31For a little more in-depth tutorial about very basic kernel development, see http://www.osdever.net/bkerndev/index.php?the_id=90
- ropers, on 10/10/2007, -0/+31Part III: http://linuxgazette.net/issue82/raghu.html
- ropers, on 10/10/2007, -0/+31Part II: http://linuxgazette.net/issue79/krishnakumar.html
- EspressoNinja, on 10/10/2007, -1/+21As soon as I find a floppy drive, I'm going to try this out.
- beejay, on 10/10/2007, -0/+12What's really painful is x86 assembly, and it's a shame because assembly is fun and enlightening. The problem is that no one sat down and designed x86 in its current form--it has evolved over several decades to meet radically new technological needs (floating point numbers, MMX, dual core) while still maintaining backwards compatibility. So it ends up looking like a rube goldberg machine held together with duct tape.
The MIPS architecture, on the other hand, was designed by a small group of people. It's actually kind of beautiful. If you learn MIPS assembly, you will have (relative) fun, and then your skills and knowledge will transfer well to a subsequent attempt at x86. A good way to learn MIPS is the SPIM assembly simulator (http://pages.cs.wisc.edu/~larus/spim.html). Good luck! - JavertHolmes, on 10/10/2007, -0/+9Awesome series! It was reading stuff like this (albeit C-64 related stuff) when I was younger that got me into computers in the first place. May stuff like this inspire future geeks.
- tuzziel, on 10/10/2007, -3/+11"At startup, every microprocessor is just another 8086"
So every modern processor on the market today (like the latest Intel's CoreDuo CPU) operates at the very start in the 80's archaic 16bit-8086 realmode ?!. Reminds of the fact that the six week old human has a reptile's tale... - mck9235, on 10/10/2007, -2/+8Assembly language looks painful, really painful.
- GMorgan, on 10/10/2007, -0/+6Assembly language is good fun mun, especially if you start writing self modifying code. Really the biggest issue with ASM is the amount of state you have to deal with and the lack of control structures.
Fortunately you can bind up the key ASM instructions in C functions (out/in, etc) and write 99.9999% of your code in C. Unfortunately you need more ASM the closer you get to boot so there is quite a lot here.
//edit - if you really struggle with ASM then write out your function in pseudoC and hand compile it. Then you can fit in the bits that cannot be done from C without extra work or the standard library.// - greyfade, on 10/10/2007, -0/+6You don't even need one. You can do all of your prototyping with Bochs or Qemu and a floppy image.
- HouseCentipede, on 10/10/2007, -0/+5The point of this is that you are learning by doing, not that you're making something useful.
- beejay, on 10/10/2007, -0/+5Depends on your goal in doing this. If you want to learn how your OS works at the lowest level (startup, bootstrapping), read this article. If you have a useful reason to create a new operating system, adapt linux or something else.
- GMorgan, on 10/10/2007, -0/+5Yes, though it you make your kernel multiboot compliant then the boot loader should put you into protected mode before booting the kernel. I highly recommend looking at the multiboot specification and the multiboot data structure for anyone interested in writing their own kernel from scratch. By using Grub you can gain access to all sorts of useful information like a sane way of probing memory (which does all the BIOS interrupt nonsense for you).
It's a nice short cut and allows you to avoid a lot of headaches. - GMorgan, on 10/10/2007, -0/+4Nothing has changed in that time. Basic kernel development is about getting out of real mode, setting up GDT's, IVT's and IRQ's, setting up the PIT, writing a memory mapped VGA driver and a keyboard driver. Then you get into setting up virtual memory with page directories and tables (maybe segmentation if you are insane). Finally you start producing things like semaphores (or spinlocks if you are multicore) and all the other things a kernel will need.
- greyfade, on 10/10/2007, -0/+4Yes, and they even go so far as to emulate the keyboard controller on A20. (Which you have to enable to get out of the 1MB area.)
- GMorgan, on 10/10/2007, -0/+4Yeah, I recommend making it so your build script will automatically generate a floppy image with the new kernel on it. Saves you plenty of grunt work.
- kinghajj, on 10/10/2007, -0/+4Yes, that's absolutely true. If you want to enable "unreal" mode, protected mode, long mode, etc., you have to explicitly tell the processor. x86/x64's main constraint, after all, is to be 100% backward-compatible, which is why things can get so messy.
- greyfade, on 10/10/2007, -0/+3Once you learn a few mnemonics, it's really quite easy to use. When you work with it for a few months, you'll probably find yourself deliberately using side-effects of instructions without even thinking about it. Programming at this level is very powerful.
- LordofShadows, on 10/10/2007, -0/+3I saw this a loooong time ago! Dugg.
- inactive, on 10/10/2007, -0/+3Say that to the never-taking-off 64 bits architecture. I know it is x86 compatible but is just to show you how do we manage 'some drop in backward compatibility'.
- Weebs43, on 10/10/2007, -0/+3I'm not 100% sure, but most likely you should be able to just create a floppy image (I can't think of any programs to do it at the moment though) and boot it up in your favorite Virtual Machine software (VMWare, Bochs, etc).
- JoshuaGross, on 10/10/2007, -0/+3You'd have better luck with Bochs or Qemu.
- maybeway36, on 10/10/2007, -0/+3it's smaller.
- erkokite, on 10/10/2007, -0/+2I used that very same tutorial. Wonderful stuff. By far the most complete tutorial on kernel development that I have ever seen.
- Stonekeeper, on 10/10/2007, -1/+3It was the template for Vista
- pr1268, on 10/10/2007, -0/+2Thanks for the article. Not just this one, but several others on linuxgazette.net look like interesting reads...
- GMorgan, on 10/10/2007, -0/+2Sorry for the DP but if anyone actually wants to look at some ASM I have a few recommendations:
1. Write a simple C program (no libc) then run the command "gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -S -o prog.asm prog.c" this will convert your C into ASM. One warning, it is AT&T ASM and that is not as well documented as the Intel version. GCC tends to add so much Gnuism to it's ASM that this probably won't assemble with anything other than gas.
2. Write a C program that calls an ASM function. This enables you to mix and match so you don't have to deal with all ASM at once. You'll probably have to deal with how to get the return value (a quick and dirty hack might be an extern pointer, global in the C code).
3. Write a self modifying program that alters a jmp instructions address to simulate a function return, then jmp to the function. Push the return value/s onto the stack before the return jmp (in reality the return address tends to be pushed onto the stack as well, we don't use self modifying code for function calls).
Lots of fun things you can do with ASM. NASM is probably the best assembler. - GMorgan, on 10/10/2007, -0/+2I can vouch for this site. I've followed that particular tutorial myself.
- Curufir, on 10/10/2007, -1/+3In my experience you need at least a few resets on a floppy before it reads a sector properly. Doesn't make any difference on virtual hardware, but on a real floppy drive (admittedly mine is ancient) you WILL need some error checking and resetting.
Use 'as' out of the binutils package. It can do 16-bit x86 assembly, you probably have it already and it will save your sanity as you can avoid the braindead Intel operand order.
It's bad karma to anything other than a long jump as your first instruction. The difference between 07c0:0000 and 0000:7c00 will come back and bite you eventually.
Plus, have fun. You have 510 bytes to play with in a boot sector and it is AMAZING how much you can fit in there once you know what you're doing. - GMorgan, on 10/10/2007, -0/+2Get a floppy image (there is one somewhere on the site linked in the first comment) and then treat the image as if it were the floppy itself for all the instructions (i.e. modify the C program to point at your image rather than "/dev/fd0"). Then point your VMware floppy drive at the image.
- pak314, on 10/10/2007, -0/+1Assembly language may be powerful but it is not always productive compared to higher level languages like C for your average code. What you really gain from knowing assembly is a better understanding of how a compiler generates code so you can you can help it along in generating cleaner code.
- maybeway36, on 10/10/2007, -1/+2knoppix?
- deadbaby, on 10/10/2007, -1/+2There's nothing wrong with legacy support. That's the main reason x86 has been the dominate processor for the last 25 years. Breaking all that compatibility just isn't worth it yet.
- PhireN, on 10/10/2007, -0/+1Or, if your computer supports it, you could boot off a usb flash drive, or even use grub to load a boot image off your hard drive.
- smeenz, on 10/10/2007, -0/+1Probably the same way - just connect the real floppy drive to the vm machine and have it boot from t here.
- shadowspawn, on 10/10/2007, -0/+1I friggin love assembly. i like this article.
- crossers, on 07/16/2008, -0/+0it's grate article. thanks!
http://www.shpe-sac.org
http://www.ocflex.com/
http://www.trgovinca.org
http://www.chasr.org/ - geminitojanus, on 10/10/2007, -1/+1Heh, MIPS was designed by scientists for students. It's great if you're learning, but once you get into the realm of reality, you realize it's RISC with a capital R.
And as for your first comment, look at AMD64/EM64T/64-bit x86. They've basically went in and removed all of the cruft added 8 registers to make it feel like a recent processor and not as you so aptly called it "a Rube Goldberg machine held together with duct tape." It's still nowhere as elegant as PPC, but its performance is better in the desktop sector so I'll take it. Would have rather have seen them do 24 more registers and officially deprecated the old hold-over modes (with a given compatibility date for full removal, like say 3 years). - inactive, on 10/10/2007, -2/+2God, its been over 20 years, I think we can get away with dropping some backward compatibility now. Most any of that crap would have to be emulated anyways.
- BLOODSUCKER, on 10/10/2007, -3/+3This is so dugg!
Thanks for this great article. - SSUK, on 10/10/2007, -1/+1Needs some Windows-based compilers... =/
- Iam9376, on 10/10/2007, -0/+0You can use nasm.
- Garou, on 10/10/2007, -4/+3simian primate tail, not reptile tale tuzziel
- headzoo, on 10/10/2007, -2/+1Hrm.. anyone know how to create this, in such a way that it can be booted into a VM using VMWare?
- geminitojanus, on 10/10/2007, -2/+1"So every modern processor on the market today (like the latest Intel's CoreDuo CPU) operates at the very start in the 80's archaic 16bit-8086 realmode"
Err.. no, not exactly. All modern /x86/ processors on the market today operates like a legacy 80[2+]86 in real mode at boot. Some firmwares (like EFI and some hypervisors) are smart enough to put the system in protected mode before handing over control to the OS.
And this code won't work at all for ARM, PPC, etc. Most of us who have at least a degree in Computer Science would have already done everything here in some shape or form during the coursework (whether or not it was for x86 or for a MIPS board, the freshman hacker's standby, is a whole other question). - Jeffler, on 10/10/2007, -2/+1Considering this system wouldn't be impressive in _1982_, I don't think it matters.
- stealthc, on 10/10/2007, -8/+5Why write your own toy OS when Apple has already made one for you? =P
/mac owner angst - thomasprebble, on 10/10/2007, -8/+3Look out Windows! LOL (sarcastic).
- mikedoth, on 10/10/2007, -6/+1Would it be faster to just strip down a copy of Linux?
- inactive, on 10/10/2007, -10/+5Has anyone noticed this article was made way back in 2002??? =/


What is Digg?
Digg is coming to a city (and computer) near you! Check out all the details on our