基本的NASM引导程序 [英] Basic NASM bootstrap
问题描述
我最近一直在研究操作系统,引导过程和NASM.在旅途中,我遇到了一段有用的自举代码,部分理解并通过虚拟软盘进行了测试.我的基本问题是,我不了解其中的哪些行.我已经评论了我认为这些字眼所做的事情,任何更正或确认将不胜感激.
I've recently been researching Operating Systems, the boot process, and NASM. On my journeys I ran into a piece of useful bootstrapping code which I partially understand and have tested via a virtual floppy disk. My basic question is to what some of these lines I don't understand do. I've commented what I think the lines do, and any corrections or confirmations would be much appreciated.
; This is NASM
BITS 16 ; 16 bits!
start: ; Entry point
mov ax, 07C0h ; Move the starting address (after this bootloader) into 'ax'
add ax, 288 ; Leave 288 bytes before the stack beginning for some reason
mov ss, ax ; Show 'stack segment' where our stack starts
mov sp, 4096 ; Tell 'stack pointer' that our stack is 4K in size
mov ax, 07C0h ; Use 'ax' as temporary variable for setting 'ds'
mov ds, ax ; Set data segment to where we're loaded
mov si, text_string ; Put string position into SI (the reg used for this!)
call print_string ; Call our string-printing routine
jmp $ ; Jump here - infinite loop!
text_string db 'This is my cool new OS!', 0 ; Our null terminated string
; For some reason declared after use
print_string: ; Routine: output string in SI to screen
mov ah, 0Eh ; I don't know what this does..
; Continue on to 'repeat'
.repeat:
lodsb ; Get character from DS:SI into AL
cmp al, 0 ; If end of text_string
je .done ; We're done here
int 10h ; Otherwise, print the character (What 10h means)
jmp .repeat ; And repeat
.done:
ret
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC 'magic word' boot signature
谢谢
乔
推荐答案
您的评论在很大程度上是正确的.
Your comments are largely correct.
mov ah,0Eh
这为BIOS中断调用设置了一个参数:
This sets a parameter to the BIOS interrupt call:
int 10h
有关更多详细信息,请参见此处,但从本质上讲,对10h的调用需要在al
中的操作数据.
See here for more details, but essentially the call to 10h expects an operation in ah
and data for the operation in al
.
段寄存器不能直接加载,只能从寄存器加载,因此将ax
用作临时变量".
The segment registers cannot be loaded directly and can only load from a register, thus the use of ax
as a 'temporary variable.'
添加到基本堆栈指针的288个字节实际上根本不是字节.加载到段寄存器中的地址实际上是指向16字节块的指针,因此要将数字转换为其实际地址,将其向左移4位.这意味着地址07C0h实际上是指7C00h,这是您的引导加载程序代码所在的位置. 288是120h(十六进制),因此堆栈的实际位置实际上是7C00h + 1200h = 8E00h.
The 288 bytes added to the base stack pointer are actually not bytes at all. Addresses loaded into the segment registers are actually pointers to 16-byte blocks, so to convert the number to its actual address, shift it left by 4-bits. That means that the address 07C0h is actually referring to 7C00h, which is where your bootloader code is placed. 288 is 120h in hex, and so the actual location of the stack is really 7C00h + 1200h = 8E00h.
此外,您还可以使用"show"和"tell"之类的词,但最好考虑通过设置ss
和sp
来定义堆栈,而不是报告其位置.希望有道理.
Also, you use words like "show" and "tell" which are fine, but it's better to think of defining the stack by setting ss
and sp
as opposed to reporting where it is at... I hope that makes sense.
这篇关于基本的NASM引导程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!