Printing to screen

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
ManOfSteel

Printing to screen

Post by ManOfSteel »

Hello, I'm trying to print a string direcly to VGA memory.
I'm working under 32bit protected mode and I set up, in the boot loader, two GDT entries (code and data segments).

Is there anybody who can explain to me why this code is not working:

Code: Select all

    mov esi,offset message
    mov edi,0B8000h
    mov ecx,12
    rep movsb

    message db 'test message'
This should at least print a 't', right?

Code: Select all

    mov dl,[message]
    mov dh,04h
    mov word ptr [ds:0b8000h],dx

    message db 'test message'

The following code works perfectly, so I'm confused.

Code: Select all

    mov dl,'A'
    mov dh,04h
    mov dword ptr [ds:0b8000h],edx
I've been trying a lot of printing codes (some I did myself, some I copied from other OSes) and nothing worked. Please help me, I can't do anything without this.
FlashBurn

Re:Printing to screen

Post by FlashBurn »

First you needn?t to write:

Code: Select all

mov word ptr [ds:0b8000h],dx
You only need to write:

Code: Select all

mov word ptr [0b8000h],dx
Try this code(it?s the same as yours, but give it a try)

Code: Select all

mov word ptr [0b8000h],0x0741h
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Printing to screen

Post by Candy »

Code: Select all

    mov esi,offset message
    mov edi,0B8000h
    mov ecx,12
    rep movsb

    message db 'test message'
you copy from 8-bit chars to 16-bit 'chars', so use lodsb; stosw

You need an attribute to go with it. Fortunately, it lines up perfectly with ah in stosw (as compared to stosb) so you can just use lodsb; stosw; loopnz xxx
This should at least print a 't', right?

Code: Select all

    mov dl,[message]
    mov dh,04h
    mov word ptr [ds:0b8000h],dx

    message db 'test message'
In theory, yes. In practice, did you set the correct org?

As for the other references, www.atlantisos.com/pub/pd/bootloader.zip (www.atlantisos.com/pd.html) contains a printing routine that uses lodsb; stosw; cmp al, 0; jnz xxx for printing. It also uses color, so check it. Should help you along :). It's 100% free too (no GPLs attached) so enjoy.
ASHLEY4

Re:Printing to screen

Post by ASHLEY4 »

Hi
This code works in my pc (es is set to linear).

Code: Select all

    lea esi,[message]
    mov edi,0B8000h
    mov ecx,24
    cld
    rep movsb

    message: db 't e s t   m e s s a g e '
ASHLEY4.
ManOfSteel

Re:Printing to screen

Post by ManOfSteel »

You only need to write:

Code: Select all

mov word ptr [0b8000h],dx
TASM needs "ds:" to compile or else it tells me it's an illegal immediate.
did you set the correct org?
I set org to zero.
As for the other references, www.atlantisos.com/pub/pd/bootloader.zip (www.atlantisos.com/pd.html) contains a printing routine that uses lodsb; stosw; cmp al, 0; jnz xxx for printing. It also uses color, so check it. Should help you along . It's 100% free too (no GPLs attached) so enjoy.
I don't have free time right now, so I'll check it later.
This code works in my pc (es is set to linear).

Code: Select all

    lea esi,[message]
    mov edi,0B8000h
    mov ecx,24
    cld
    rep movsb

    message: db 't e s t  m e s s a g e '
It does the same thing that the other ones: print weired white and yellow characters (spaces, dollar signs, ...).
And pardon my ignorance, but what do you mean by setting es to linear?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Printing to screen

Post by Candy »

Yellow and white? The background you mean?

If so, if you happen to be off by one, the lowercase characters map to yellow and white (gray) backgrounds, the uppercase characters map to red and purple backgrounds. Try swapping the halves around or something.
ASHLEY4

Re:Printing to screen

Post by ASHLEY4 »

Hi,
I have checked the code and you must make sure that there is 3 spaces in the middle ( one space from each letter and three in the middle) .
As for linear , if the es is not set to linear (eg: = 0) address, then this will be added to 0b8000h, this is just so it prints in the top corner and not in the middle of the screen.

So Candy is right, you are off by one space.

PS. The spaces repercent the color .

ASHLEY4.
ManOfSteel

Re:Printing to screen

Post by ManOfSteel »

Yellow and white? The background you mean?
Yes, the background. But all it's printing is: several spaces, a dollar sign and a Spanish N (with a ~ over it). Even when I point it to another string, it still prints the same weired message. There's a problem with the routine itself, unfortunately I don't know what.
As for linear , if the es is not set to linear (eg: = 0) address, then this will be added to 0b8000h, this is just so it prints in the top corner and not in the middle of the screen.
The message position is not the problem.
Es is already set (in the boot loader) to the data segment (10h) along with ds, fs, ss and gs.
I can't set it to 0 or else it will point to the null segment and cause a triple-fault.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Printing to screen

Post by Pype.Clicker »

it looks like your code is not loaded at the address the assembler expected it to be loaded ... This makes no problem for accessing the video memory -- which is at a memory-independent location, but it will load garbage instead of your preciousss data when trying to read bytes from something in the data section.

If i were you i would put the following magic-word testing at the start of my kernel

Code: Select all

_start:
     mov eax,[magic]
     cmp eax,'OKAY'
     jne .failure
;; if we reach this point, the loading was fine.
    mov [0xb8000],' D ;'
     do_stuff
     cli
     hlt
.failure:
    mov [0xb8000],' ( :'
    cli     
    hlt
magic dd 'OKAY'
ASHLEY4

Re:Printing to screen

Post by ASHLEY4 »

This code assembled with nasm works fine on my pc,it comely used in start up os.

Code: Select all

    lea esi,[message]
    mov edi,0B8000h
    mov ecx,24
    cld
    rep movsb
                     
                        
    message: db 't e s t   m e s s a g e '
It sounds like you have a space at the start,
letter first then space for color, Middle has one space for color of last letter,one space for hex for space(20h),and one space for color,so on.

Most people when set up GDT have a nul,code,data,linear descriptors .


ASHLEY4.
ManOfSteel

Re:Printing to screen

Post by ManOfSteel »

If i were you i would put the following magic-word testing at the start of my kernel

Code: Select all

_start:
    mov eax,[magic]
    cmp eax,'OKAY'
    jne .failure
;; if we reach this point, the loading was fine.
    mov [0xb8000],' D ;'
    do_stuff
    cli
    hlt
.failure:
    mov [0xb8000],' ( :'
    cli    
    hlt
magic dd 'OKAY'
Ok, I tried it and it failed, I see the unhappy face.
It sounds like you have a space at the start,
letter first then space for color, Middle has one space for color of last letter,one space for hex for space(20h),and one space for color,so on.
No, it's the same weired message I get with any routine.
Most people when set up GDT have a nul,code,data,linear descriptors.
Ok, I know for the null, code and data descriptors, but what is the linear one?

In my bootloader I have the following code:

Code: Select all

    mov eax,10h
    mov ds,eax
    mov fs,eax
    mov es,eax
    mov ss,eax
    mov gs,eax
    mov esp,9f000h

Code: Select all

GDT      db 0,0,0,0,0,0,0,0
GDTCode  db 0ffh
         db 0ffh
         db 0h
         db 0h
         db 0h
         db 10011011b
         db 11011111b
         db 0h
GDTData  db 0ffh
         db 0ffh
         db 0h
         db 0h
         db 0h
         db 10010011b
         db 11011111b
         db 0h
GDTEnd:
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Printing to screen

Post by Pype.Clicker »

if the unhappy face shows up, there is certainly a problem with your 'ORG' statements. As you have 0-based data & code descriptors, you should have an 'ORG xxx' where 'xxx' is the physical loading address. I.e. if you load your kernel at 0x800:0x0000, you should have "ORG 0x8000" .
ASHLEY4

Re:Printing to screen

Post by ASHLEY4 »

I think clicker,is right .
PS.Like the test clicker ;)
ASHLEY4.
ManOfSteel

Re:Printing to screen

Post by ManOfSteel »

you should have an 'ORG xxx' where 'xxx' is the physical loading address. I.e. if you load your kernel at 0x800:0x0000, you should have "ORG 0x8000" .
I load the kernel at 8h:1000h, is it "org 1080" or "org 1080h" or something else? I tried "org 1080" and it still prints the unhappy face, than I tried "org 1080h" and Bochs (the CPU) triple-faulted.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Printing to screen

Post by Pype.Clicker »

warning! i bet the '8:' of '8:1000h' is a pmode selector, right ? in that case, of course, the adjustment is not the same. the 'segment*16' should then be replaced by the base of your segment (which is in your GDT, somehow).
Post Reply