test.c文件如下:
short add(short first, short second);
/*
* 如果用完全用C语言写16位代码,则入口函数必须写成第一个函数。
*
* 虽然使用了freestanding environment,但gcc仍然对main函数额外做了特殊的处理。
* 具体是添加了下面三条指令:
* leal 4(%esp), %ecx
* and 0xFFFFFFF0, %esp
* pushl (%ecx-4)
* 其在main函数最开始初始化了栈顶的位置,故在此需要使用另外的入口函数。
* 在链接过程中,需要在在ld命令中需要使用的-e参数设置该函数为入口函数。
*/
void mymain(void)
{
short a = 0x1122;
short b = 0x3344;
short c;
c = add(a, b);
}
short add(short first, short second)
{
return first+second;
}
makefile文件:
test.img:test
dd if=/dev/zero of=test.img bs=1440K count=1
dd if=test of=test.img bs=512 count=1 conv=notrunc
test:test.o
ld test.o --oformat binary -m elf_i386 -e mymain -o test
test.o:test.s
as test.s --32 -c -o test.o
test.s:test.c
gcc test.c -m16 -ffreestanding -std=c11 -S -o test.s
bochsrc文件:
memory:guest=64, host=64
ata0:enabled=1, ioaddr1=0x1f0, ioaddr2=3f0, irq=14
ata0-master:type=disk, path=test.img
boot:disk
需要注意的是:
在用bochs执行之前,必须手动将test.img文件中第0x1FE字节和0x1FF两个字节分别改为0x55和0xAA,否则无法成功引导。