当我修改代码时,磁盘映像无法启动 [英] When I modify code the disk image becomes unbootable
问题描述
我正在用汇编语言开发操作系统.更改代码时,输出文件不会被视为可引导.
I'm developing an operating system in assembly language. When making changses o the code the output file isn't seen as bootable.
这是我的代码:
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
call cls
MOV AH, 06h ; Scroll up function
XOR AL, AL ; Clear entire screen
XOR CX, CX ; Upper left corner CH=row, CL=column
MOV DX, 184FH ; lower right corner DH=row, DL=column
MOV BH, 1Eh ; YellowOnBlue
INT 10H
mov si, text_string ; Put string position into SI
call print_string ; Call our string-printing routine
push bx ;push registers
push cx
push dx
mov ah,0h
int 16h
cmp al, '1'
je reboot
cmp al, '2'
je shutdown
cmp al, '3'
je about
cmp al, '4'
je message
cmp al, '5'
je shutdown
jmp $ ; Jump here - infinite loop!
text_string db '|Main Menu| |Smile OS V1.2|',13,10,'1) Reboot',13,10,'2) Shutdown',13,10,'3) About',13,10,'4) Message',13,10,'5) System Halt',0
about_string db '|About|',13,10,'Smile OS is a console based operating system in assembly.',13,10,'Press any key to go back!',0
message_str db '|Message|',10,13,'Hello, World!',13,10,'Press any key to go back!',0
reboot:
mov ax, 0
int 19h
shutdown:
mov ax, 0x1000
mov ax, ss
mov sp, 0xf000
mov ax, 0x5307
mov bx, 0x0001
mov cx, 0x0003
int 0x15
message:
call cls
mov si, message_str ; Put string position into SI
call print_string ; Call our string-printing routine
push bx ;push registers
push cx
push dx
mov ah,0h
int 16h
je start
cls:
pusha
mov ah, 0x00
mov al, 0x03 ; text mode 80x25 16 colours
int 0x10
popa
ret
about:
call cls
mov si, about_string ; Put string position into SI
call print_string ; Call our string-printing routine
push bx ;push registers
push cx
push dx
mov ah,0h
int 16h
je start
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 512-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
当我尝试还原更改时,代码中断了.
When I tries to revert the change the code broke.
- 我正在使用Oracle VM VirtualBox和最新版本的NASM.
- 我试图重新启动计算机几次,没有任何改变.
- 我试图安装旧版本的NASM,什么也没有.
我该怎么做才能修复此引导程序?
What do I have to do to fix this bootloader?
推荐答案
您需要更改:
times 512-($-$$) db 0
收件人:
times 510-($-$$) db 0
您需要填充510字节,以便引导签名0xaa55是512字节引导扇区的最后2个字节.如果不这样做,该映像将不会被视为有效的引导扇区.您先前的问题提到使用命令:
You need to pad 510 bytes so the boot signature 0xaa55 are the last 2 bytes of the 512 byte boot sector. If you don't do this the image will not be seen as a valid boot sector. Your previous question mentions using the command:
nasm -f bin os.asm -o os.iso
您不会使用此命令生成ISO(CD-ROM映像).要在VirtualBox中进行引导,我建议通过以下方式用引导扇区创建1.44MB的软盘映像:
You aren't generating an ISO (CD-ROM image) with this command. To boot in VirtualBox I recommend creating a 1.44MB floppy disk image with your boot sector this way:
将文件汇编到os.bin
.
nasm -f bin os.asm -o os.bin
在此步骤之后,检查文件os.bin
是否完全是 512字节 1 .如果不是,您在引导扇区中放置了太多的代码和数据,则引导签名将放置在错误的位置,VirtualBox将拒绝将其标识为可引导设备.
After this step check that the file os.bin
is exactly 512 bytes1. If it isn't you have placed too much code and data in your boot sector, the boot signature will be in the wrong place and VirtualBox will refuse to identify it as a bootable device.
创建一个名为os.img
的空白1.44MB软盘映像:
Create a blank 1.44MB floppy disk image called os.img
:
dd if=/dev/zero of=os.img bs=1024 count=1440
将os.bin
复制到os.img
的开头,而不会截断磁盘映像:
Copy the os.bin
to the beginning of os.img
without truncating the disk image:
dd if=os.bin of=os.img conv=notrunc
在VirtualBox中将其设置为从FLOPPY(不是CD-ROM)启动,然后选择os.img
文件作为启动映像. VirtualBox需要适当大小的软盘映像.
In VirtualBox set it up to boot from FLOPPY (not CD-ROM) and select the os.img
file as the boot image. VirtualBox needs a properly sized floppy disk image.
在运行时在此问题中显示的代码在我的VirtualBox中显示如下:
The code you show in this question when run appears like this in my VirtualBox:
如果您替换:
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
使用:
section bootsig start=510
dw 0xAA55 ; The standard PC boot signature
然后,您会收到一条错误消息,提示您引导加载程序太大:
Then you will be informed with an error that your bootloader is too big:
nasm:致命:.text和bootsig部分重叠!
nasm: fatal: sections .text and bootsig overlap!
这意味着bootsig
之前的部分中的代码与引导签名部分重叠.如果发生这种情况,那么您将知道该是您自己读取磁盘扇区以将代码扩展到512字节引导扇区之外的时候了.
This means code in the section before bootsig
overlaps with the boot signature section. If this happens then you will know that it is time to read disk sectors yourself to extend your code beyond the 512 byte boot sector.
如果使用ORG 0x7c00
,则start=510
必须为start=0x7c00+510
.
If using ORG 0x7c00
then start=510
would have to be start=0x7c00+510
.
如果您依靠TIMES
告诉您有问题,它将看起来像这样:
If you rely on TIMES
to tell you there is a problem it will look something like:
os.asm:###:错误:TIMES值-###为负
os.asm:###: error: TIMES value -### is negative
1 如果文件os.bin
大于512字节,则需要使用BIOS手动将更多磁盘扇区读入内存.可以使用 INT 13h/AH = 2h 从软盘读取磁盘
1 If the file os.bin
is more than 512 bytes then you will need to use the BIOS to read more disk sectors into memory manually. The disk reads from a floppy can be done with INT 13h/AH=2h.
这篇关于当我修改代码时,磁盘映像无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!