Search code examples
assemblybinaryoperating-systemhex-editorsmbr

What environment can I use to write binary code of the operating system?


For learning bootstrapping of operating systems I do some simple tests this way:

  • I install oracle viratualbox and create hdd disk

  • I install hex-editor HxD and write code to this hdd disk, opening the file which presents this hdd

In the end of first 512-byte sector I write 55 AA in 1FE and 1FF bytes consiquently,

and other code I write from first byte of the first sector.

In this way I must unblock hdd file from HxD, because virtualbox can't start it until this is done.

I want to use a virtual machine or another real machine (the second way is less convenient), because it creates an independent development environment.

How can I more efficiently do this tests for learning bootstrapping (and after simple developing) operating system ?


Solution

  • When I do this sort of development I build a disk image from scratch and point the VM at it as a floppy disk. In this way, the output of your assembler, the object file, can be a complete boot sector for a floppy and you can easily chain load further sectors. For example:

    ;   x86 architecture systems all support MBR style boot sectors.  An
    ;   MBR boot sector must be 512 bytes in length and have machine
    ;   language code originating at 0000:7c00.  Additionally, it must
    ;   have the signature "0x55aa" as the final word in the sector or it
    ;   is not a valid boot sector.
    
    
    
    org 0x7c00                  ; BIOS will load the MBR to this location 
                                ; and then jump here to continue execution
    
    ; Your code here!
    
                                ; As stated above, the boot sector must 
    times   510-($-$$) db 0     ; Create padding to fill out to 510 bytes
    dw      0xaa55              ; Magic number in the trailer of a boot sector
                                ; We write it as 0xaa55 because we're little
                                ; endian and it will be reversed to the required
                                ; 0x55 0xaa
    

    Simply add your initial code. Create a link to the object file that is named "floppy.img" or something like that and then tell VirtualBox where to find it. Voila!

    You didn't ask, but I'm hoping that you can see that you can actually put all of your code in this file; simply add the code to be chain loaded from later sectors after the 0xaa55 and you can simply load it to memory because you know that it falls at the beginning of the next sector.