[英]Develop a Bootloader In Assembly
我已经在 Assembly 中完成了我的操作系统的一部分,但现在我也想为它构建一个自己的引导加载程序,而不是使用 GRUB。 当我在 Assembly 中开发我的测试操作系统时,我记得我是这样启动它的:
org 0x7c00
bits 16
; OS Kernel Here
times 510 - ($-$$) db 0
dw 0xAA55
这个我已经知道了。 现在我想使用它并执行“真正的”操作系统,它将是一个写入软盘第二扇区的 *.bin 文件。 然后我想知道一些事情
您使用int 0x13
加载所需数量的扇区并跳转到放置新代码的位置。 在第二阶段您无需执行任何操作,但您需要确保将DS
设置为对加载代码的任何位置都有效。
来自我的小操作系统档案的示例:
/* BIOS loads the sectors into es:bx */
pushw $STAGE1_WORKSEG
popw %es
movw $STAGE1_OFFSET, %bx
read_stage1:
/* Try to read in a few sectors */
movb $0x2, %cl /* Sector */
movb $0x0, %ch /* Cylinder */
movb $0x0, %dh /* Head */
movb $0x0, %dl /* Drive */
movb $0x2, %ah /* BIOS read function */
/* How many sectors to load */
movb $STAGE1_SIZE, %al
int $0x13
jnc read_stage1_done
/* Reset drive */
xorw %ax, %ax
int $0x13
jmp read_stage1
read_stage1_done:
/* Perform a long jump into stage1 */
ljmp $STAGE1_WORKSEG, $STAGE1_OFFSET
call halt
halt:
/*
* Function: halt
* Synopsis: Sends the processor into a permanent halted status
* Notes:
* The only way out of this is to manually reboot
*/
hlt /* Halt the processor */
jmp halt
这是 GAS 格式,因此您需要颠倒操作数顺序,因为看起来您正在使用times
指令中的 NASM。 变量名称应该是不言自明的。
如果您正在开发一个业余操作系统,那么http://forum.osdev.org/
是一个获得其他人的支持的好地方,做同样的事情。 它比 stackoverflow 更专业,而且很多操作系统的东西都非常深奥。
在 16 位模式下,您可以使用以下代码将内核从磁盘加载到内存中:
disk_load:
push dx
mov ah, 0x02
mov al, dh
mov ch, 0x00
mov dh, 0x00
mov cl, 0x02
int 0x13
jc disk_error
pop dx
cmp dh, al
jne disk_error
ret
disk_error:
mov bx, DISK_ERROR_MSG
call print_string
jmp $
DISK_ERROR_MSG db "Disk read error !", 0
load_kernel:
mov bx, MSG_LOAD_KERNEL
call print_string
mov bx, KERNEL_OFFSET
mov dh, 54
mov dl, [BOOT_DRIVE]
call disk_load
ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.