为什么引导加载程序的编译提到错误TIMES值-9为负?

问题描述

感谢补丁,我从另一个由github证实的​​帐户中获得了引导加载程序代码。谢谢补丁,你指导我的MSDOS引导加载程序代码 但是当我编译该引导程序代码时,它显示在给定的错误下方,我该如何调整我的代码,为什么会出现此错误

TIMES value -9 is negative

完整的引导程序代码如下:

%include "bios/config.asm"

org 0x7c00
base:
jmp start
nop

db  "PETYADOS"  ; OEM
dw  0x0200      ; bytes per sector
clstsz: db  0x01        ; sectors per cluster
rsvds:  dw  0x01        ; reserved sectors
nfats:  db  0x02        ; number of FATs
ndire:  dw  0x00e0      ; number of dir entries
dw  0x0b40      ; number of sectors
db  0xf0        ; media descriptor
fatsz:  dw  0x0009      ; sectors per FAT
nsect:  dw  0x0012      ; sectors per head
nhead:  dw  0x0002      ; heads per cylinder
dd  0x000000000 ; hidden sectors
dd  0x000000000 ; large total logical sectors
db  0x00        ; physical drive number
db  0x00        ; flags
db  0x00        ; extended boot signature
dd  0x12345678  ; volume serial number
db  "DOS        "   ; volume label
times   8 db 0      ; filesystem type

start:  mov bx,[si + 8]
cli ; Don't rely on the interrupt shadow (some cpus are buggy)
xor ax,ax
mov ss,ax
mov sp,STKTOP
xor ax,ax
mov ds,ax
mov es,ax
cld
sti

mov [drive],dl
test    dl,0x80
jz  .floppy
mov [poff],bx
.floppy:

; load IO.SYS and MSDOS.SYS

mov word [cseg],0x70
mov word [cname],ionam
call    loadfile

mov word [cseg],0x280
mov word [cname],dosnam
call    loadfile

; transfer control to IO.SYS

mov dl,[drive]
mov ax,0x70
mov ds,ax
jmp 0x70:0

loadfile:
mov ax,[fatsz]
mov bl,[nfats]
xor bh,bh
mul bx
add ax,[rsvds]
mov bx,[ndire]
add bx,15
mov cl,4
shr bx,cl
mov [resid],bx
push    ax
call    seek
.nextdt:push    word [cseg] ; XXX
mov word [cseg],DIRBUF >> 4
call    read
pop word [cseg] ; XXX

; find the directory entry

mov di,DIRBUF
.next:  mov si,[cname]
mov cx,11
rep cmpsb
je  .found
and di,0xffe0
add di,32
cmp di,DIRBUF + 512
jne .next
dec word [resid]
cmp word [resid],0
jne .nextdt
jmp fail

; calculate the first sector CHS address of the file

.found: mov ax,[di + 0x1a - 11] ; first cluster
sub ax,2
mov bl,[clstsz]
xor bh,bh
mul bx
pop bx
add ax,bx
mov bx,cl
add ax,bx
call    seek

; calculate the number of sectors to load

mov cx,[di + 0x1c - 11] ; file size
add cx,511
mov cl,ch
shr cl,1
xor ch,ch
mov word [resid],cx

; load the file

.load:  call    read
add word [cseg],0x20
dec word [resid]
cmp word [resid],0
jne .load

seek:   add ax,[poff]
xor dx,dx
mov bx,word [nsect]
div bx
inc dx
mov byte [csect],dl
xor dx,word [nhead]
div bx
mov byte [chead],dl
mov [ccyl],ax
ret

read:   push    es
mov ax,word [cseg]
mov es,ax
mov ax,0x0201
mov ch,byte [ccyl]
mov cl,byte [ccyl + 1]
ror cl,1
ror cl,1
or  cl,byte [csect]
mov dh,byte [chead]
mov dl,byte [drive]
xor bx,bx
int 0x13
jc  fail
pop es
inc byte [csect]
mov cl,[nsect]
cmp [csect],cl
jbe .fini
mov byte [csect],1
inc byte [chead]
mov cl,[nhead]
cmp [chead],cl
jb  .fini
mov byte [chead],0
inc byte [ccyl]
.fini:  ret

fail:   mov bx,0x0007
mov ah,0x0e
mov si,.msg
.loop:  lodsb
test    al,al
jz  .halt
int 0x10
jmp .loop
.halt:  xor ah,ah
int 0x16
int 0x19
.msg    db  13,10,"Not a system disk or d",13,0

dosnam  db  "MSDOS   SYS"
ionam   db  "IO      SYS"

ccyl    dw  0
chead   db  0
csect   db  0
cseg    dw  0xb800
cname   dw  0
drive   db  0
resid   dw  0
poff    dw  0

times   0x01fe - ($ - $$) db 0
dw  0xaa55

再次提及错误

TIMES value -9 is negative

解决方法

您的九个字节太大,无法容纳在引导扇区中。

需要从该asm文件中压缩9个字节。

这是一个开始:

start:  mov bx,[si + 8]
cli ; Don't rely on the interrupt shadow (some CPUs are buggy)
xor ax,ax
mov ss,ax
mov sp,STKTOP
xor ax,ax
mov ds,ax
mov es,ax
cld
sti

可以优化两个字节(补丁出错了吗?)

start:
cli ; Don't rely on the interrupt shadow (some CPUs are buggy)
mov bx,[si + 8] ;Save before clobbering segs
xor ax,STKTOP
cld
sti

实际上,我们可以做得更好。该nop在开始时就有一个空闲字节。让我们自助吧。

base:
cli
jmp short start

现在,开始后我们不需要cli

start:
mov bx,[si + 8] ;Save before clobbering segs

drivepoff csegcname在读取之前已全部写入,因此可以将它们移到安全位置,例如过去的前8个字节start。有8个字节。都适合。

最后一件事:jmp start请确保这是一个短跳转(两个字节长),否则整个事情将无法正常工作。