自己动手写操作系统——开发环境搭建
本文參考于淵老師寫的《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”, 第一步完成。
總結
以上是生活随笔為你收集整理的自己动手写操作系统——开发环境搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(2383):vue编码规范
- 下一篇: 读 自己动手写操作系统