bootloader 比较枯燥,主要是对各个寄存器进行设置,而后进行 BIOS 的 int10H 调用。须要用到一些汇编的知识,这里简要记录一些要点。函数
第十七个中断向量(interrupt vector),一般在实模式用于设置显示服务。须要配合 AH 一块儿使用,指定其子函数。this
AH = 06H,向上滚动窗口
AL = 00H,这时开启清屏功能
BH 指定颜色属性,其他寄存器可暂时忽略(07即为黑底白字)spa
AH = 02H
BH 为页码
DH 为行数
DL 为列数3d
AH = 13H
AL 为写入模式
BH 为页码
BL 为颜色
CX 存放字符串长度
DH 为游标坐标行号
DL 为游标坐标列号
ES:BP 须要设置为字符串的偏移地址code
org 指令,设置程序的起始段,避免再须要的地方手动就设置 0x7c00(主要影响绝对地址寻址指令)。
最简单的显示字符串程序共用了代码断和数据段/extra 段,所以数据段放到了最后。blog
org 0x7c00 ; set origin as 0x7c00 mov ax, cs mov es, ax ; es is equal to cs in this case ; using int10h ah=06, al = 0 to clear screen mov ax, 0600h mov bx, 0700h; black background and white color mov cx, 0000h mov dx, 0xffff int 10h ; using int10h, ah = 02h to set focus mov ax, 0200h mov bx, 0000h mov dx, 0000h int 10h ; show string mov ax, 1301h; AL = 01 indicates that after display string, the cursor will be the end mov bx, 0007h mov dx, 0000h mov cx, 10; length of string mov bp, DisplayString int 10h jmp $ DisplayString: db "Hello Boot" times 510 - ($ - $$) db 0 db 0x55, 0xaa
使用 nasm 编译,qemu 运行便可看到屏幕的输出ip
nasm -f bin boot.asm -o boot.binqemu-system-x86_64 boot.bin字符串