当前位置: 动力学知识库 > 问答 > 编程问答 >

x86 assembly OS hello world not working as expected

问题描述:

i made this code, this should be simple OS, that should print "Hello world!".

bits 16

mov bx,msg

call printstr

printstr:

mov al,byte[bx]

mov ah,0Eh

int 10h

inc bx

cmp byte[bx],0

je end

jmp printstr

end:

jmp end

msg: db "Hello world!",0

times 510-($-$$) db 0

dw 0xaa55

I assembled that on NASM.

When i run it on QEMU, it prints one strange character, and 'S'.

I ran QEMU with those arguments

"qemu-system-x86_64 ost.bin" where "ost.bin" is that file.

Does anybody know solution?

网友答案:

There are several problems in your code:

bits 16
 mov bx, msg

msg now contains only offset of your message from start of output binary. You can solve this problem by either adding 0x7c00 to msg or setting program origin (start in memory, ORG 0x7c00).

  call printstr
printstr:
  mov al,byte[bx]

BX is not good register for indexing strung characters, as it contains color (lower byte) and page (higher byte) information.

  mov ah,0Eh
  int 10h

You don't have to set AH to 0x0E everytime you print a character. It can be done on the start of code.

  inc bx
  cmp byte[bx], 0

In this kind of loops, you should first check value, and then process it. What would you do if character on BX+0 was equal to 0? You would miss that fact and that would cause wrong output or even endless loop.

  je end
  jmp printstr

This will surely cause endless loop, as you already are in that procedure.

end:
  jmp end

Prefered syntax is jmp $ (where dollar sign means "here").

msg: db "Hello world!",0
times 510-($-$$) db 0
dw 0xaa55 
网友答案:

You have to insert "org 7c00h" at the beginning of the code.

And also, you have to stop the program after printing, or else it will fall into printstr procedure once again.

Maybe something like:

sleep: hlt 
       jmp sleep
网友答案:

The main problem seems to be that the segment registers (DS and ES) are not initialized.

The "org 7C00h" is not required; the program may also start at address 100h (ideal for DOS .COM files) or 0h. Depending on the start address the segment registers must be initialized to 7C0h (org 0h), 7B0H (org 100h) or 0 (org 7C00h).

The "jmp sleep" instruction at the end is sufficient; a "HLT" instruction is definitely not needed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: