Page 1 of 1

Compiling a C program for my Assembly OS

Posted: Thu Oct 23, 2008 10:45 am
by IanSeyler
Hello everyone,

I'm looking for some help with properly compiling a C program for my Assembly-based OS.

Here is some background info:
My OS is written in 64-bit x86 assembly.
The C program also needs to be 64-bit.
Currently I am not interested in any library calls (like stdio, stlib, etc).
I can compile the program in Linux using GCC (Using Arch Linux in 64-bit mode).

I have had some success with this using the following:

Code: Select all

int main()
{
	return 0x12345678;
}
I compile the code with these commands:

Code: Select all

gcc -c testc1.c
ld -r -Tdata 0x100 -e _start -s -o testc1.out testc1.o
objcopy -O binary -j .text testc1.out testc1.bin
I can then run testc1.bin and upon execution it sets EAX to 0x12345678. (Not sure why I can't return with a 64-bit integer.. Any insight?)

The real issue is trying to get a C program to be able to write to the screen:

Code: Select all

int main(void)
{
	char *str = "Hello, world", *ch;
	unsigned short *vidmem = (unsigned short*) 0xb8000;
	unsigned i;

	for (ch = str, i = 0; *ch; ch++, i++)
		vidmem[i] = (unsigned char) *ch | 0x0700;

	return 0x12345678;
}
I can compile the code with the same example above but it does not include the "Hello, world" string in the binary. Is there something wrong with my arguments to "ld" or "objcopy"?

Thanks,
-Ian

Re: Compiling a C program for my Assembly OS

Posted: Thu Oct 23, 2008 10:58 am
by Walling
Strings are constants and hence included in the .rodata (read-only data) section. It is a good idea to use a linker script with LD to control exactly which sections you want and where to load them. See Printing to Screen (Missing Strings).