OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 6:28 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: String defined outside function equals to null
PostPosted: Sun Jan 11, 2015 2:47 pm 
Offline

Joined: Sat Dec 20, 2014 5:17 am
Posts: 8
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:
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:
char string[] = "Hello World!";
   
void kmain()
{
   monitor_print(string);
}


Here's how monitor_print looks like:
Code:
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?


Top
 Profile  
 
 Post subject: Re: String defined outside function equals to null
PostPosted: Sun Jan 11, 2015 4:55 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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?


Top
 Profile  
 
 Post subject: Re: String defined outside function equals to null
PostPosted: Mon Jan 12, 2015 4:18 am 
Offline

Joined: Sat Dec 20, 2014 5:17 am
Posts: 8
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:
Quote:
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


Top
 Profile  
 
 Post subject: Re: String defined outside function equals to null
PostPosted: Mon Jan 12, 2015 4:40 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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 ]


Top
 Profile  
 
 Post subject: Re: String defined outside function equals to null
PostPosted: Mon Jan 12, 2015 7:11 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 150 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group