保护模式下C语言编程,第5章 在保护模式中调用C语言函数

步骤

设置ds、es、fs、gs、ss选择符指向内核数据段;

调用main函数(main.c文件中的main函数由C语言编写)。

setup.s源代码

.text

.global pm_mode

.include "kernel.inc"

.code32

.org 0

pm_mode:

movl $DATA_SEL, %eax #内核数据段选择符

movw %ax, %ds

movw %ax, %es

movw %ax, %fs

movw %ax, %gs

movw %ax, %ss #ds、es、fs、gs、ss的选择符指向内核数据段,基址是0x00

movl $KERNEL_STACK_BOT,%esp #内核堆栈指向0x7c00

movl $msg, %esi # ds:esi = msg

movl $0xb8000, %edi # es:edi = 0xb8000, 显存地址

movl %edi, %eax

addl $160, %eax

movl %eax, %edi # es:edi = 0xb8000 + 160, 屏幕第二行首地址

cld

movb $0x07, %ah # 字符属性:黑底白字

print_c:

cmp $0, (%esi) #是否是字符串结尾

je end

lodsb # ds:[esi] -> al, ++esi

stosw # ax -> es:[edi] , edi += 2, al=字符,ah=属性

jmp print_c

end: call main

msg: .string "setuping.....\x0\x0\x0\x0"

.org 0x1fe, 0x90

.word 0xaa55

汇编语言调用C语言函数的编译方法是

#as -a boot.s -o boot.o > boot.map

#ld --oformat binary -N -e start -Ttext 0x7c00 -o boot boot.o

#as -a setup.s -o setup.o > setup.map

#gcc -Wall -pedantic -W -nostdlib -nostdinc -Wno-long-long -I include -fomit-frame-pointer -c -o main.o main.c

#ld --oformat binary -N -e pm_mode -Ttext 0x7e00 -o kernel setup.o main.o

创建虚拟软盘文件的方法是

#cat boot kernel > final.tmp

#dd if=/dev/zero of=final.img bs=1024 count=1440

#dd if=final.tmp of=final.img bs=1024 count=1440 conv=notrunc

切记main函数中必须要有死循环。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值