ACHI OS
Overview
Introduction
The AchiOS kernel is written in 16-bit x86 real mode assembly language. This provides easy access to BIOS routines for handling the keyboard, screen and floppy drive, so that we don’t need complicated drivers. Therefore most of the code is focused on actual OS aspects: loading programs, system calls and so forth.
Additionally, AchiOS avoids the real mode segmentation complications by existing in a single 64K segment. The first 32K (0–32767) of RAM is reserved for the kernel; the second 32K is for external program code and memory.
Structure
These are the most important files and directories in the AchiOS zip file:
- source/ — Contains the entire OS source code
- source/bootload/ — Source to generate BOOTLOAD.BIN, which is added to the disk image when building
- source/features/ — Components of AchiOS such as FAT12 support, string routines, the BASIC interpreter etc.
- source/kernel.asm — The core kernel source file, which pulls in other source files
- programs/ — Source code for programs added to the disk image
Memory map
This is the makeup of the 64K memory segment after AchiOS has loaded:
0–24575 (hex: 0h — 5FFFh)
24K kernel executable code
24576–32767 (hex: 6000h — 7FFFh)
8K kernel disk operation buffer
32768–65535 (hex: 8000h — FFFFh
32K space for external programs
So, the first 32K is devoted to the AchiOS kernel code and its 8K buffer for performing disk operations. After that we have another 32K of memory, this time for external programs. These are loaded at the 32K point and hence need to be ORGed to 32768 as described in the App Developer Handbook.
Code path
When the PC starts up, it loads the bootblock, BOOTLOAD.BIN, that was inserted into the first sector (512 bytes) of the floppy disk image by the build script. It loads this at memory location 31744 (7C00h in hex) and begins executing it.
BOOTLOAD.BIN then scans the floppy disk for KERNEL.BIN and loads it at memory location 2000h:0000h. Here, the 2000h is the segment and 0000h is the offset in that segment — you don’t need to concern yourself with this, but effectively it means that the kernel is loaded at location 131072 (128K) in the PC’s RAM. (You get a complete memory location by multiplying the segment by 16 and adding the offset.)
Once the bootloader has loaded the kernel, it jumps to memory location 131072 (aka 2000h:0000h) to begin executing it. After this, for simplicity we ignore segments and just use offsets (0000h to FFFFh), thereby giving us 64K of RAM to use.
At the start of the kernel we have a series of jmp instructions. Why are these here? Well, the system calls are in semi-random places in the kernel executable. As AchiOS evolves, the exact locations of these system calls shifts around; an external program can’t guarantee where they are. So we have a list of vectors right at the very start of the kernel which jmp to these calls, so an external program can call these vectors and know that they’ll never move!
There’s a jmp just before these vectors to skip over them, and then the main kernel execution starts, setting up various things and offering the user a choice of a program list or command line interface.
Building
Build requirements: the NASM assembler, dosfstools package, ‘mkisofs’ utility and root access. We need root access because we loopback-mount the floppy disk image to insert our files.
Get Darshana Seneviratna’s stories in your inbox
Join Medium for free to get updates from this writer.
To build AchiOS, open a terminal and switch into the expanded AchiOS package. Enter sudo bash in Ubuntu-flavoured distros, or just su in others, to switch to the root user. Then enter:
./build-linux.shThis will use NASM to assemble the bootloader, kernel and supplied programs, then write the bootloader to the achios.flp floppy disk image in the disk_images/ directory. (It writes the 512-byte bootloader to the first sector of the floppy disk image to create a boot sector and set up a DOS-like filesystem.) Next, the build script loopback-mounts the achios.flpimage onto the filesystem — in other words, mounting the image as if it was a real floppy. The script copies over the kernel (kernel.bin) and binaries from the programs/ directory, before unmounting the floppy image.
With that done, the script runs the ‘mkisofs’ utility to generate a CD-ROM ISO image of AchiOS, injecting the floppy image as a boot section. So we end up with two files in the disk_images/ directory: one for floppy disks and one for CD-Rs. You can now use them in an emulator or on a real PC as described in the Running section above.
Modifying
Overview
To test out code, the simplest approach is to create a new app in the programs/ directory, build AchiOS and run your program. You can then be guaranteed that your code isn’t interfering with the kernel code (at least at the assembling stage!). When you’re happy with it, you may want to introduce it into source/kernel.asm or a specific file in source/features/.
Note that the files in source/features/ correspond to the system calls in programs/achidev.inc (and detailed in the App Developer Handbook), but those source files also include internal calls that are used by the kernel and are not accessible to user programs. So use ‘grep’ or the search tool of your choice to find specific calls if they’re not available through the API.
System calls
Adding new system calls is easy and fun — it extends AchiOS! So if you want to help out, this is the best way to start. Open up source/features/screen.asm in a text editor and paste in the following after the header text:
All system calls should start with pusha and end with popa before ret: this stores registers on the stack at the start, and then pops them off at the end, so that we don’t end up changing a bunch of registers and confusing the calling program. (If you’re passing back a value, say in AX, you should store AX in a temporary word and drop it back in between the popa and ret, as seen in os_wait_for_key in keyboard.asm.)
The body of our code simply places the location of our message string into the SI register, then calls another AchiOS routine, os_print_string. You can freely call other routines from your own system call.
Once we’ve done this, we can access this routine throughout the kernel. But what about external programs? They have no idea where this call is in the kernel! The trick we use is vectors — a bunch of jmp instructions at the start of our kernel code, which jump to these routines. Because these vectors are at the start, they never change their position, so we always know where they are.
For instance, right now, your new system call may be at 0x9B9D in the kernel. But if you add another call before it, or someone else does, it may move to 0x9FA6 in the kernel binary. We simply don’t know where it’s going to be. But if we put at vector at the start of our kernel, before anything else happens, we can use that as the starting point as the vector will never move!
Open up source/kernel.asm and scroll down to the list of system call vectors. You can see they start from 0003h. Scroll to the bottom of the list and you’ll see something like this:
Let’s add a vector to our new call. Add this beneath the existing vectors:
How do we know this jmp is at 00D2h in the kernel binary? Well, just follow the pattern in the jmps above — it’s pretty easy to guess. If you’re unsure, you can always use ndisasm to disassemble the kernel and look for the location of the final jmp in the list.
That’s all good and well, but there’s one last thing: people writing external programs don’t want to call an ugly number like 00C9h when they run our routine. They want to access it by name, so we need to add a line to achidev.inc in the programs/ directory:
Now, any program that includes achidev.inc will be able to call our routine by name. Et voila: a brand new system call for AchiOS!
Patches
If you’ve made some improvements or additions to AchiOS and wish to submit them, great! If they’re small changes — such as a bugfix or minor tweak — you can paste the altered code into an email. Explain what it does and where it goes in the source code, and if it’s OK, we’ll include it.
If your change is larger (eg a system call) and affects various parts of the code, you’re better off with a patch. On UNIX-like systems such as Linux, you can use the diff command-line utility to generate a list of your changes. For this, you will need the original (release) source code tree of AchiOS, along with the tree of your modified code. For instance, you may have the original code in a directory called achios-4.2/ and your enhanced version in new-achios-4.0/.
Switch to the directory beneath these, and enter:
diff -ru achios-4.2 new-achios-4.2 > patch.txtThis collates the differences between the two directories, and directs the output to the file patch.txt. Have a look at the file to make sure it’s OK (you can see how it shows which lines have changed), and then attach the file to an email.








