2014-09-29 09:43:20 +00:00
|
|
|
*Concepts you may want to Google beforehand: interrupts, CPU
|
|
|
|
registers*
|
|
|
|
|
2014-10-05 16:39:45 +00:00
|
|
|
**Goal: Make our previously silent boot sector print some text**
|
|
|
|
|
2014-09-29 09:43:20 +00:00
|
|
|
We will improve a bit on our infinite-loop boot sector and print
|
|
|
|
something on the screen. We will raise an interrupt for this.
|
|
|
|
|
|
|
|
On this example we are going to write each character of the "Hello"
|
|
|
|
word into the register `al` (lower part of `ax`), the bytes `0x0e`
|
|
|
|
into `ah` (the higher part of `ax`) and raise interrupt `0x10` which
|
2014-09-29 09:54:35 +00:00
|
|
|
is a general interrupt for video services.
|
2014-09-29 09:43:20 +00:00
|
|
|
|
2014-09-29 09:54:35 +00:00
|
|
|
`0x0e` on `ah` tells the video interrupt that the actual function
|
2014-09-29 09:54:58 +00:00
|
|
|
we want to run is to 'write the contents of `al` in tty mode'.
|
2014-09-29 09:43:20 +00:00
|
|
|
|
|
|
|
We will set tty mode only once though in the real world we
|
|
|
|
cannot be sure that the contents of `ah` are constant. Some other
|
|
|
|
process may run on the CPU while we are sleeping, not clean
|
|
|
|
up properly and leave garbage data on `ah`.
|
|
|
|
|
|
|
|
For this example we don't need to take care of that since we are
|
|
|
|
the only thing running on the CPU.
|
|
|
|
|
|
|
|
Our new boot sector looks like this:
|
|
|
|
```nasm
|
|
|
|
mov ah, 0x0e ; tty mode
|
|
|
|
mov al, 'H'
|
|
|
|
int 0x10
|
|
|
|
mov al, 'e'
|
|
|
|
int 0x10
|
|
|
|
mov al, 'l'
|
|
|
|
int 0x10
|
|
|
|
int 0x10 ; 'l' is still on al, remember?
|
|
|
|
mov al, 'o'
|
|
|
|
int 0x10
|
|
|
|
|
|
|
|
jmp $ ; jump to current address = infinite loop
|
|
|
|
|
|
|
|
; padding and magic number
|
|
|
|
times 510 - ($-$$) db 0
|
|
|
|
dw 0xaa55
|
|
|
|
```
|
|
|
|
|
|
|
|
You can examine the binary data with `xxd file.bin`
|
|
|
|
|
|
|
|
Anyway, you know the drill:
|
|
|
|
|
2021-12-17 21:24:54 +00:00
|
|
|
`nasm -f bin boot_sect_hello.asm -o boot_sect_hello.bin`
|
2014-09-29 09:43:20 +00:00
|
|
|
|
|
|
|
`qemu boot_sect_hello.bin`
|
|
|
|
|
|
|
|
Your boot sector will say 'Hello' and hang on an infinite loop
|