"times 510-($-$$) db 0" 无效

"times 510-($-$$) db 0" does not work

我正在学习引导扇区。我从 NASM 网站下载了 nasm-installer-x64.exe。我的操作系统是win7-64位。当我 运行 以下代码无法正常工作时

mov ah, 0x0e;

mov al, the_secret;
int 0x10;

mov al, [the_secret];
int 0x10;

mov bx, [the_secret];
add bx, 0x7c00;
mov al, [bx];
int 0x10;

mov al, [0x7c1e];
int 0x10;

jmp $;

the_secret:;
    db 'X';

times 510-($-$$) db 0;
dw 0xaa55;

我认为 times 510-($-$$) db 0 没有任何问题。在我看来,您正在尝试找到访问变量 the_secret 的正确方法,然后将其显示到屏幕上。我将提供一种基于这种尝试的最有希望的机制:

mov al, [the_secret];
int 0x10;

如果 DS 设置正确,请使用 org 0x7c00 设置原点并确保 BH 设置为您要写入的页码(您想要 0),那么下面的代码应该可以工作:

[bits 16]          ; 16-Bit code
[org 0x7c00]       ; Set the origin point to 0x7c00

start:
    xor ax,ax      ; We want a segment of 0 for DS for this question
    mov ds,ax      ;     Set AX to appropriate segment value for your situation
    mov es,ax      ; In this case we'll default to ES=DS
    mov bx,0x8000  ; Stack segment can be any usable memory

    mov ss,bx      ; This places it with the top of the stack @ 0x80000.
    mov sp,ax      ; Set SP=0 so the bottom of stack will be @ 0x8FFFF

    cld            ; Set the direction flag to be positive direction

    mov ah, 0x0e
    mov al, [the_secret]  ; al = character from memory DS:[the_secret]
    xor bh, bh            ; bh = 0 = video page number
    int 0x10;

    jmp $

the_secret:;
    db 'X';

times 510-($-$$) db 0
dw 0xAA55

启动代码将 DS 设置为零,因为我们将原点设置为 0x7c00。引导加载程序加载到 0x0000:0x7c00(物理地址 0x07c00)。这可确保正确访问变量 the_secretmov al, [the_secret] 相当于说 mov al, ds:[the_secret]。如果DS段寄存器设置不正确,原点设置不正确,内存访问将不会从正确的位置读取。

INT 0x10/AH=0x0E 需要设置页码。第一个视频显示页为0,BH应相应设置。

有关其他设置说明的更多信息,请参阅我的包含 .

的 Whosebug 答案

如果正确写入磁盘映像,我提供的代码应显示 X 到控制台。


到assemble此代码并生成磁盘映像(在我的示例中是 720k 软盘):

nasm -f bin bootload.asm -o bootload.bin
dd if=/dev/zero of=disk.img bs=1024 count=720
dd if=bootload.bin of=disk.img bs=512 count=1 conv=notrunc

第一个命令将 bootload.asm 汇编成一个名为 bootload.bin 的平面二进制文件。第二条命令生成大小为 1024 * 720(720kb 软盘)的零填充磁盘映像 (disk.img),最后一条命令将 512 字节的数据从 bootload.bin 复制到磁盘映像的第一个扇区。 conv=notrunc 告诉 DD 不要在写入后截断文件。如果您将其关闭,disk.img 将在引导扇区写入后长 512 个字节。

您可以尝试只替换代码段的十六进制。 应该是新十六进制破坏了MBR格式