为什么这个启动加载程序代码不起作用?

kim*_*yun 2 x86 assembly bios nasm bootloader

我的期望是它打印一个字符串,但没有打印出来.当我把字符串缩短时,它有时会起作用,当我再次使它们变长时,它有时会起作用.

我不知道为什么这不起作用.

有人能帮帮我吗?谢谢.

我正在使用的汇编代码是:

(Emacs 23,Ubuntu 10.10,nasm,VirtualBox OSE)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org 0x7c00
bits 16
str:
    db "Some say the world will end in fire",10,13
    db "Some say in ice",10,13
    db "From what I've tasted of desire",10,13
    db "I hold with those who favor fire",10,13
    db "But if I had to perish twice,",10,13
    db "I think I know enough of hate",10,13
    db "To say that for destruction ice",10,13
    db "is also great and would suffice."
    db "Robert Frost - Fire and Ice"
    db 0
start:
    xor ax,ax
    mov ds,ax
    mov es,ax
    mov si, str
    xor bx,bx
    mov ah, 0x0e
print:
    lodsb   ;al = current char
    cmp al, 0
    je end
    int 0x10
    jmp print
end:    
    cli
    hlt

    times 510 - ($-$$) db 0
    dw 0xAA55
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 9

因为它开始在指令处执行代码7c00.那不幸的是你有你的字符串.

您应该在该字符串前面加上一条jmp指令,以便它跳转到start.

这通常是短暂的跳跃,EB xx然后是NOP 90.一些BIOS可能会坚持认为这种形式即使对处理器来说并不重要.

换句话说,你会找到类似的东西:

org 0x7c00
bits 16
realstart:
    jmp short start
    nop
str:
    db "Some say the world will end in fire",10,13
    :
    db "Robert Frost - Fire and Ice"
    db 0
start:
    xor  ax,ax
    :
Run Code Online (Sandbox Code Playgroud)

请记住,短跳是有限的,它可以走多远,大约+/- 128字节,所以你的字符串大小必然受到限制.如果您的BIOS不需要EB xx 90格式,您可以定期跳转.

您可以尝试的另一件事是在hlt指令之后移动整个字符串:

org 0x7c00
bits 16
start:
    xor  ax,ax
    :
end:    
    cli
    hlt
str:
    db "Some say the world will end in fire",10,13
    :
    db "Robert Frost - Fire and Ice"
    db 0
Run Code Online (Sandbox Code Playgroud)

但是,这又取决于你的BIOS jmp/nop在开始时不需要组合.

  • 可能一些字符串可以被解释为有效的代码,最终将执行到"真正的"代码. (2认同)