- 一:安装nasm
1、首先下载nasm的压缩包http://www.nasm.us/pub/nasm/releasebuilds 找到2.14rc16 (版本号,可以选择其他的版本),之后进入找到nasm-2.14rc16.tar.gz 这个压缩包(一般在linux上面使用这个压缩形式),下载下来
2、解压:
tar zxvf nasm-2.14rc16.tar.gz
3、之后进入到解压的文件,
cd tar nasm-2.14rc16
然后依次执行一下三条命令
./configure
make
sudo make install
到此,nasm就在ubuntu上面安装好了。可以通过
nasm -version
查看版本信息,如果出现版本信息,则说明安装成功。
4、在ubuntu用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的目录中,并进入目录执行如下命令:
nasm -f elf64(elf32) hello.asm (注意这里使用elf64还是elf32要看操作系统的位数来决定)
gcc -o hello hello.o
./hello
如果输出了hello wrod 则说明安装成功了。
- 二:安装bochs
安装bochs的步骤比较麻烦,因为他需要很多依赖文件,在这里总结了安装bochs的所有问题,可以按照这个步骤安装:
1、从官网下载源码:http://sourceforge.net/projects/bochs/files/bochs/ 找到要安装的版本,同样下载.tar.gz格式的压缩包
2、在安装之前先安装一些依赖项(不然在后面安装的过程中还要安装,还会出现各种乱七八糟的问题,以及各种文件丢失,劝各位不要走我的老路,先把这些安装上)
sudo apt-get install build-essential(gcc,make基本工具支持,注意bochs是用C++写的,可能需要安装g++-sudo apt-get install g++)
sudo apt-get install libx11-dev,
sudo apt-get install libxrandr-dev,
sudo apt-get install xorg-dev(x window的图形的支持),
sudo apt-get install libgtk2.0-dev
sudo apt-get install vgabios
3、编译安装
tar -xvf bochs-2.6.2.tar.gz
cd bochs-2.6.2
./configure --with-x11 --enable-debugger --enable-disasm
make
sudo make install
make clean(清除编译中产生的文件,例如*.o文件之类的,如果放着不管的话,很占空间)
注:如果在此过程中出现问题:
A:make的时候提示 /usr/bin/ld: gui/libgui.a(gtk_enh_dbg_osdep.o): undefined reference to symbol pthread_create@@GLIBC_2.1 // lib/i386-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line collect2: error: ld r
解决办法:
在bochs-2.6.2(也就是解压后的那个文件夹)中找到Makefile文件,打开,找到libs这一行,再其后面添加
-lz -lrt -lm -lpthread
B:make的时候出现
/usr/bin/ld: gui/libgui.a(gtk_enh_dbg_osdep.o): undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' /
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
解决方法:
同上,在Makefile的libs中添加:
-lpthread
如果你安装前面的所有的话,应当没啥问题了。
至此,bochs安装完毕
直接输入bochs即可运行
- 三:现在撸起袖子,让我们怀揣着一颗真心,对世界充满希望的来运行我们Hello ,OS world !
1、先写上如下test.asm汇编代码
org 07c00h
mov ax,cs
mov ds,ax
mov es,ax
call DispStr
jmp $
DispStr:
mov ax,BootMessage
mov bp,ax
mov cx,16
mov ax,01301h
mov bx,000ch
mov dl,0
int 10h
ret
BootMessage: db "Hello,OS Wrold!"
times 510-($-$$) db 0
dw 0xaa55
2、制作img镜像文件
首先,用nasm编译成bin文件
nasm test.asm -o test.bin
这下你会发现在当前文件下多了一个test.bin文件,之后,利用linux自带的dd工具,输入
dd if=test.bin of=test.img bs=512 count=2
这样就生成了test.img镜像文件
3、配置bochs
新建一个bochsrc文件(注意,没有后缀),里面输入如下内容:
###############################################################
# Configuration file for Bochs
###############################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/vgabios/vgabios.bin
# what disk images will be used
floppya: 1_44=test.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_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
这里面要注意romimage,vgaromimage 这两个参数的地址,你可以去你自己的电脑上看看地址和你的是否对应,如果不对应改成你自己的地址
4、启动bochs
在终端下输入 bochs -f bochsrc
随后输入 6
你会发现弹出一个小东西,可是为毛是黑屏呐!不要着急,接着在终端输入 c
Hello,OS World!已经输出!
到此,我们已经在ubuntu上安装nasm,bochs,并且制作一个小镜像文件,输出os的logo了!