实模式汇编 I:用 NASM 构建并引导 16 位内核
文章目录
|
本文的 语气或风格 可能与整个 wiki 使用的百科全书式语气不一致。请参阅 维基百科关于语气的文章 获取建议。 |
|
本页面或章节使用 I、我、我们 或 我们 来称呼读者或编辑者。应将其 编辑 为百科全书式的语气。 |
| 难度级别 |
|---|
初级 |
|
警告: 请在继续之前参阅 实模式操作系统警告 页面。 |
在本教程中,我们将使用 NASM 汇编一个小型的 16 位 汇编语言 内核,并引导它。
概述
你可能会叹口气,然后忽略 又一篇 用 x86 汇编语言编写操作系统的教程,尤其是这一篇还使用实模式。但这一篇有所不同;它实际上不只是在屏幕上打印 "Hello World" 然后停机。
为此,你需要:
- 最新版本的 NASM(截至 2008 年 11 月 28 日为 2.05.01日)
- Windows 上的 PARTCOPY 或 Linux 上的 dd
- 一个模拟器,例如 QEMU、Bochs 或 Microsoft Virtual PC
那么它会是什么样子?
嗯,会有一个单一的源文件,即内核。那 引导程序呢?这个内核非常小,我们根本不会使用文件系统,只需将内核放入磁盘映像的前几个扇区!
系统将包含一个字符串打印调用(当然)、键盘输入,以及一个类似 C 语言的 strcmp 调用,全部打包在不到一个扇区的空间内。
但我想要图形界面、音效,还要让所有 Windows 游戏都能在我的操作系统上运行……
那么代码在哪里?
给你,尽情发挥吧。
org 0x7C00 ; add 0x7C00 to label addresses bits 16 ; tell the assembler we want 16 bit code mov ax, 0 ; set up segments mov ds, ax mov es, ax mov ss, ax ; setup stack mov sp, 0x7C00 ; stack grows downwards from 0x7C00 mov si, welcome call print_string mainloop: mov si, prompt call print_string mov di, buffer call get_string mov si, buffer cmp byte [si], 0 ; blank line? je mainloop ; yes, ignore it mov si, buffer mov di, cmd_hi ; "hi" command call strcmp jc .helloworld mov si, buffer mov di, cmd_help ; "help" command call strcmp jc .help mov si,badcommand call print_string jmp mainloop .helloworld: mov si, msg_helloworld call print_string jmp mainloop .help: mov si, msg_help call print_string jmp mainloop welcome db 'Welcome to My OS!', 0x0D, 0x0A, 0 msg_helloworld db 'Hello OSDev World!', 0x0D, 0x0A, 0 badcommand db 'Bad command entered.', 0x0D, 0x0A, 0 prompt db '>', 0 cmd_hi db 'hi', 0 cmd_help db 'help', 0 msg_help db 'My OS: Commands: hi, help', 0x0D, 0x0A, 0 buffer times 64 db 0 ; ================ ; calls start here ; ================ print_string: lodsb ; grab a byte from SI or al, al ; logical or AL by itself jz .done ; if the result is zero, get out mov ah, 0x0E int 0x10 ; otherwise, print out the character! jmp print_string .done: ret get_string: xor cl, cl .loop: mov ah, 0 int 0x16 ; wait for keypress cmp al, 0x08 ; backspace pressed? je .backspace ; yes, handle it cmp al, 0x0D ; enter pressed? je .done ; yes, we're done cmp cl, 0x3F ; 63 chars inputted? je .loop ; yes, only let in backspace and enter mov ah, 0x0E int 0x10 ; print out character stosb ; put character in buffer inc cl jmp .loop .backspace: cmp cl, 0 ; beginning of string? je .loop ; yes, ignore the key dec di mov byte [di], 0 ; delete character dec cl ; decrement counter as well mov ah, 0x0E mov al, 0x08 int 10h ; backspace on the screen mov al, ' ' int 10h ; blank character out mov al, 0x08 int 10h ; backspace again jmp .loop ; go to the main loop .done: mov al, 0 ; null terminator stosb mov ah, 0x0E mov al, 0x0D int 0x10 mov al, 0x0A int 0x10 ; newline ret strcmp: .loop: mov al, [si] ; grab a byte from SI mov bl, [di] ; grab a byte from DI cmp al, bl ; are they equal? jne .notequal ; nope, we're done. cmp al, 0 ; are both bytes (they were equal before) null? je .done ; yes, we're done. inc di ; increment DI inc si ; increment SI jmp .loop ; loop! .notequal: clc ; not equal, clear the carry flag ret .done: stc ; equal, set the carry flag ret times 510-($-$$) db 0 dw 0AA55h ; some BIOSes require this signature
在 Windows 上汇编:
nasm kernel.asm -f bin -o kernel.bin partcopy kernel.bin 0 200 -f0
或在 Linux 上:
nasm kernel.asm -f bin -o kernel.bin dd if=kernel.bin of=/dev/sda
这些命令会汇编你的内核二进制文件并将其写入第一个磁盘(sda 可能是你的系统磁盘,请根据你的配置使用 sdb、sdc 等。U 盘也会显示为某个 "sd" 设备)。现在就去测试你的操作系统吧!
接下来做什么?
当然,这取决于你!你可以添加更多命令、将操作系统扩展到另一个扇区并学习使用 BIOS 扇区加载函数、添加栈并改进调用等。
无论你决定如何编写,祝你玩得开心!
2008 年 12 月 12 日编辑:我已经在 实模式汇编 II 撰写了本教程的第二部分。本部分和后续部分将减少可复制粘贴的代码,增加更多理论内容!
<- none | Real mode assembly II ->
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Real mode assembly I;离线修订号:27894。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论