Help! strings don't work

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
tesfabpel
Posts: 2
Joined: Sun Jun 05, 2011 2:16 am

Help! strings don't work

Post 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 :)
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: Help! strings don't work

Post by Combuster »

FAQ, literally :shock:
"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 ]
tesfabpel
Posts: 2
Joined: Sun Jun 05, 2011 2:16 am

Re: Help! strings don't work

Post 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
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Help! strings don't work

Post by Chandra »

Did you try disassembling the final binaries? You may be able to track down the error from there.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Post Reply