[编译环境][gcc]编译16位程序

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,否则无法成功引导。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值