【发布时间】:2022-01-07 03:29:45
【问题描述】:
我试图从Bootloader 编写引导加载程序。写的代码是
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, text_string ; Put string position into SI
call print_string ; Call our string-printing routine
jmp $ ; Jump here - infinite loop!
text_string db 'This is my cool new OS!', 0
print_string: ; Routine: output string in SI to screen
mov ah, 0Eh ; int 10h 'print char' function
.repeat:
lodsb ; Get character from string
cmp al, 0
je .done ; If char is zero, end of string
int 10h ; Otherwise, print it
jmp .repeat
.done:
ret
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
我不明白我们为什么要写 jmp$。通过编写 jmp$ 它进入一个无限循环。所以,进入无限循环后,最后两行
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
永远不会被执行。
此外,我们为什么要在 ax 中添加 288?
【问题讨论】:
-
好吧,它在打印后没有任何事情可做,所以无限循环是它可以做的最好的事情,以避免执行您的文本字符串及其之后的任何内容。
-
将 288 添加到 0x7c0 并将其存储在堆栈段中只是在堆栈和引导加载程序之间放置空间。中间的内存区域可以用作磁盘缓冲区等。
-
您可能还会遇到
jmp $的另一个变体是序列cliendloop: hltjmp endloop。
标签: assembly nasm bootloader bios x86-16