汇编语言自举程序代码问题

时间:2019-07-09 06:54:22

标签: assembly x86-16

我试图了解Bootloader的内容,但是在给定的代码部分下面遇到了一些麻烦,请帮忙。我在这里提到评论,这将对您有所帮助,为什么我们使用07C0h,是固定位置还是任意位置,所以第二行中的544是什么。

Bootloader_start:    
mov ax, 07C0h        ;set up 4k of stack space above buffer
add ax, 544 ;8k buffer = 512 paragraphs + 32 paragraphs 
                      ;(loader) 

cli                  ;disable interrupts while changing stack
mov ss, ax 
mov sp, 4096
sti                  ;restore interrupts

mov ax, 07C0h ;set data segment to where we are loaded
mov ds, ax

cmp dl, 0
je no_change
mov [bootdev], dl    ;save boot device number   
mov ah, 8                 ;get drive parameters
int 13h
jc fatal_disk_error
and cx, 3fh              ;maximum sector number 
mov [SectorsPerTrack], cx  ;Sector numbers start at 1
movzx dx, dh        ;maximum head number
add dx, 1.             ;head number starts at 0 - add 1 for total
mov [Sides], dx

......继续

1 个答案:

答案 0 :(得分:2)

Bootloader_start:    
mov ax, 07C0h   ;set up 4k of stack space above buffer
add ax, 544     ;8k buffer = 512 paragraphs + 32 paragraphs (loader) 

cli             ;disable interrupts while changing stack
mov ss, ax 
mov sp, 4096
sti             ;restore interrupts

mov ax, 07C0h   ;set data segment to where we are loaded
mov ds, ax

首先,进行明显的优化,避免了Peter关于不使用汇编时间添加项的正确批评:

Bootloader_start:    
mov ax, 07C0h  ;set up 4k of stack space above buffer
MOV DS, AX
add ax, 544    ;8k buffer = 512 paragraphs + 32 paragraphs (loader) 
cli            ;disable interrupts while changing stack
mov ss, ax 
mov sp, 4096
sti            ;restore interrupts

那为什么要选择544?

作者希望在引导加载程序正上方有一个8192字节的缓冲区,该堆栈位于内存中线性地址7C00h处。

算术知道线性地址7C00h是07C0h段:

              Paragraph
              ---------
                07C0h    Bootloader 512 bytes ==  32 paragraphs (20h)
07C0h + 0020h = 07E0h    Buffer    8192 bytes == 512 paragraphs (200h)
07E0h + 0200h = 09E0h    Stack     4096 bytes
                                                 ---
                                                 544

堆栈底部在09E0h07C0h + (32 + 512)的{​​{1}}处。
然后将堆栈指针07C0h + 544设置为偏移量4096,所以我们有完整的SP