我的引导扇区尝试从另一个扇区加载代码并执行它,但它却锁定了。出了什么问题?

问题描述 投票:0回答:2

为了学习,我考虑制作一个小型操作系统,现在正在启动加载程序。我希望能够使用

int 0x13
从软盘驱动器读取扇区,将它们放入内存,然后跳转到该代码。这是我到目前为止所拥有的:

org 0x7c00
bits 16

main:
    call setup_segments

    mov ah, 2      ; function
    mov al, 1      ; num of sectors
    mov ch, 1      ; cylinder
    mov cl, 2      ; sector
    mov dh, 0      ; head
    mov dl, 0      ; drive
    mov bx, 0x1000 ;
    mov es, bx     ; dest (segment)
    mov bx, 0      ; dest (offset)
    int 0x13       ; BIOS Drive Interrupt

    jmp 0x1000:0   ; jump to loaded code

times 510 - ($-$$) db 0 ; fluff up program to 510 B
dw 0xAA55               ; boot loader signature




LoadTarget: ; Print Message, Get Key Press, Reboot

jmp new_main

Greeting: db "Hello, welcome to the bestest bootloader there ever was!", 0
Prompt:   db "Press any key to reboot...", 0

Println:
    lodsb ; al <-- [ds:si], si++

    or al, al    ; needed for jump ?
    jz PrintNwl  ; if null is found print '\r\n'
    mov ah, 0x0e ; function
    mov bh, 0    ; page number ?
    mov bl, 7    ; text attribute ?
    int 0x10     ; BIOS Interrupt
    jmp Println

PrintNwl: ; print \r\n
    ; print \r
    mov ah, 0x0e ; function
    mov al, 13   ; char (carriage return)
    mov bh, 0    ; page number ?
    mov bl, 7    ; text attribute ?
    int 0x10

    ; print \n
    mov ah, 0x0e ; function
    mov al, 20   ; char (line feed)
    mov bh, 0    ; page number ?
    mov bl, 7    ; text attribute ?
    int 0x10

    ret          ; return

GetKeyPress:
    mov si, Prompt ; load prompt
    call Println   ; print prompt

    xor ah, ah     ; clear ah
    int 0x16       ; BIOS Keyboard Service

    ret            ; return

setup_segments:
    cli ;Clear interrupts
    ;Setup stack segments
    mov ax,cs
    mov ds,ax
    mov es,ax
    mov ss,ax
    sti ;Enable interrupts

    ret

new_main:
    call setup_segments

    mov si, Greeting ; load greeting
    call Println     ; print greeting

    call GetKeyPress ; wait for key press

    jmp 0xffff:0     ; jump to reboot address

times 1024 - ($-$$) db 0 ; fluff up sector

我想将

LoadTarget
之后的扇区加载到地址
0x1000:0
中,然后跳转到它。到目前为止,我只得到一个空白屏幕。我觉得这个 bug 位于
main
和线
times 510 - ($-$$) db 0
之间。也许我只是没有得到正确的寄存器值?请帮忙!谢谢

assembly x86 nasm boot bios
2个回答
5
投票

您应该用完成这项工作的实际说明替换第一个

call setup_segments
。另外,正如 Jester 指出的,在更改 SS 寄存器时始终更新 SP 寄存器。

当前您正在从柱面 1 读取。它应该是柱面 0。

换行代码是10(不是你写的20)。

PrintNwl中的两个BIOS调用都不需要BL寄存器,因为CR和LF都是不可显示的ascii。


3
投票

您使用

ORG 0x7C00
的事实意味着您希望 CS:IP 对或寄存器保存 0000h:7C00h。请记住,BIOS 已将 1024 字节长程序的前 512 字节放置在线性地址 00007C00h 处。

设置其他段寄存器只需将CS复制到DS、ES和SS即可。但非常重要的是,每次更改 SS 时,您还必须更改 SP 以维持一致的 SS:SP 寄存器对。在您的程序中,堆栈的方便位置将位于程序下方,因此我使用 7C00h 设置 SP 寄存器。您不能在子例程中摆弄 SS:SP (就像您所做的那样),因为最后的

RET
不知道返回到哪里!

当您从软盘加载第二个扇区时,它必须来自柱面 0。在 CHS 表示中,柱面和磁头从 0 开始,扇区从 1 开始。
如果您检查 CF 是否操作成功就好了。

无法通过

jmp 0x1000:0
跳转到加载的代码,因为现在所在的代码是使用
ORG 0x7C00
指令编译的程序的一部分。这点你需要补偿!

  1. 补偿
    ORG 0x7C00
    指令。段部分减去07C0h,偏移部分加上7C00h。 =>
    jmp 0840h:7C00h
  2. 补偿您的 LoadTarget 标签位于程序偏移量 512 处。段部分减去0020h,偏移部分加上0200h。 =>
    jmp 0820h:7E00h

这是初始代码的样子

 org 7C00h
 bits 16
main:
 mov ax,cs
 mov ds,ax  <-- Not necessary at this stage in this program
 mov es,ax  <-- (keep them should you display a message on error)
 cli
 mov ss,ax
 mov sp,7C00h
 sti
 mov ax,0201h
 mov cx,0002h
 mov dx,0000h
 mov bx,1000h
 mov es,bx
 mov bx,0000h
 int 13h
 jc  Error
 jmp 0820h:7E00h
Error:
 hlt

第二阶段运行后,您只需设置 DS 和 ES 寄存器。 CS:IP 通过

JMP
设置,SS:SP 继续。

 ...
new_main:
 push cs
 pop  ds
 push cs
 pop  es
 mov  si,Greeting
 call Println
 ...
© www.soinside.com 2019 - 2024. All rights reserved.