您的位置:首页 > 其它

自己动手写操作系统——开发环境搭建

2017-04-16 11:25 866 查看
本文参考于渊老师写的《Orange S:一个操作系统的实现》一书。

参考文章:

http://www.linuxidc.com/Linux/2016-10/135905.htm

http://blog.csdn.net/tkp2014/article/details/42527557

实验环境

VMware 11.0 + ubuntu 14.04(64位)

实验过程

1. 安装nasm

首先,在网站http://www.nasm.us/上下载nasm,本文下载版本为nasm-2.12.02.tar.gz。

在Terminal中执行如下命令进行安装:

tar xvf nasm-2.12.02.tar.gz //解压

进入解压的目录


./configure

make

sudo make install

安装完成,输入nasm -version查看是否安装成功。如果出现了nasm的版本信息则说明安装成功。

可以用hello world程序对nasm进行测试。

hello.asm 源代码如下:

section .text
global main
main:
mov eax,4 ;4号调用
mov ebx,1 ;ebx送1表示输出
mov ecx,msge ;字符串的首地址送入ecx
mov edx,14 ;字符串的长度送入edx
int 80h ;输出字串
mov eax,1 ;1号调用
int 80h ;结束 

msge:
db "Hello world!",0ah,0dh


把上面的代码保存为 hello.asm,执行命令:

nasm -f elf64(elf32) hello.asm (注意这里使用elf64还是elf32要看操作系统的位数来决定)

gcc -o hello hello.o

./hello

如果输出“Hello world” 则说明安装成功。

2 安装bochs

Bochs是用C++写的,首先安装C++相关软件,执行命令如下:

sudo apt-get install build-essential

sudo apt-get install xorg-dev

sudo apt-get install bison

sudo apt-get install g++

在网站https://sourceforge.net/projects/bochs/files/ 下载 bochs-2.6.9.tar.gz

与安装nasm类似,执行如下命令:

sudo tar zxvf bochs-2.6.9.tar.gz //解压

进入解压后的目录,运行configure脚本,用于测试机器,C/C++编译器以及一些库。运行:

sudo ./configure –enable-debugger –enable-disasm (这两个是用来开启调试和反汇编功能)

sudo make

sudo make install //安装

若安装遇到错误,参考文章http://www.linuxidc.com/Linux/2016-10/135905.htm

3 启动简易操作系统

首先制作虚拟软盘,执行:

bximage

如下所示:



在询问创建硬盘还是软盘映像的时候,我们输入了“fd”,选择软盘映像,其余选项默认即可。

使用bximage工具完成软盘的创建以后,当前工具目录下就会多出一个a.img,即软盘映像。下一步,我们需要向软盘映像中写入我们的代码,

代码即于渊老师书中的代码。

执行命令:

dd if=boot.bin of=/home/zjd/Downloads/bochs-2.6.9/a.img

if 是下载的镜像所在的位置 ,of 是软盘所在的路径。

至此,软盘准备好了。下一步,建立bochs启动所需要的配置文件,其主要定义了虚拟计算机BIOS&VGA BIOS的路径,内存大小,软盘大小,确定启动盘,日志文件的位置,鼠标是否启用和键盘布局的设定等配置信息。首先新建bochsrc文件,打开文件,进行编辑,写入内容如下:

###############################################################
# Configuration file for Bochs
###############################################################

# how much memory the emulated machine will have
megs: 32

# filename of ROM images
romimage: file=/usr/local/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/local/share/bochs/VGABIOS-lgpl-latest

# what disk images will be used
floppya: 1_44=/home/zjd/Documents/OS/a.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 default.
keyboard: keymap=/usr/local/share/bochs/keymaps/x11-pc-us.map


最后,运行bochs,执行:

./bochs -f ~/Desktop/bochsrc

其中,-f 后面的参数为bochsrc的路径。出现以下界面:



选择选项6即可运行。在终端输入c,显示”hello, world”, 第一步完成。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  操作系统 vmware ubuntu