从头开始操作系统系列】环境搭建以及第一个Hello World

网友投稿 707 2022-05-28

写在最前

笔者在动手写此操作系统时是一名大学本科大二的学生,可能文章中会有好多地方我没有解释清楚,可能也会有许多地方出现错误,我恳请各位读者能提出质疑和纠正我的错误,谢谢!

参考书籍:

《30天自制操作系统》川合秀实

《ORANGE’S:一个操作系统的实现》

开发环境

笔者采用以下开发环境

Apple Macbook Air

Ubuntu 12.04 LTS(32 bit)

读者准备

毕竟是动手写一个操作系统,对读者的要求也会相应要求高一些,对计算机编程0基础的读者还是不建议直接看这个系列的文章。希望读者能有 汇编语言、C 语言的基础,对操作系统最好有了解(推荐书籍《操作系统概念》),另外由于开发是在 Ubuntu 的环境中进行的,读者最好对 Linux 的命令、操作等比较熟悉。

环境搭建

安装Bochs 等

Bochs是一个x86硬件平台的开源模拟器。它可以模拟各种硬件的配置。Bochs模拟的是整个PC平台,包括I/O设备、内存和BIOS。更为有趣的是,甚至可以不使用PC硬件来运行Bochs。事实上,它可以在任何编译运行Bochs的平台上模拟x86硬件。通过改变配置,可以指定使用的CPU(386、486或者586),以及内存大小等。一句话,Bochs是电脑里的“PC”。根据需要,Bochs还可以模拟多台PC,此外,它甚至还有自己的电源按钮。

我们所写的操作系统就是模拟在 Bochs 来运行和调试的。

我们使用以下的命令来安装 bochs 等一些工具

sudo apt-get install vgabios bochs bochs-x bximage bochs-sdl

第一个 Hello World

创建软盘镜像

使用bximage 命令

按照以下来进行配置

======================================================================== bximage Disk Image Creation Tool for Bochs $Id: bximage.c,v 1.34 2009/04/14 09:45:22 sshwarts Exp $ ======================================================================== Do you want to create a floppy disk image or a hard disk image? Please type hd or fd. [hd] fd Choose the size of floppy disk image to create, in megabytes. Please type 0.16, 0.18, 0.32, 0.36, 0.72, 1.2, 1.44, 1.68, 1.72, or 2.88. [1.44] 1.44 I will create a floppy image with cyl=80 heads=2 sectors per track=18 total sectors=2880 total bytes=1474560 What should I name the image? [a.img] helloworld.img Writing: [] Done. I wrote 1474560 bytes to helloworld.img. The following line should appear in your bochsrc: floppya: image="helloworld.img", status=inserted

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

最终我们得到了一个名叫『helloworld.img』的文件,它就是我们创建的软盘镜像

操作系统源码

hello.asm

org 07c00h mov ax, cs mov ds, ax mov es, ax call DispStr jmp $ DispStr: mov ax, Str mov bp, ax mov cx, 13 mov ax, 01301h mov bx, 000ch mov dl, 0 int 10h ret Str: db "Hello, world!" times 510-($-$$) db 0 dw 0xaa55

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

下面我们来逐行分析这段汇编代码

org 07c00h 指定程序的起始地址,告诉编译器程序从这里开始

org是Origin的缩写:起始地址源。在汇编语言源程序的开始通常都用一条org伪指令来实现规定程序的起始地址。

mov ax, cs 将 cs(代码段寄存器)中的值赋值给 ax 寄存器

mov ds, ax 将 ax 中的值赋值给 ds( 一个段寄存器)

mov es, ax 将 ax 中的值赋值给 es()

call DispStr 跳转执行到 DispStr

jump $ 死循环

mov ax, Str 将 Str 的首地址放入 ax

mov bp, ax 将 ax 的值赋给 bp(栈底指针)

mov cx, 13 将13放入 ax 寄存器

mov ax, 01301h ax 寄存器高位AH=13,低位 AL=01h

mov bx, 000ch 将000ch 放入 bx 寄存器

mov dl, 0 将光标设置为0行0列

int 10h 调用10h 中断

int 10h 中断功能介绍

times 510-($-$$) db 0 将剩余的位置都填充为0

dw 0xaa55 结束标志

制作操作系统镜像

编译源码

nasm hello.asm -o hello.bin

写入软盘镜像扇区

dd if=hello.bin of=helloworld.img bs=512 count=1 conv=notrunc

运行 HelloWorld

编写 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=helloworld.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 display_library: sdl

1

2

3

4

【从头开始写操作系统系列】环境搭建以及第一个Hello World

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

运行bochs

bochs -f bochsrc

运行结果如下图:

单片机 汇编语言

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:递归
下一篇:被问:这个BUG为什么没测出来?该如何回答
相关文章