printf doesn't work

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.
Post Reply
Unexpected

printf doesn't work

Post by Unexpected »

Why this function doesn't work??
A loading kernel, but text doesn't printing...
Why?


unsigned int k_printf(char *message, unsigned int color, unsigned int line)
{
char *vidmem = ( char * ) 0xb8000;
unsigned int i = 0;

i = ( line * scrwidth * 2 );

while ( *message != 0 )
{
if ( *message == 0x2F )
{
*message++;
if ( *message == 0x6e )
{
line++;
i = ( line * scrwidth * 2 );
*message++;
if ( *message == 0 )
{
return( 1 );
};
};
*message--;
};
vidmem[ i ] = *message;
*message++;
i++;
vidmem[ i ]=color;
i++;
};
return(1);
}
Slasher

Re:printf doesn't work

Post by Slasher »

hi,
the problem could be in the way you linked your kernel and the selector base address in your data segments (es,ds)
if they base address in these registers are not zero based, then you have to subtract the base address from pointers to get to the right address.
eg
base address = 0x00000000 video pointer=0x000b8000 - 0x00000000

base address = 0x00004000 video pointer=0x000b8000 - 0x00004000

also in your linker script you need to specify the start address of your code in its full absolute adress form
ie
if you want to load your kernel at the 1mb mark you have to write (using gcc and ld)
eg
.text : .= 0x00100000 {
......
.......
.......
}

if you want to load your kernel at the 4000k mark you have to write (using gcc and ld)
eg
.text : .= 0x00004000 {
......
.......
.......
}
it would be a good idea for you to post your linker script
Unexpected

Re:printf doesn't work

Post by Unexpected »

There is script:
ld -Ttext=0x1000 -o kernel kernel.o -e 0x0
I loading kernel to adress 0x1000:0x0000
So what I need to do?

Thnx very much!
Slasher

Re:printf doesn't work

Post by Slasher »

that's not a linker script, but command line settings
You have set the address for the code section but there isn't one for the data,bss etc sections. its best to use linker script.
read the gcc/ld info files and you will see an example linker script.
Unexpected

Re:printf doesn't work

Post by Unexpected »

Is this good?

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
.text 0x00100000 : {
*(.text)
}
.data : {
*(.data)
}
.bss :
{                
*(.bss)
}
}


Which script is better?
What is the video pointer address?
Unexpected

Re:printf doesn't work

Post by Unexpected »

My printf still doesn't work :(
What can I do?
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:printf doesn't work

Post by Pype.Clicker »

you're using *message++ to move forward in the string ? do you know what *message++ actually do is incrementing the character under *message (if i remember well from C operator precedence) ?

i guess what you want to do is message++, which increments the pointer !
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:printf doesn't work

Post by Pype.Clicker »

maybe you could also give us feedback on what actually displays on screen ...
Ozguxxx

Re:printf doesn't work

Post by Ozguxxx »

I think you are not remembering right Pype. Postfix operators (++, --) have the highest precedence so what *message++ will do is to return you value at address of unincremented pointer and then increment the pointer. Since you do not need a return value, only message++ will work but *message++ still works. (I think) If I am wrong I am sorry...
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:printf doesn't work

Post by Pype.Clicker »

the video pointer address will depend on your code descriptor's base address. it should be

(void*)(0xb8000 - GDT[CODESEL].base)

(provided that base is baselo+65536*baseme+65536*256*basehi, of course :)
Post Reply