Printing text

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.
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Printing text

Post by deathkillspt »

Ok please bear with me, I have been struggling with this for about 2 days now. All i want to do is just print a string on the screen, I have my bootloader all setup and it boots me into pmode i then execute my kernel and try to print the string with this function:

Code: Select all

unsigned int prints(char *string)
{
	char *memptr = (char *) 0xb8000;

	while(*string!='0')
	{
		*memptr = *string;
		memptr++;
		string++;
		*memptr = 0x17;
		memptr++;	
	};

	return(0);
};
and i call it like this:

Code: Select all

prints("Hello");
I originally got this from a tutorial and it didnt work so i kept changing things and trying to figure it out but, i'm stuck now i have tried everything i can think of.

Please help me out with this, Thanks in advance, Deathkillspt.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Post by Craze Frog »

It would be helpful if you told us what happened when you boot your kernel.
If you're compiling with gcc and then linking as binary the string will probably end up before the code, which isn't a good idea. To fix this you use a linker script.
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

oh yea i forgot that part, it's very strange when i boot it up it does in fact print but it prints an "S" and thats it. i have a clearscreen function and that works fine. so i don't know why this isn't working. and i am using a linker script and also using the gcc option -fwritable-strings, here is my linker script if it is of any help:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
  .text  0x100000 : {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .bss  :
  { 					
    *(.bss)
  }
}
as i have been saying i'm farely new to this whole scene so i might have done some things wrong but i am trying! :D
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Shouldn't your while loop be:

Code: Select all

while(*string!='\0'){...}
Thats what I find on a skim of your code...

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

i already tried that, it doesn't make a difference, but thanks anyway.
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Reply

Post by matias_beretta »

'0' is a char (48)

you should put 0, that means the end of a string...

good luck

mati
Matías Beretta
OrOS
Member
Member
Posts: 143
Joined: Sat Sep 08, 2007 11:26 pm
Location: Canada

Post by OrOS »

C does it automaticly. prints("Hello"); IS rints("Hello" + '\0');
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

well i tried all of the above but still nothing has worked.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Add these to your linker script somewhere.

Code: Select all

*(.const*)
*(.ro*)
I think one of those is the section for strings.
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

frank wrote:Add these to your linker script somewhere.

Code: Select all

*(.const*)
*(.ro*)
I think one of those is the section for strings.
nope that didn't work either :cry:
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Try doing objdump -h on your object file and looking for any section names you don't have in your linker script. Excluding .comment of course.
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

Code: Select all

kernel.o:     file format coff-go32

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         000000b0  00000000  00000000  000000b4  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, CODE
  1 .data         00000010  000000b0  000000b0  00000164  2**4
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  000000c0  000000c0  00000000  2**2
                  ALLOC
  3 .comment      00000010  000000c0  000000c0  00000174  2**2
                  CONTENTS, DEBUGGING
nope it looks like it's all there.

hold on you said do a dump of your object file, but this function is in a separate video header file, could that be the problem? do i have to do some special thing if i'm calling this from a header file?
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Do you have bochs? Maybe you could try stepping though the function in the debugger mode and checking to see if everything looks ok. Also maybe try this:

Code: Select all

char string[] = {'h', 'e', 'l', 'l', 'o', 0};
prints( string );
hold on you said do a dump of your object file, but this function is in a separate video header file, could that be the problem? do i have to do some special thing if i'm calling this from a header file?
EDIT:
I was saying do a dump of the object file that had the call to prints in it.
deathkillspt
Posts: 12
Joined: Sun Sep 23, 2007 2:22 pm
Location: Blairstown,NJ,USA

Post by deathkillspt »

frank wrote:Do you have bochs? Maybe you could try stepping though the function in the debugger mode and checking to see if everything looks ok. Also maybe try this:

Code: Select all

char string[] = {'h', 'e', 'l', 'l', 'o', 0};
prints( string );
I did have bochs but i could never get it to work right it would always say >>PANIC<< and then something about the rom and bios. As of now i am testing on an old laptop, but i guess i could give bochs another shot, and i will try that.

EDIT: yes that is a dump of the object file that had the call to prints in it


EDIT 2:well this does work:

Code: Select all

char string[] = {'h', 'e', 'l', 'l', 'o', 0};
prints( string );
but why does that work and the other doesn't?
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Well the answer is that there is nothing wrong with your prints function. Somehow the linker messes up the address of the string "Hello" but it works just fine with the address of string. This normally means that you are missing the section for strings in your linker script but it doesn't seem to be the problem this time. Okay one last thing to try, add .rdata to your linker script.
Post Reply