Compiling a C program for my Assembly OS

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
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Compiling a C program for my Assembly OS

Post 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
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Re: Compiling a C program for my Assembly OS

Post 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).
Post Reply