Printing a string without OS

2019-01-20 12:39发布

问题:

I have a simple program in x86 assembly language. It should print a string directly to the video memory without OS.

[bits 16]
[org 0x7c00]
mov ax, 0x3
int 0x10
sdl
mov ax, 0xb800
mov es,ax
mov si, msg
xor di, di
repnz movsw
jmp $
msg db 'Hello'
times 510 - ($ - $$) db 0
dw 0xaa55

But it doesn't work. Can you help me?

回答1:

There are some issues:

  1. There is no such instruction as sdl.

  2. To copy data, you should use rep movsw, not repnz movsw.

  3. You need to set cx before rep movsw.

  4. You need to define the colors of each character too, in every other byte of video memory, either in the data to be copied with rep movsw, or inside copy loop. The code below illustrates both options:

Edit: added code.

[bits 16]
[org 0x7c00]

mov ax,3
int 10h

push word 0xb800
pop  es

push cs    ; just in case, for bootloader code,
pop  ds    ; needed for movsb 

xor di,di
mov si,msg
mov cx,msg_length_in_bytes

one_color_copy_to_vram_loop:
    movsb
    mov al,0x0f
    stosb
    loop one_color_copy_to_vram_loop

mov si,multicolor_msg
mov cx,multicolor_msg_length
rep movsw

jmp $

msg db 'Hello'
msg_length_in_bytes equ $-msg

multicolor_msg db ' ',0
               db 'H',1
               db 'e',2
               db 'l',3
               db 'l',4
               db 'o',5
               db ' ',0 
               db 'w',6
               db 'i',7
               db 't',8
               db 'h',9
               db ' ',0
               db 'c',10
               db 'o',11
               db 'l',12
               db 'o',13
               db 'r',14
               db '!',15
multicolor_msg_length equ ($-multicolor_msg)/2


回答2:

With the repnz prefix you must first set the cx register to the character count, and as nrz points out you shouldn't use that one as it stops when zero is encountered.