Writing OS-Level Code in Assembly Language
for the Intel ix86 / IBM PC computer

Edited by Don Stoner, Revision 2014/03/23 (Always Under Construction)

The author cannot be held responsible for any property damage, loss of data, or loss of health or life that might result from using this information. I'm still trying to sort this stuff out myself.

Since so few people write operating systems, it can be relatively difficult to locate good documentation on how to interface to all of the necessary parts of the computer. These links are intended to be a step toward fixing that problem. Similar attempts to document this information (which you should also check out) can be found: Here, here, here, and here, you can download a simple open-source assembly-language operating system (described as a "learning tool").

What You Will Need:

To write and test OS-level code you will need to have complete control of the computer. This means you can't work under LINUX, Windows, or any other 32-bit, protected- mode operating system. Plain old fashioned 16-bit real-mode DOS turns out to be the right operating system to use. You can use MS DOS if you like it and own a copy, or you can use FreeDOS if you want a newer version of DOS which is both good and free. A FreeDOS boot CD burnable-image can be downloaded off the net here.

You will also need an editor and an assembler which run under DOS. The Tiny Editor (TED) is a free text editor for DOS. You can dowload a copy of it here. The Netwide Assembler (NASM) is a free ix86 assembler for DOS. You can download a copy of it here.

Since you will probably be crashing your system a few times, you will want to use a dedicated computer. You can usually find low-cost and expendable (but still quite good) used computers here (their "more info" link really means "more types of used computers"). You will also want to keep everything you write backed up. Plan on trashing your HDD a few times when you start doing sector writes. (Completely debug all of the reads before you try.) When you start trying to switch to 32-bit protected mode, plan on triple-fault crashing the machine more times than you will want to count.

Setting up a DOS computer:

Warning: Here we are assuming that there is absolutely no information, that you will want to keep, on the targeted hard disk drive; once you have performed the following series of steps, there will certainly be nothing left:

To set up a hard disk to run MS DOS (typically 6.22) starting from a blank disk (or recovering from a complete crash), some or all of the following steps may be necessary:
1) Boot the computer from a bootable DOS floppy (which contains the FDISK and FORMAT programs).
2) Run "FDISK /MBR" (details here). This is only necessary if the Master Boot Record somehow gets messed up; otherwise you can skip this step. The "FDISK /MBR" function is "undocumented" because it isn't needed except by those of us who invalidate warranties as a hobby.
3) Run "FDISK". This step isn't needed as long as you are still able to format your C: partition with the Format command (below). FDISK is a simple menu-driven program which will set up normal DOS partitions for you. (Each partition acts like a different drive, with its own directory etc.) First, remove all existing partitions, one at a time; Then create a single "active" DOS partition (using as much space as is available - up to the 2-gig limit imposed by DOS). You can create more partitions if you plan to make use of more than 2 gigs of space on the disk.
4) Run "FORMAT C:/S" This destroys everything on the active partition (the C: drive). It also formats that partition with blank directories and blank File Allocation Tables (FATS). Then the "/S" option makes it install the DOS operating system (I know: that means "Disk Operating System operating system") so the disk will boot up with DOS. This step isn't needed as long as DOS still boots and runs correctly. You will probably also want to copy your AUTOEXEC.BAT and CONFIG.SYS files from the root directory of the boot floppy to the root directory of your newly created C: drive. (Or you might want to design your own files or edit the old ones to suit the intended use of this new computer; it is a good idea to keep memory-resident applications to a bare minimum, since the point of this exercise is to control the computer's resources yourself.)
5) Finally, make a DOS directory (use "MD DOS"), and copy all of your DOS files over to that directory (using "COPY A:*.* C:\DOS\"). You will probably also want to add your selected text editor and the NASM assembler to this directory. Most entry-level DOS reference books (e.g. DOS for Dummies) will explain these (and other) commands well enough to get you up to speed quickly.

Learning Assembly Language:

And here, I am attempting to put together a sequence of very short assembly-language programs which you can study. Unit1 is the source code for the simplest assembly-language program I could imagine. Subsequent units build in complexity in very small steps. I hope this will help get you started: Unit1, Unit2, Unit3, Unit4, Unit5, Unit6, Unit7, Unit8, Unit9, Unit10, Unit11, Unit12, Unit13, Unit14, Unit15, Unit16, Unit17, Unit18, Unit19, Unit20, Unit21.


And finally, here are some links and books to take you farther:

Intel ix86 Processor Assembly Language Documentation:
ix86 Assembly Overview
Links to Webster (Assembly language Tutor) are here and here
Here is another tutorial
Books:
Linux Assembly Language Programming (Prentice Hall Open Source Technology) (Paperback) (This one has a Linux bias but it's a pretty OS-independent machine-level starter book.)
The Assembly Programming Master Book (Paperback)
(This one has a Windows bias, is twice as thick, and seems to be a little less starter and more detail on how to cope with the Windows OS stuff.)
Windows Assembly Language and Systems Programming: 16- and 32-Bit Low-Level Programming for the PC and Windows (Paperback)
(This one also looked interesting to me; but I haven't really checked it out)
The Art of Assembly Language
Write Great Code: Volume 1: Understanding the Machine
Write Great Code, Volume 2: Thinking Low-Level, Writing High-Level

Intel ix86 Manuals:
Online from Intel
OR older and shorter:
Volume 1 (Dead link)
Volume 2
Volume 3
Books:
Intel Architecture Software Developer's Manual, Volume 2 : Instruction Set Reference (Paperback)
I use this volume for reference pretty constantly; though I notice Amazon doesn't seem to have any of this old edition at the moment. I suppose the new edition would be better anyhow:
IA-32 Intel Architecture Software Developer's Manual, Vol. 1: Basic Architecture(Basic Architecture, Volume 1) (Paperback)
IA-32 Intel Architecture Software Developer's Manual Volume2: Instruction Set Reference. (2002 INTEL CORP., Intel)
IA-32 Intel Architecture Software Manual: Volume 3: System Programming Guide (Paperback)
(See IA-64 links at the very bottom of this page.)

Other Books Covering the Processor:
Pentium Processor System Architecture (2nd Edition)(PC System Architecture Series) (Paperback)
Pentium Pro and Pentium II System Architecture (2nd Edition) (PC System Architecture Series) (Paperback)
The Unabridged Pentium 4: IA32 Processor Genealogy

Switching to Protected 32-bit mode:
What is Protected Mode?
Protected-mode demo 1
Protected-mode demo 2
Protected-mode demo 3
Books:
Protected Mode Software Architecture (PC System Architecture Series) (Paperback)

System Architecture (Books):
ISA System Architecture (3rd Edition) (PC System Architecture Series) (Paperback)
PCI System Architecture (4th Edition) (PC System Architecture Series)(Paperback)

Phoenix BIOS:
Phoenix BIOS (4.0, Rev.6) User's Manual (.pdf)
The Phoenix BIOS Enhanced Disk Drive Specification
Books:
System BIOS for IBM PCs, Compatibles, and EISA Computers (2nd Edition) (Paperback)

Other Disk Stuff:
Low Level Hard Disk Operations
Int 13h (disk)
FAT: General Overview of Disk Format
Writing a simple program which boots from a disk (like an O.S. does)
And here is a simple open-source assembly-language operating system (a greatly expanded version of the booting program from the previous line).
Books:
Memory Resident Utilities, Interrupts and Disk Management with M.S.and P.C.-DOS (Management Information Systems) (Paperback)
Advanced MS-DOS Programming: The Microsoft Guide for Assembly Language and C Programmers
MS-DOS Encyclopedia

Disk File Formats:
The DOS MZ .EXE File Format
The PE (Portable Executable) .EXE File Format
Creating the smallest possible PE executable
A Tour of the Win32 PE File Format
In-Depth Look into the Win32 PE File Format, Part 1
In-Depth Look into the Win32 PE File Format, Part 2
PE File Format Specification

Plug and Play BIOS:
PnP BIOS Specification
PnP BIOS Clarification (Dead link)
PnP BIOS Clarification (Download)
PnP ISA Specification
PnP Parallel Port Devices
PnP External COM Device Specification
MicroSoft Pages
Books:
Plug and Play System Architecture (PC System Architecture Series) (Paperback)

Interrupt Reference:
Interrupt Services DOS, BIOS, EMS and Mouse A
Interrupt Services DOS, BIOS, EMS and Mouse B (Dead link)
Here's the "standard reference" on interrupts
Common BIOS Interrupts are: 10h (video), 13h (disk), 14h (comm), 16h (keybd), and 17h (printer).
Books:
PC Interrupts : A Programmer's Reference to BIOS, DOS, and Third-Party Calls

Disk and Interrupt Reference (Book):
Advanced MS-DOS Programming: The Microsoft Guide for Assembly Language and C Programmers

Display:
VESA BIOS Extensions Standard Version 3.0 (Dead link)
VESA BIOS Extensions Standard Version 2.0 (Older version)
Introduction to Vesa Programming
Free VGA Home Page
INT 10h (video)
Graphics File Formats
Graphics Programming Black Book
Books:
Programmer's Guide to the EGA, VGA, and Super VGA Cards (3rd Edition)
AGP System Architecture (2nd Edition) (PC System Architecture Series) (Paperback)
Michael Abrash's Graphics Programming Black Book (Paperback)

Mouse and Keyboard:
General Mouse Information
The PS/2 Mouse/Keyboard Protocol
The PS/2 Mouse Interface
The PS/2 Keyboard Interface
PS/2 Keyboard Chip (8042) Registers (Dead link)
INT 16 (keyboard Services)
INT 33h (Mouse, under DOS - not part of BIOS)

Printer:
INT 17h (printer)
PnP Parallel Port Devices
Parallel port tutorial
DB25 pin assignments and printer cables

Comm Ports:
INT 14h (comm)
DB9 pin assignments and RS-232 cables

USB:
USB In a Nutshell
Books:
Universal Serial Bus System Architecture (2nd Edition) (PC System Architecture Series) (Paperback)
USB Mass Storage: Designing and Programming Devices and Embedded Hosts (Paperback)
USB Design by Example: A Practical Guide to Building I/O Devices (2nd Edition)(Emphasis on designing peripherals; reviews warn of non-working code examples)
USB Explained (Brief USB overview; most of book is reference for other buses)

Sound Card:
SoundBlaster Notes (Dead link)
Sound Blaster Series Hardware Programming Guide
Datasheet for YMF754 PnP-compatible Sound Chip (Download Link)

Pseudo-Random Number Generation:
Wikipedia article.
Very-long-sequence table (pdf)

Ethernet Stuff:
Cat5 Cable
Crossover Cable
Wiring RJ45 Connectors
Embedded Ethernet and Internet Complete (Book)
Datasheet for RTL8019AS PnP-compatible Ethernet Controller Chip (Download Link)

General OS design stuff:
Operating Systems: Design and Implementation (3rd Edition) Detailed explanation + source-code CD for MINIX (simpler precursor to LINUX)
Operating Systems: Design and Implementation (2nd Edition) Earlier edition; lower price and probably simpler still
Operating Systems: Design and Implementation Earlier yet (but slightly scarce)
Linux Kernel Development (3rd Edition) Same thing, but for LINUX
Linux Kernel Development (2nd Edition)
Linux Kernel Development
Operating System Concepts, (8th Edition) Overview of Operating Systems in general, with a tighter focus on several.
Operating System Concepts, (7th Edition)
Operating System Concepts

A "Lateral Thinking" Approach to Efficient OS Design:
Thinking Forth
Starting FORTH: Introduction to the FORTH Language and Operating System for Beginners and Professionals (Prentice-Hall software series)


Current pricing on relevant Amazon.com books: