查看文章 |
《自写系统》的学习笔记之一 ——实现最小的“操作系统”(1)
2009-06-16 9:15
写在前面的话: 读了《Linux - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - ![]() 由于用的是AT&T语法,只学了 masm 或 nasm 的同学可能会有些不习惯。不要紧,看了符录中《对比 GAS 和 NASM 汇编语法的差异》,对理解这个 boot.s 以及以后用到的汇编应该不成问题了。 ![]() # 文件boot.s ------------------------------------------------------------------------------------------- .global startup_16 #引出全局标号,使得在连接时可指定程序入口 ,请看Makefile .code16 .text startup_16: mov %cs,%ax mov %ax,%ds mov %ax,%es call DispStr #显示字符串 jmp . #进入无限循环 DispStr: mov $BootMessage, %ax mov %ax,%bp mov $0x20,%cx mov $0x1301,%ax mov $0x00c,%bx mov $0,%dl int $0x10 ret BootMessage: .ascii "Hello, the way to ourselves' OS!" .org 510 .word 0xaa55 #启动扇区的结束标志 # 文件boot.s ------------------------------------------------------------------------------------------- # 文件Makefile ----------------------------------------------------------------------------------------- AS =as LD =ld OBJCOPY=objcopy # -R 移掉对应的段; -S 移掉所有的标志及重定位信息; -O binary 生成二进制文件 TRIM_FLAGS=-R .pdr -R .comment -R .note -S -O binary # -m 设定连接的类型; -Ttext 指定程序text段的起始地址,这里设为0x7c00 # 为什么是0x7c00 # -e 指定程序的入口; -s 剔除所有符号; -x 忽略所有本地符号 LDFLAGS =-m elf_i386 -Ttext 0x7c00 all: kernel.img kernel.img: boot.bin @dd if=boot.bin of=kernel.img bs=512 count=1 @dd if=/dev/zero of=kernel.img skip=1 seek=1 bs=512 count=2879 boot.bin: boot.s $(AS) -o boot.o boot.s $(LD) $(LDFLAGS) boot.o -o boot.elf $(OBJCOPY) $(TRIM_FLAGS) boot.elf $@ clean: rm -f *.elf *.img *.o *.bin # 文件Makefile ----------------------------------------------------------------------------------------- 1、把 boot.s 2、用虚拟机加载kernel.img,并设置虚拟机从软盘启动。启动虚拟机,就会看到上面的截图了。 good luck! ![]() 1、对比 GAS 和 NASM 汇编语法的差异 |
最近读者:




