03 以組語撰寫 boot sector

1
2
3
4
5
6
7
8
; Infinite loop (e9 fd ff)
loop:
jmp loop

; Fill with 510 zeros minus the size of the previous code
times 510-($-$$) db 0
; Magic number
dw 0xaa55

組譯:

1
$ nasm -f bin boot_sect.asm -o boot_sect.bin
  • -f 為 format
  • -o 為 output file

這樣就可以產生 01 Boot Process 用 binary 寫的 512 byte 的 disk image 並且最後兩個 byte 值是 0xaa55。可以用 xxd 看一個檔案的 hex dump。

最後用 QEMU 執行!

1
$ qemu-system-x86_64 boot_sect.bin

可以在畫面看到 Booting from Hard Disk... 然後停住。

16-bit Real Mode

現代 CPU 都不是 16-bit 了,但為了 backward compatibility,CPU 一啟動都是在模擬出來的 16-bit real mode,之後才會轉到 32-bit 或 64-bit protected mode。

當我們說 16-bit CPU 時,表示 instruction 在一個 cycle 中一次最多只能處理 16 bit。