7

我一直在尝试设计一个简单的操作系统,只有引导扇区和带中断的 16 位实模式。我终于能够制作我在虚拟盒中测试过的操作系统/引导加载程序,并且它工作正常。

然后,我将映像刻录到 CD 上,并将其引导到我的旧桌面,使用 Pentium 4、BIOS 修订版 A05 和 1GB 内存,它运行良好 - 一个简单的操作系统,在顶部打印一个“标题”屏幕,它允许您在屏幕上键入,并注册几个键来导航光标。

然后我将光盘插入我使用了 1 年的笔记本电脑,配备 i5 处理器、2.6 GB 内存和 A05 BIOS 修订版,光标似乎随机移动,高速打印随机字符,最后停在 anscii 字符235(扩展字符表的一部分),此时键盘工作正常,指定移动光标的键工作正常,只是标题。这是我在上面测试、编译、写入和刻录 CD 的计算机。(我使用的是 Linux Mint 12 操作系统)

我已经跳过了所有我认为我需要做的“箍”:制作了一个遵循 El Torito 'no-emulation' 引导标准、引导签名、512 字节并写入正确扇区的 iso 映像。

是我的代码有问题,我没有做某事,还是这只是正常的?

这是我的代码(NASM x86 语法):

    ;**************************
    ; Note OS, Experimental OS
    ;**************************
    [org 0x7C00]
    [bits 16]
    start:
    jmp loader                           ;jump to the actual start of bootloader
    times 8 - ($ - $$) db 0          ;pad eight bytes

    ;*********************
    ;El Torito Boot Info Table
    ;*********************

    ;in nasm, I couldn't figure out how to reserve bytes, in the middle of .text
    ;so I zeroed it out.
    times 56 db 0

    loader:
    call cls                ;clear the screen
    mov si, head1           ;setup page headers
    call printf 
    mov si, head2
    call printf
    jmp note                ;start note program

    cls:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10                ;set pos
    ret

    printf:
    .loop                   ;our function that loops
    mov al, [si]            ;load byte
    cmp al, 0               ;if null, end
    je .end
    mov ah, 0x0E            ;function 0E
    mov bh, 0x00            ;page 0x00
    mov bl, 0x0F            ;white text on black background
    int 0x10                ;print
    inc si                  ;increment source index
    jmp .loop               ;repeat
    .end
    ret                     ;return

    ;*******************
    ; Note 'Program'
    ;*******************

    note:
    mov ah, 0x00            ;function 00
    int 0x16                ;get character
    cmp al, '`'             ;go up line?
    je setcur
    cmp al, 0x0D            ;enter?
    je setent
    cmp al, '+'             ;plus?
    je setplu
    cmp al, '-'             ;minus?
    je setminu
    cmp al, '\'             ;reset?
    je loader
    cmp al, 0x08            ;backspace?
    je setback
    mov ah, 0x0E            ;function 0E
    mov bh, 0x00            ;page 00
    mov bl, 0x0F            ;white on black
    int 0x10                ;print
    jmp note                ;repeat

    setcur:
    mov ah, 0x03            ;get cur pos
    mov bh, 0x00            ;page 00
    int 0x10                ;get pos
    cmp dh, 0x00            ;are we at top of page?
    je .begin               ;just reset cursor if so
    sub dh, 0x01            ;go up one line
    .begin
    mov dl, 0x00            ;set to beginning of line
    mov ah, 0x02            ;set cursor function
    mov bh, 0x00            ;page 00
    int 0x10                ;set position
    jmp note                ;read next character

    setent:
    mov ah, 0x0E            ;write character
    mov al, 0x0A            ;begin line
    mov bh, 0x00            ;page 00
    mov bl, 0x0F            ;white on black
    int 0x10                ;print

    setplu:
    mov ah, 0x03            ;get cursor pos
    mov bh, 0x00            ;page 0x00
    int 0x10                ;get pos
    mov ah, 0x02            ;set cursor pos
    add dl, 0x01            ;add one to column
    int 0x10                ;set new pos
    jmp note                ;get next char

    setminu:
    mov ah, 0x03            ;get cursor pos
    mov bh, 0x00            ;page 00
    int 0x10                ;get pos
    mov ah, 0x02            ;set cursor pos
    sub dl, 0x01            ;sub one to column
    int 0x10                ;set new pos
    jmp note                ;get next char

    setback:
    mov ah, 0x03            ;get cursor pos
    mov bh, 0x00            ;page 00
    int 0x10                ;get pos
    mov ah, 0x02            ;set cursor pos
    sub dl, 0x01            ;sub one column
    int 0x10                ;set pos
    mov ah, 0x0E            ;write char
    mov al, ' '             ;write space
    mov bh, 0x00            ;page 00
    mov bl, 0x0F            ;white on black
    int 0x10
    mov ah, 0x02            ;reset cur pos
    int 0x10                ;reset
    jmp note

    ;******************
    ; Our Page Headers
    ;******************
    head1: db '- Note OS Version 1.2-', 0x0A, 0x0D, 0
    head2: db '=======================', 0x0A, 0x0D, 0x0A, 0x0D, 0

    times 510 - ($ - $$) db 0
    dw 0xAA55

供参考(我参考的东西):

Anscii 表:http ://www.asciitable.com/

El-Torito 信息:http ://wiki.osdev.org/El-Torito

编辑:这是我编程的键以及它们的作用:

enter - 现在正常工作 backspace - 现在正常工作 plus - 向右移动光标减 - 向左移动光标` - 将光标移动到上一行的开头 \ - “软重启”几乎跳转到加载程序的开头

4

2 回答 2

2

问题在这里:

[org 0x7C00]
...
start:
jmp loader                           ;jump to the actual start of bootloader
...
loader:
call cls                ;clear the screen
mov si, head1           ;setup page headers
call printf 
...
printf:
.loop                   ;our function that loops
mov al, [si]            ;load byte

您期望CS=DS=0您的代码何时开始。DS 不保证为 0。因此,当它为非零时,mov al, [si]从段 0 以外的某个段读取字节,可能会有一些垃圾而不是您的问候消息的副本。

此外,一些 BIOS 使用 的地址跳转到您的代码,0:0x7C00而另一些则使用0x7C0:0,这意味着即使 CS 也不能保证具有固定值。

我会做的是:

[org 0x7C00]
[bits 16]
start:
jmp loader                           ;jump to the actual start of bootloader
...
loader:
jmp 0:loader2 ; this far jump guarantees CS=0

loader2:
push cs
pop ds ; this guarantees DS=0

; the rest of the code goes here
于 2012-03-12T06:19:52.347 回答
2

哇塞;现在这是一个沉重的问题!

实际上,这是一个太笼统的编程问题,无法在 Stack Overflow 上真正解决问题,而只是向您扔一些东西,希望对您有所帮助!

如果您达到了一个甚至可以移动光标的地步,尽管是随机的,那么我猜您的引导加载程序还可以;但要清楚 - 它甚至会在 I5 上显示“标题”吗?

我在这里要查看的是用于使光标移动的中断向量/地址线/等。您确定 int10 操作是完全通用的吗?

于 2012-03-12T00:30:16.700 回答