Page 1 of 1

Help! strings don't work

Posted: Sun Jun 05, 2011 2:24 am
by tesfabpel
Hello, I'm new to OSDev.org, I've followed the Bare Bone guide of C kernel and it works...
But there is something weird when I use strings.

If i do

Code: Select all

cls();
	
char x[] = "Welcome to My OS!\n";
kprint(x);
it works

But if I do

Code: Select all

cls();
	
char* y = "Welcome to My OS!\n";
kprint(y);
it doesn't.
I've noticed that if I do

Code: Select all

if(*y == 0) putchar('?');
the condition is satisfied...

It seems that char* y is different to char x[]
but in normal programming it works both ways.

PS: This is the code of kprint, putchar and cls

Code: Select all

void putchar(char c)
{
	if (c == '\n')
	{
		LF(); //NewLine
		return;
	}
	
	int addr = ((ypos * COLUMNS) + xpos) * 2;
	
	video[addr] = c & 0xFF;
	video[addr + 1] = 0x07;
	
	xpos++;
	if (xpos >= COLUMNS) LF();
}

void kprint(const char* str)
{
	//while(*msg) putchar(*msg++);
	int i = 0;
	while(str[i])
	{
		putchar(str[i++]);
	}
}

void cls (void)
{
	int i;

	video = (unsigned char *) VIDEO;

	for (i = 0; i < COLUMNS * LINES * 2; i++)
		*(video + i) = 0;

	xpos = 0;
	ypos = 0;
}
Thanks in advance :)

Re: Help! strings don't work

Posted: Sun Jun 05, 2011 2:33 am
by Combuster
FAQ, literally :shock:

Re: Help! strings don't work

Posted: Sun Jun 05, 2011 3:20 am
by tesfabpel
Sadly, it doesn't still work... :cry:
However it doens't matter so much...
I just wanted to try it...

Here is my loader.ld (is the one in http://wiki.osdev.org/Strings_do_not_work)

Code: Select all

OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
phys = 0x00100000;

SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
Here the makefile

Code: Select all

all:
	as --32 -o loader.o loader.s
	#nasm -f elf -o loader.o loader_nasm.s
	gcc -m32 -o kernel.o kernel.c kutils.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
	ld -T linker.ld -o kernel.bin loader.o kernel.o -melf_i386
	cat stage1 stage2 pad kernel.bin > os.img
	#qemu -fda os.img #run it
	qemu -kernel kernel.bin

Re: Help! strings don't work

Posted: Sun Jun 05, 2011 5:31 am
by Chandra
Did you try disassembling the final binaries? You may be able to track down the error from there.