我正在为自定义内核编写一个简单的引导程序。它有非常简单的逻辑:
这是我的引导程序:
.set DEST, 0x8000
start:
movw $0x3, %ax
int $0x10
movw msg, %bp
movw $0xa, %cx
call print_msg
//copy the end of bootloader to new place
selfCopy:
cld
xor %ax, %ax
movw %ax, %ds
movl move_kernel, %esi
movw %ax, %es
movl DEST, %edi
movl (move_kernel_end-move_kernel), %ecx
rep movsb
//load kernel to 0x9000
load_kernel:
xor %dx, %dx
xor %ah, %ah
int $0x10
jc reboot
xor %ax, %ax
movw %ax, %ds
movb $0x42, %ah
movw DAP, %si
int $0x13
jc reboot
jmp DEST
DAP:
.byte 0x10
.byte 0x0
.word 2048
//offset
.word 9000
.word 0x0
.long 1
move_kernel:
xor %ax, %ax
movw %ax, %ds
movl $0x9000, %esi
movw %ax, %es
movl $0x0, %edi
movl (move_kernel_end-move_kernel), %ecx
rep movsb
lgdt gdtr
jmp $0x0
move_kernel_end:
print_msg:
//print message
movw $0x0007, %bx
movw $0x1301, %ax
int $0x10
ret
msg:
.string "Booting..."
error_msg:
.string "Error..."
gdt:
.quad 0x0
.byte 0x0
.byte 0b11001111
.byte 0b11111010
.byte 0x0
.word 0x0
.word 0xffff
.byte 0x0
.byte 0b11001111
.byte 0b11110010
.byte 0x0
.word 0x0
.word 0xffff
gdtr:
.long gdt
.word $23
reboot:
movw error_msg, %bp
movw $0x8, %cx
call print_msg
jmp .
.fill 510-(. - start), 1, 0
.byte 0x55
.byte 0xaa
所以,它不起作用)如果我将jmp .
放在jmp DEST
之前,它就不会进入无限循环。那么,我的bootloader出了什么问题?
感谢。
P.S。抱歉我的英语不好。
修改后的代码:
.set DEST, 0x8000
start:
movw $0x3, %ax
int $0x10
pushw msg
pushw $0xa
call print_msg
//copy the end of bootloader to new place
selfCopy:
cld
xor %ax, %ax
movw %ax, %ds
movl move_kernel, %esi
movw %ax, %es
movl DEST, %edi
movl (move_kernel_end-move_kernel), %ecx
rep movsb
//load kernel to 0x9000
load_kernel:
xor %dx, %dx
xor %ah, %ah
int $0x13
jc reboot
xor %ax, %ax
movw %ax, %ds
movb $0x42, %ah
movw DAP, %si
int $0x13
jc reboot
jmp DEST
DAP:
.byte 0x10
.byte 0x0
.word 2048
//offset
.word 9000
.word 0x0
.long 1
move_kernel:
cli
xor %ax, %ax
movw %ax, %ds
movl $0x9000, %esi
movw %ax, %es
movl $0x0, %edi
movl (move_kernel_end-move_kernel), %ecx
rep movsb
lgdt gdtr
jmp .
sti
jmp $0x0
move_kernel_end:
print_msg:
//print message
popw %ax
popw %cx
popw %bp
pushl %eax
movb $0x07, %bl
movb $0x13, %ah
movb $0x1, %al
int $0x10
ret
msg:
.string "Booting..."
gdt:
.quad 0x0
.byte 0x0
.byte 0b11001111
.byte 0b11111010
.byte 0x0
.word 0x0
.word 0xffff
.byte 0x0
.byte 0b11001111
.byte 0b11110010
.byte 0x0
.word 0x0
.word 0xffff
gdtr:
.long gdt
.word 23
reboot:
pushw $0xdead
pushw $0x8
call print_msg
.fill 510-(. - start), 1, 0
.byte 0x55
.byte 0xaa