Fasm-TCC BareBones

转自: http://wiki.osdev.org/Fasm-TCC_BareBones

The purpose of this article is to explain how to make a sample ELF kernel with FASM and Tiny C Compiler (aka TCC). TCC is a small and fast C compiler, which produces x86, x86_64 or ARM code, and generates PE or ELF executables. TCC is heading toward full ISOC99 compliance, and can compile itself, like FASM. Here are all features of TCC:

  • TCC compiles C code about 9 times faster than GCC.
  • TCC generates averagely optimized x86 code.
  • TCC supports the ISO C99 standard (even if this support is not as complete as PCC).
  • Under Linux, TCC can be used as a C interpreter (just add '#!/usr/local/bin/tcc -run' at the first line of your C source, and execute it directly from the command line).
  • TCC has few dependencies. Only libc is required for its compilation.
  • TCC includes a linker and an assembler (for x86 only).


Contents

A small kernel example

This little example builds a small kernel in ELF format, which can be booted by Grub.

Note: The Windows version of TCC doesn't produces ELF executables, but only object files. You need to recompile TCC without PE support, if you want to use this tutorial on Windows.

start32.asm

;  Tutorial: A small kernel with Fasm & TCC
;  By Tommy.
 
        format elf
        use32
 
;
; Equates
;
MULTIBOOT_PAGE_ALIGN	    equ (1 shl 0)
MULTIBOOT_MEMORY_INFO	    equ (1 shl 1)
MULTIBOOT_AOUT_KLUDGE	    equ (1 shl 16)
MULTIBOOT_HEADER_MAGIC	    equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS	    equ MULTIBOOT_PAGE_ALIGN or MULTIBOOT_MEMORY_INFO
MULTIBOOT_CHECKSUM          equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
 
        section '.text' executable
;
; Multiboot header
;
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM
 
 
;
; Kernel entry point.
;
        public  _start
        extrn   kmain
_start:
        ; Call the main kernel function.
        call    kmain
 
@@:
        jmp     @b

kernel.c

/*  Tutorial: A small kernel with Fasm & TCC
 *  By Tommy.
 */
 
/*
 * Main kernel function.
 */
void
kmain (void)
{
    *((unsigned char *) 0xB8000) = 'H';
    *((unsigned char *) 0xB8001) = 0x1F;
    *((unsigned char *) 0xB8002) = 'E';
    *((unsigned char *) 0xB8003) = 0x1F;
    *((unsigned char *) 0xB8004) = 'L';
    *((unsigned char *) 0xB8005) = 0x1F;
    *((unsigned char *) 0xB8006) = 'L';
    *((unsigned char *) 0xB8007) = 0x1F;
    *((unsigned char *) 0xB8008) = 'O';
    *((unsigned char *) 0xB8009) = 0x1F;
}

Compiling and linking

Assemble start32.asm with:

fasm start32.asm

Compile kernel.c with:

tcc -c kernel.c

Then link the whole thing with:

tcc -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o -o kernel-i386.elf

That's all!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值