Previous lesson Table of Contents Next lesson]
In this lesson we will learn to use an assembler to write our programs. In previous lessons we have assembled them using DEBUG. After playing around with this for a while, you will quickly see that it would be a pain to use DEBUG to create a program of more than a handful of instructions (even harder to modify). We need a simpler way. We will start by using the Netwide Assembler” (NASM). Follow that link to the official web page and download a copy of the assembler.
Now we’ll use this assembler to create the same “operating system” that we did at the end of Lesson 2. Save the following boot program as h.asm
;----------------------------------------------------------------------
; Simple boot program that prints the letter 'H'
; and then hangs
; Joel Gompert 2001
;
; Disclaimer: I am not responsible for any results of the use of the contents
; of this file
;----------------------------------------------------------------------
org 0x7c00 ; This is where BIOS loads the bootloader
; Execution begins here
entry:
jmp short begin ; jump over the DOS boot record data
; ----------------------------------------------------------------------
; data portion of the "DOS BOOT RECORD"
; ----------------------------------------------------------------------
brINT13Flag DB 90H ; 0002h - 0EH for INT13 AH=42 READ
brOEM DB 'MSDOS5.0' ; 0003h - OEM name & DOS version (8 chars)
brBPS DW 512 ; 000Bh - Bytes/sector
brSPC DB 1 ; 000Dh - Sectors/cluster
brResCount DW 1 ; 000Eh - Reserved (boot) sectors
brFATs DB 2 ; 0010h - FAT copies
brRootEntries DW 0E0H ; 0011h - Root directory entries
brSectorCount DW 2880 ; 0013h - Sectors in volume, < 32MB
brMedia DB 240 ; 0015h - Media descriptor
brSPF DW 9 ; 0016h - Sectors per FAT
brSPH DW 18 ; 0018h - Sectors per track
brHPC DW 2 ; 001Ah - Number of Heads
brHidden DD 0 ; 001Ch - Hidden sectors
brSectors DD 0 ; 0020h - Total number of sectors
DB 0 ; 0024h - Physical drive no.
DB 0 ; 0025h - Reserved (FAT32)
DB 29H ; 0026h - Extended boot record sig
brSerialNum DD 404418EAH ; 0027h - Volume serial number (random)
brLabel DB 'Joels disk ' ; 002Bh - Volume label (11 chars)
brFSID DB 'FAT12 ' ; 0036h - File System ID (8 chars)
;------------------------------------------------------------------------
; --------------------------------------------
; Boot program code begins here
; --------------------------------------------
; boot code begins at 0x003E
begin:
mov ah, 0x0e ; Function to print a character to the screen
mov al, 'H' ; Which character to print
mov bl, 7 ; color/style to use for the character
int 0x10 ; print the character
hang:
jmp hang ; just loop forever.
;---------------------------------------------
size equ $ - entry
%if size+2 > 512
%error "code is too large for boot sector"
%endif
times (512 - size - 2) db 0
db 0x55, 0xAA ;2 byte boot signature
The first instruction should be somewhat familiar by now. This is the instruction to jump over the Boot Record data. In this case, it’s a jump to the label begin . After the jump instruction is 20 bytes of data. This is the data that I read off my floppy disk using the DEBUG program. These values should work fine. If you want, try replacing the data with the data from your own disk. Most of it should be the same.
(NOTE: Keep in mind that numbers made up of more than 1 byte will look “byte swapped” when viewed in DEBUG because on the Intel architecture, the least significant byte is stored at the lowest memory address and vice versa. The bytes will look backwards.)
The code starting at the label
beginshould look similar to the code we wrote for Lesson 2. It simply prints the letter ‘H’ to the screen and loops forever. At the bottom of this file you will see first a check to make sure the code all fits within 512 bytes (the size of one sector), then the line beginning with the word “times” adds zeros to the end of the file to pad the executable to 510 bytes. Finally, the two-byte signature 0x55, 0xAA is added to the end of the file. Assemble the file at the command prompt with the following command.
nasmw h.asm –o h.bin
This assembles the assembly file to a pure binary executable h.bin. Check the size of the binary file. If we have done things right, it should be exactly 512 bytes. This is exactly the size needed to fit in the boot sector of the floppy.
Now we need to copy this file onto our floppy. With the floppy in the disk, run DEBUG, and type the following commands
debug -n h.bin -l 0
This loads our file into memory starting at address 0. Use the dump (d) and unassemble (u) commands if you wish to confirm that our file has assembled and been loaded correctly. You will be able to see the few instructions that we have written. Notice that the file has been correctly padded with zeros up until bytes 0x1FE and 0x1FF at the end of the sector. Also note that DEBUG fills the CX register with the number of bytes loaded from the file. (Display the contents of the registers with the r command.)
With the floppy disk in the drive, write the file to the disk with the usual command.
-w 0 0 0 1
Reboot the computer with the floppy and see the program in action. Try some more things with the source code, now. For example, maybe modify the code to print more characters. When you are ready, proceed to the next lesson where we will create a “Hello, World” operating system.
Previous lesson Table of Contents Next lesson
No comments:
Post a Comment