最近我试图弄清楚引导程序如何工作。 我在nasm汇编程序中编写我的加载程序,并用bochs和软盘映像进行测试。加载引导程序的第二阶段并启动它
第1阶段和第2阶段的已编译二进制文件通过复制加入到一个图像中。 这张图片就像我想要的一样。 512Bytes stage1代码(包含magicnumber,它加载得很好)以及第二扇区中的512阶段2代码。
但我认为我的问题是将该部门加载到内存中并跳入内存中。我的代码有问题吗?
Stage1.asm
BITS 16
start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512)/16 bytes per paragraph
mov ss, ax
mov sp, 4096
mov ax, 07C0h ;Set data segment to where we're loaded
mov ds, ax
mov si,s_version
call print_string
; ## Load stage2
mov si,s_loading
call print_string
xor ax,ax
xor bx,bx
xor cx,cx
xor dx,dx
;read 2nd sector
mov ah,02h
mov al,1 ;read 1
mov ch,0 ;on track 0
mov cl,2 ;2nd sector
mov dh,0 ;head 1
mov dl,0 ;from floppy a
mov bx,09C0h;destination segment
mov es,bx
mov bx,0 ;destination offset
int 13h ;<-- Fails right here
mov si,s_sector
call print_string
;print number of read sectors
add ax, 48
mov ah, 0Eh
int 10h
mov al, 21
mov ah, 0Eh
int 10h
;print the sector's magicnumber (debugging purposes)
mov al, [09C0h+511]
int 10h
xor ax,ax
int 16h
mov si,s_jumping
call print_string
call word 09C0h:0000h
; #### print a string from si
print_string:
push ax
push bx
mov ah, 0Eh
.repeat:
lodsb
cmp al, 0
je .exit
int 10h
jmp .repeat
.exit:
pop bx
pop ax
ret
; ****
; #### define strings
s_version db 'VeOS 0.0.0.1',10,13,0
s_loading db 'Loading Stage2...',10,13,0
s_sector db 'Loading sector...',10,13,0
s_jumping db 'Passing control to Stage2.',10,13,0
; ****
;fillup with zeros
times 510-($-$$) db 0
;boot signature
dw 0xAA55
stage2.asm
BITS 16
start:
mov ax, 09C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512)/16 bytes per paragraph
mov ss, ax
mov sp, 4096
mov ax, 09C0h ;Set data segment to where we're loaded
mov ds, ax
mov ah, 0Eh
mov al, 21 ;"!"
int 10h
mov ah, 00h
int 16h
jmp $
times 511-($-$$) db 0
;Magicnumber for debugging
db 0x41
我彻底地用Google搜索,没有发现任何确切描述了如何在部门加载到RAM和跳进去。我的程序甚至没有发现第二部分的Magicnumber。
如果只是对地址进行一些错误估计,那会很好。
更新: 当前源代码,它有一个锁定的行被标记。我把所有4个主要寄存器设置为0,而不是纯粹的偏执狂。
Update2: 再次当前版本。在设置寄存器和发出int 13h之间没有任何东西完成。
正如你所说,我设置了bx = 0和cl = 2。现在bochs(或我的代码)在执行int 13h的时刻锁定。 Bochs对虚拟软盘的状态保持在“阅读”状态,没有任何反应。 但是,感谢您的帮助到目前为止。要看你的代码。 – vikenemesh
而'dh = 0',也只是为了清楚你将'es'设置为'09c0h'后将'bx'设置为零,对吧?因为这种行为听起来像是你刚刚重写了中断表。无论如何,随时更新您的问题与更新的源代码,我会看看。 – user786653
查看上面的更新源代码。 – vikenemesh