TCC: Difference between revisions

198 bytes added ,  26 days ago
m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
Line 111:
 
===start32.asm===
<sourcesyntaxhighlight lang="asm">
; Tutorial: A small kernel with Fasm & TCC
; By Tommy.
Line 149:
jmp @b
 
</syntaxhighlight>
</source>
 
===kernel.c===
<sourcesyntaxhighlight lang="c">
/* Tutorial: A small kernel with Fasm & TCC
* By Tommy.
Line 175:
}
 
</syntaxhighlight>
</source>
 
==Compiling and linking==
Assemble start32.asm with:
<sourcesyntaxhighlight lang="bash">
fasm start32.asm
</syntaxhighlight>
</source>
 
Compile kernel.c with:
<sourcesyntaxhighlight lang="bash">
tcc -c kernel.c
</syntaxhighlight>
</source>
 
Then link the whole thing with:
<sourcesyntaxhighlight lang="bash">
tcc -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o -o kernel-i386.elf
</syntaxhighlight>
</source>
 
If you would prefer it in binary form, for example, if you're using your own bootloader that doesn't support ELF, link it with this:
<sourcesyntaxhighlight lang="bash">
tcc -nostdlib -Wl,-Ttext,0x100000 -Wl,--oformat,binary -static start32.o kernel.o -o kernel-i386.bin
</syntaxhighlight>
</source>
 
That's all!
Line 202:
==Inline Assembly==
TCC supports inline GAS syntax assembly like GCC:
<sourcesyntaxhighlight lang="c">
__asm__ __volatile__("hlt");
</syntaxhighlight>
</source>
You can use this to your benefit for many things, such as debugging in Bochs:
<sourcesyntaxhighlight lang="c">
#define breakpoint() __asm__ __volatile__("xchg %bx, %bx");
 
Line 216:
}
}
</syntaxhighlight>
</source>
Then adding this to your bochsrc.bxrc file in a text editor:
<sourcesyntaxhighlight lang="text">
port_e9_hack: enabled=1
magic_break: enabled=1
</syntaxhighlight>
</source>
And from boch's install location, executing bochsdbg.exe instead of bochs.exe.
 
Line 228:
So if you use structs to store your GDT entries or GDTR, beware, you will encounter issues loading your GDT if you don't specify the packing of structures correctly.
When creating structures, use something like this:
<sourcesyntaxhighlight lang="c">
// We use the attribute 'packed' to tell TCC not to change any of the alignment in the structure.
struct some_struct {
Line 236:
// This last attribute can be kept, it won't interfere with the compilation or output, so it may be
// useful to retain compatilbity with GCC, as long as the above attributes don't interfere with GCC.
</syntaxhighlight>
</source>
Instead of this:
<sourcesyntaxhighlight lang="c">
// We use the attribute 'packed' to tell GCC not to change any of the alignment in the structure.
struct some_struct {
Line 244:
unsigned char b;
} __attribute__((packed));
</syntaxhighlight>
</source>