在Ubuntu下,从零开始写操作系统(0)-笔记

 

1.安装Ubuntu 16.04操作系统 32位,因为16.04版本是最稳定的版本。安装方法请百度。
2.安装bochs; 命令:sudo apt-get install bochs
3.安装gcc;可能系统没有自带gcc, 命令:sudo apt-get install gcc

编写如下代码
 

//16位的代码段
.code16
//代码起始
.text    
mov %cs,%ax    
mov %ax,%ds //设置ds,es和cs为同一段    
mov %ax,%es    
call Dispstr //显示字符串    
jmp    
.Dispstr:    
mov $bootmsg,%ax    
mov %ax,%bp    
mov $16,%cx    
mov $0x1301,%ax    
mov $0x0c,%bx    
mov $0,%dl    
int $0x10 //调用10号中断的显示字符串功能    
ret
bootmsg: .ascii "Hello,OS World!"
.org 510
.word 0xaa55

编写Makefile文件如下

OBJCOPY=objcopy

all: boot.img

#Step 1:gcc 调用as将boot.S编译成目标文件boot.o
boot.o: boot.S
	$(CC) -c boot.S
#Step 2:ld 调用脚本foot.ld将boot.o链接成可执行文件boot.elf
boot.elf: boot.o
	$(LD) boot.o -o boot.elf -e -c -T$(LDFILE)
#Step3: objcopy 移除boot.elf 中无用的section(.pdr,.comment,.note),
#	strip 掉所有符号信息,输出为二进制文件 boot.bin
boot.bin: boot.elf
	@$(OBJCOPY) -R .pdr  -R .comment -R .note -S -O binary boot.elf boot.bin
#Step 4:生成可启动软盘镜像
boot.img: boot.bin
	@dd if=boot.bin of=boot.img bs=512 count=1	#用boot.bin 生成镜像文件第一个扇区
	#在bin生成镜像文件后补空白,最后成为合适大小的软盘镜像
	@dd if=/dev/zero of=boot.img skip=1 seek=1 bs=512 count=2879
clean:
	@rm -rf *.o *.elf *.bin *.img


编译成功后,使用bochs -qf os,运行,os文本内容如下:


#Configuration file for Bochs(Linux) 
#filename of ROM images 
romimage: file=$BXSHARE/BIOS-bochs-latest 
#=======================================================================
vgaromimage:file=$BXSHARE/VGABIOS-lgpl-latest
#=======================================================================
#what disk images will be used 
#=======================================================================
floppya: 1_44=./boot.img, status=inserted
#=======================================================================
# choose the boot disk. 
#=======================================================================
boot: floppy
#=======================================================================
#where do we send log messages?  
#=======================================================================
log:bochsout.txt
#=======================================================================
#disable the mouse
#=======================================================================
mouse: enabled=0
#=======================================================================
#enable key mapping, using US layout as
keyboard: type=mf, serial_delay=200, paste_delay=100000
#=======================================================================
#how much memory the emulated machine will have
#=======================================================================
megs:32

 

你可能感兴趣的:(编程)