Trying to get a string printed on 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.
Post Reply
bazi
Posts: 7
Joined: Fri Mar 20, 2009 3:15 am
Location: Allgaeu (Germany, BW)

Trying to get a string printed on screen

Post by bazi »

Hi,

i'v done the bare bones tutorial and now i'm trying go write a small lib for i/o. Actually i'm on the functions for printing on screen.

I've done a function for printing a char on the screen, located 1 after the last char (code for this is attached after my post).
Now i'll try to write a function for printing a whole string instead of only on character, but it prints nothin....

Here's the code, which isn't working.

Code: Select all

void print(char str[]);
.....
    print("hi");
.....

void print(char str[]) {
	unsigned int count=0;
	while(str[count] != 0) {
		printc(str[count]);
		count++;
	}		
}
This is the code for only one character, it's working:

Code: Select all

void printc(unsigned char c) {
	unsigned char *videoram = (unsigned char *) 0xB8000;
	unsigned char *video_mode = (unsigned char *) 0x500;
	unsigned char x = video_mode[0];
	unsigned char y = video_mode[1];
	unsigned char color = video_mode[2];
	int pos_char = (x*2)+(y*160);
	
	videoram[pos_char] = c;
	videoram[pos_char+1] = color;

	x++;
	if(x == 80) {
		x = 0;
		y++;
	}
	if(y == 25) {
		y = 0;
	}

	video_mode[0] = x;
	video_mode[1] = y;
	set_cursor(x, y);
}
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Trying to get a string printed on screen

Post by AJ »

Hi,

Are you currently using a linker script and if so, do you explicitly place the .data and .rodata sections? If not, that's the first thing you need to look at.

I must say - the prototype for the print function looks...unusual too. Are you really able to compile that code with no warnings - at least about implicit conversion of a const char* to a char*? I'll take your word for it that the printc function is working.

Cheers,
Adam
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Trying to get a string printed on screen

Post by jal »

bazi wrote:void print(char str[])
Could you please change that to "void print(char* str)" and try again? Iirc, using [] in parameter lists doesn't always work well.


JAL
bazi
Posts: 7
Joined: Fri Mar 20, 2009 3:15 am
Location: Allgaeu (Germany, BW)

Re: Trying to get a string printed on screen

Post by bazi »

I'm compiling with gcc with -Wall, no warnings.

A Part of my linker script:

Code: Select all

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }
I'll take your word for it that the printc function is working.
Yes, it's working ;)

I wrote the function of printing on char within print(), but this didn't work...
Could you please change that to "void print(char* str)" and try again? Iirc, using [] in parameter lists doesn't always work well.
Ok, i'll try it :)

//Edit: hmmmm char* str doesn't work too... Would it help, if i post more source code? But there is not much more.... only a main file, bootloader (taken from the bare bones tutorial) and functions like clr_scr() and printc() ....
bazi
Posts: 7
Joined: Fri Mar 20, 2009 3:15 am
Location: Allgaeu (Germany, BW)

Re: Trying to get a string printed on screen

Post by bazi »

Now i'm confused.... I got it working, but not the way i want to.

With this print()

Code: Select all

void print(char str[]) {
	unsigned int count=0;
	while(str[count] != 0) {
		printc(str[count]);
		count++;
	}		
}
and this code:

Code: Select all

char bla[] = "hallo";
print(bla);
But print("hallo"); isn't working...
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Trying to get a string printed on screen

Post by AJ »

Hmmm - this really sounds like a .rodata problem. I guess with the barebones you are using GRUB and therefore the section is being loaded properly?

The one other thing I can think of is a stack-based parameter passing problem. Are you happy that you have a reasonable sized stack that is stored away from the rest of your code?

Cheers,
Adam
bazi
Posts: 7
Joined: Fri Mar 20, 2009 3:15 am
Location: Allgaeu (Germany, BW)

Re: Trying to get a string printed on screen

Post by bazi »

ouh, it was a really stupid mistake...
I've exported CC and AS for easier use in Makefiles. But the bins were /usr/bin/gcc-linux-.... instead of /usr/cross-....
This was caused by copy&paste from a existing Makefile from another project...

I can't say why the other compiler didn't work correctly, but i can say that it did'nt ;) Now this problem is solved :)

Thanks a lot guys!
Post Reply