步骤
设置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函数中必须要有死循环。