Page 1 of 1
First post-compiler question
Posted: Wed Oct 26, 2011 9:44 am
by psychobeagle12
Sorry if this has been asked. I did a search of the forum and could not find a thread that answered my question (I am officially search engine illiterate!) I am trying to start work on a basic 16-bit kernel written in C. I have already written a kernel in assembly that has a working command line interface and supports a few different commands but I really want to expand it so I want to write it in C/C++. I am currently running Ubuntu 11.10 (dual boot with windows vista 32-bit) and am using gcc to compile. I am getting compilation errors with inline assembly however. Here is my issue:
Code: Select all
/tmp/ccJhBB1d.s: Assembler messages:
/tmp/ccJhBB1d.s:12: Error: bad register name `%rbp'
/tmp/ccJhBB1d.s:15: Error: bad register name `%rsp'
/tmp/ccJhBB1d.s:17: Error: bad register name `%rsp'
/tmp/ccJhBB1d.s:20: Error: bad register name `%rbp)'
/tmp/ccJhBB1d.s:33: Error: bad register name `%rbp'
/tmp/ccJhBB1d.s:36: Error: bad register name `%rsp'
/tmp/ccJhBB1d.s:39: Error: bad register name `%rbp)'
/tmp/ccJhBB1d.s:40: Error: bad register name `%rbp)'
/tmp/ccJhBB1d.s:48: Error: bad register name `%rbp'
I use the following command line:
Code: Select all
gcc -nostdinc -fno-builtin -fno-unit-at-a-time -I./include -c -o src/kernel.o src/kernel.c
ld -Ttext=0x0 -o src/stage2.sys src/kernel.o -e _kernel_entry
and the code I have (this is just a test since I just started working with gcc):
Code: Select all
asm (".code16gcc\n");
void putc (char c);
int kernel_entry {
putc ('t');
return 0;
}
void putc (char c) {
asm ("movb %0, %%al\n"
"movb $0x0e, %%ah\n"
"int $0x10\n"
:
: "r"(c));
}
I was wondering if: 1. anyone else has had this issue 2. if so, how to correct it? 3. if no one has had it, can anyone help me correct it? 4. is it a problem with my code, my command line, or gcc itself?
I know this is basic stuff but I am new to gcc/gas syntax for assembly. Really I know what im talking about I just don't know much about what is going on here!!! Thanks in advance for whoever answers my question guys!! Oh btw it is always great to join a new forum!!!
Re: First post-compiler question
Posted: Wed Oct 26, 2011 10:03 am
by Chandra
Are you working with a 64-bit compiler?
Re: First post-compiler question
Posted: Wed Oct 26, 2011 10:09 am
by theseankelly
Hey, a few things are giving you trouble here.
The first clue is that rbp and rsp are 64bit registers. Is your Ubuntu install (and thus gcc install) 64bit?
(see the 64bit section:
http://en.wikipedia.org/wiki/X86#x86_registers)
Also, you've specified "asm (".code16gcc\n");" which means that the assembly you write will be compiled using 16 bit registers, which is what you're after. Unfortunately, this doesn't mean that your C code will be compiled to 16 bit assembly (in fact the rbp and rsp messages make me guess it's being compiled to 64bit assembly).
Your compiler is probably generating 64bit code to push the variable you're passing onto its (64-bit) stack, and then your 16-bit assembly is trying to get that variable off the stack, but it doesn't know anything about 64-bit registers, so it throws that issue.
And again, even if this code compiled, it wouldn't actually run in protected mode because the C portion of the code isn't being treated as 16bit assembly. I'm not really sure if gcc can compile C into 16bit to run in Pmode or not.
However, even if it could, I don't think you want to do that. C code has a bigger footprint than assembly, and you're going to quickly run out of your limited memory in real mode. I'd suggest looking in to standard boot procedures that most bootloaders do (or installing grub and using that): enabling A20, setting up a GDT, loading your kernel (in c) into memory, switching to pmode, and then jumping to the start of your c program (where you loaded it). Switching to pmode is where your proc switches from 16bit to 32bit code, and is where C code compiled with gcc can be run.
You can search for all of those terms on the wiki for more info!
Re: First post-compiler question
Posted: Wed Oct 26, 2011 10:11 am
by theseankelly
Also, if gcc CAN compile to 16bit, it'll have to be through a cross compiler, so here's where I'd start to dig in more about that topic
http://wiki.osdev.org/GCC_Cross-Compiler
Re: First post-compiler question
Posted: Wed Oct 26, 2011 10:15 am
by psychobeagle12
Actually yes, I am on 64-bit Ubuntu. I forgot about that...
. Also, I have a 32-bit kernel in the works as well but I am looking to write one in 16-bit real mode for experience and also possibly a recovery section? I am more or less trying to write it because I would like to have it. It's just kind of a hobby project. I'm not too worried about the large footprint b/c it's going to be mostly very simple anyways. Do you (or does anyone else
) know how to make gcc output 16-bit registers only? That was what I figured the problem was, the fact that gcc was using 64-bit registers, but idk how to prevent it! Thanks for the quick replies though much appreciated!
Re: First post-compiler question
Posted: Wed Oct 26, 2011 10:25 am
by theseankelly
Googled for "16bit compiler" and eventually found bcc:
http://linux.die.net/man/1/bcc
Which is a compiler that targets the 8086 (a 16 bit proc) and should probably generate code that runs on newer procs. I don't think any 8086 features were deprecated...only expanded upon?
Re: First post-compiler question
Posted: Wed Oct 26, 2011 3:45 pm
by psychobeagle12
Ok, so I installed bruce's c compiler (bcc) but am having alot of trouble with it. Whenever I try to compile the following code I get errors (and I KNOW that such a small piece of code doesn't have errors in it!):
Code: Select all
//test.c: test of bcc for 16-bit
int test (char c) {
return 0;
}
int main () {
int a = test ('t');
return 0;
}
I am getting errors that c is not defined, that I am missing a ')' and several other errors. I am on my windows installation right now so I will have to reboot and try again to post the full list. Has anyone had experience with this compiler that could give me some advice?
Re: First post-compiler question
Posted: Wed Oct 26, 2011 3:56 pm
by gerryg400
I've never used BCC but I'm guessing you need K&R style functions. Those // are probably not recognised either.
Re: First post-compiler question
Posted: Wed Oct 26, 2011 4:04 pm
by psychobeagle12
holy crap thanks guys!!! i didn't apparently read the line that says "if you use k&r you will be fine; if you dont read line one slap yourself in the face cuz ur a moron!!" Sorry that was a stupid mistake on my part. I will have to read up on the old k&r style since it's been a while. I will go over it and see what I need to change and next time i promise I will actually read before I post stupid questions.
Yes all, that was the problem
. I guess I can use this now
does anyone know where I can find some documentation on this (besides the little list that is on the site)?
Re: First post-compiler question
Posted: Wed Oct 26, 2011 6:13 pm
by psychobeagle12
Yep. I just pulled my copy off the shelf and guess what? I am now using bcc to compile my 16-bit operating system!!! I had to do alot of research w/ experiments to figure out exactly how bcc passes arguments and such but I got it working nearly perfectly! bcc does some weird stack stuff on function calls (pushes si and di? is this normal behavior? if so i dont recall) and that was causing me issues but I got it all working. thanks everyone for your advice!!!