Page 1 of 2
Printing to screen
Posted: Sun Mar 07, 2004 2:53 pm
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.
Re:Printing to screen
Posted: Sun Mar 07, 2004 3:50 pm
by FlashBurn
First you needn?t to write:
You only need to write:
Try this code(it?s the same as yours, but give it a try)
Re:Printing to screen
Posted: Sun Mar 07, 2004 4:01 pm
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.
Re:Printing to screen
Posted: Sun Mar 07, 2004 4:48 pm
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.
Re:Printing to screen
Posted: Mon Mar 08, 2004 1:13 am
by ManOfSteel
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.
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?
Re:Printing to screen
Posted: Mon Mar 08, 2004 1:45 am
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.
Re:Printing to screen
Posted: Mon Mar 08, 2004 8:36 am
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.
Re:Printing to screen
Posted: Tue Mar 09, 2004 1:13 pm
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.
Re:Printing to screen
Posted: Tue Mar 09, 2004 1:34 pm
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'
Re:Printing to screen
Posted: Tue Mar 09, 2004 2:00 pm
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.
Re:Printing to screen
Posted: Wed Mar 10, 2004 2:45 am
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:
Re:Printing to screen
Posted: Wed Mar 10, 2004 3:26 am
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" .
Re:Printing to screen
Posted: Wed Mar 10, 2004 9:42 am
by ASHLEY4
I think clicker,is right .
PS.Like the test clicker
ASHLEY4.
Re:Printing to screen
Posted: Thu Mar 11, 2004 12:39 pm
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.
Re:Printing to screen
Posted: Thu Mar 11, 2004 12:58 pm
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).