Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - Printing a string without OS

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...