www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the absolute minimum code for a D kernel, and how should it be

reply "Stijn" <stijn.herreman telenet.be> writes:
After writing a bootloader and getting it to jump to a Hello 
World "kernel" written in assembly, I want to give it a go with a 
"kernel" written in D. I'm using GDC because I didn't have much 
luck with making DMD skip the D runtime and standard libs.

Starting with this code:

     void main() { }

Compiling it like this:

     i686-gdcproject-mingw32-gdc.exe kernel.d -o 
"../bin/kernel.bin" -nophoboslib -nostdlib -s

This is the output:
kernel.d:(.text+0xa): undefined reference to `__main'
kernel.d:(.text+0x24): undefined reference to `_d_run_main'
(.text+0x38): undefined reference to `_Dmodule_ref'
(.text+0x43): undefined reference to `_Dmodule_ref'

According to 
I need something more like this:

     extern(C) __gshared void* _Dmodule_ref;
     extern(C) int main() { return 0; }

Now this is the output:
kernel.d:(.text+0x7): undefined reference to `__main'

So I have two problems here. The first one is that it's not 
working. The second one is that I'm copy pasting code, and 
perhaps by trying a bunch of variations I'll manage to get 
something that compiles, but I probably still won't understand 
what I'm doing and why it does or does not work.

Now I'm hoping that someone can give me a hand with working code, 
and also explain the reasoning behind it.
Jan 06 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
I showed how to do it in my book using dmd. Here's the code:


Explanation is in chapter 11 here:

In the appendix, I also did ARM with gdc:

The arm one might be easier to start with than the dmd one, 
despite dmd being x86. I went a bit further there, even up to 
keyboard support in the second section with interrupt handlers.

Short version of the explanation is that the compiler outputs 
hooks for the runtime library and you need to stub those out as 
you hit them. Some are required just for the beginning.

BTW make sure your bootloader puts the processor in 32 bit more. 
D doesn't support 16 bit code.
Jan 06 2015
parent "Stijn" <stijn.herreman telenet.be> writes:
On Tuesday, 6 January 2015 at 22:41:06 UTC, Adam D. Ruppe wrote:
 I showed how to do it in my book using dmd. Here's the code:

I've just bought the book, I'll dive right into it :) Thanks!
Jan 06 2015