String defined outside function equals to null

Programming, for all ages and all languages.
Post Reply
Modestas
Posts: 8
Joined: Sat Dec 20, 2014 5:17 am

String defined outside function equals to null

Post by Modestas »

Hello. I've been learning about interrupt descriptor table and when I wanted to define string array which contained all exceptions I've encountered weird problem. If I define string array inside function, so I can print it on the screen, but if I define array outside function it prints only 'S' character. I've checked random string (from the array) length and it was zero. This happens even with simple string, not only with the string array, except with simple string it doesn't prints anything unlike string array.

This way it works (I used simple string in this example to make it shorter):

Code: Select all

void kmain()
{
	char string[] = "Hello World!";
	monitor_print(string);
}
And this way it's not working, if I print string from array I get only 'S' char, and with this string I get nothing.

Code: Select all

char string[] = "Hello World!";
	
void kmain()
{
	monitor_print(string);
}
Here's how monitor_print looks like:

Code: Select all

void monitor_print(unsigned char* message)
{
	int i;
	for(i = 0; i < strlen(message); i++){
		monitor_print_char(message[i]);
	}
}

void monitor_print_char(unsigned char c)
{
	unsigned short* where;
	unsigned attr = attribute << 8;

	where = vid_mem_ptr + (cursor_y * 80 + cursor_x); //vid_mem_ptr = (unsigned short*)0xb8000
	*where = c | attr;
	cursor_x++;
	
	if(cursor_x >= 80){
		cursor_x = 0;
		cursor_y++;
	}
	
	monitor_scroll();
	monitor_move_cursor();
}
Why is this happening?
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: String defined outside function equals to null

Post by iansjack »

It's likely that initialized data isn't being loaded to the correct memory address. You can check this using a debugger if you are running in a virtual machine.

Sorry not to be more specific, but you don't say how you are linking the file, what file format you are using, what bootloader you are using, or whether you are running on a real machine or in a virtualized environment and, if so, which one. Can we assume that you are using a cross-compiler?
Modestas
Posts: 8
Joined: Sat Dec 20, 2014 5:17 am

Re: String defined outside function equals to null

Post by Modestas »

iansjack wrote:It's likely that initialized data isn't being loaded to the correct memory address. You can check this using a debugger if you are running in a virtual machine.

Sorry not to be more specific, but you don't say how you are linking the file, what file format you are using, what bootloader you are using, or whether you are running on a real machine or in a virtualized environment and, if so, which one. Can we assume that you are using a cross-compiler?
I'm not using cross-compiler, running os with qemu, bootloader is from nick blundell's video tutorials, and this is how I compile and link everything:
nasm boot.asm -f bin -o boot.bin
nasm tables.asm -f elf -o tables.o

gcc -ffreestanding -c kernel.c -I./c_includes -o kernel.o -m32
gcc -ffreestanding -c monitor.c -I./c_includes -o monitor.o -m32
gcc -ffreestanding -c string.c -I./c_includes -o string.o -m32
gcc -ffreestanding -c desc_tables.c -I./c_includes -o desc_tables.o -m32
gcc -ffreestanding -c isr.c -I./c_includes -o isr.o -m32

ld -o kernel.bin -Ttext 0x7e00 kernel.o monitor.o string.o desc_tables.o tables.o isr.o -m elf_i386 --oformat binary

cat boot.bin kernel.bin > os_image.img
truncate os_image.img -s 20480

qemu-system-x86_64 os_image.img
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: String defined outside function equals to null

Post by Combuster »

See the Beginner Mistakes on tutorials, and the Posting Checklist for all the questions you shouldn't have needed asking us and we shouldn't have needed asking you.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: String defined outside function equals to null

Post by iansjack »

That confirms my suspicion that the static variables are not in the correct place. As you are running under qemu, you can use the built in debugger or gdb to confirm this. But, as already mentioned, you are leaving yourself open to a lot of potential problems.
Post Reply