如何实现自己的操作系统

从零实现一个操作系统(一) 从零实现操作系统(一) 阅读详情
如何实现自己的操作系统
 

How to begin to write an Os
---------------------------
This tutorial is meant for those who are familiar with assembly languges and some knowledge of system internal. Writing an Os is a challenging project. The initial step of writing one is to code an efficient boot loader to load the kernel.

What happens when you boot a computer?

- The Bios is started first
- It read the boot sector and loads it into the memory

The boot sector is the first sector of the disk. A sector contain 512 bytes and that is the limitation to write our boot loader. So the boot loader must be at the most 512 bytes. So we must load the real program or the kernel using the loader. The boot sector is loaded at the physical address 0000:7c00.

And how does the Bios knows whether the disk has a valid bootsector?

The final two bytes of the bootsector ie; bytes 511 and 512 should be Aah and 55h respectively. In other words, the last word in the bootsector should be AA55h.

Tools required:

- NASM or any other good assembler

- Interrupt list like Ralf Brown's interrupt list

- PC Emulator like Bochs will help you and avoid frequent reboot of the system. But this is not a compulsory requirement.


Now we will move into the coding part. Let's start with a simple boot loader.

;***************************
; A simple bootloader-bl.asm
;***************************

org 0x7c00 ; Bios loads our loader at this point

mov si, msg ; Displays a message on the screen
call displaystring

mov ah, 0 ; Wait for key
int 016h

db 0EAh ; Machine reboot
dw 0000h
dw 0FFFFh

displaystring: ; Function to display string
lodsb
or al,al
jz short finish
mov ah,0x0E ; Echo a char
mov bx,0x0007
int 0x10
jmp displaystring
finish:
retn

msg db 'Press Any Key …', 13, 10, 0

times 510-($-$$) db 0 ; Fills the rest of the space except the last two bytes with zero
dw 0AA55h ; Magic word for a valid bootsector

;***************************
; The End
;***************************

Now we have to compile this to plain binary. And then write the bl.bin into the bootsector.

NASM bl.asm -f bin -o bl.bin

You can use any programs to write the binary file onto the bootsector. Even the good old Debug.exe can also be used for that.

c:/>debug bl.bin
-w 100 0 0 1 ; writes bl.bin into the floppy bootsector
-q
c:/>

Set the defualt boot to floppy and boot the machine.

If you are using a Bochs freedos emulator, you will have to do the following steps;

- Edit file 'bochsrc' and change boot to 'a' ie; boot: a
- Copy the binary file 'bl.bin' into the Bochs directory and rename as 'a.img'
- Run 'Bochs.exe'

You will see the message 'Press Any Key …'

He he….Did I hear you screaming in joy? Yes, you have written a successful bootloader.

This is not the end. The main purpose of a bootloader is not yet sattisfied. And what is it?
You will have to load the kernel into the memory because a 512 bytes of code will not run a machine with all its power. The next step is to load a simple kernel into the memory.

I have used many interrupt calls in the following example code. Each of them is properly commented.

First of all we have to allocate stack for our program. Then load the kernel into any know address. I have loaded the kernel into 7c00h + 512 for the simplicity of compiling the loader and kernel in the same program. Before loading we have to reset the disk ie; bringing the disk head to the track zero. This is same like seeking the file descriptor to beginning of the file. Then load the kernel into memory. And what mext? Ofcourse jump into the loaded kernel. So simple…isn't it? Try studying this example code given below.

;**********************************
;***********Boot Loader************
;**********************************

; Boot Loader by Fajib
; This is the boot sector….
; This will load the real os from the disk into the memory

org 0x7c00

start:
; setting the stack for loading the program

cli
mov ax, 0x9000
mov ss, ax
mov sp, 0xffff
sti

mov [bootdrv], dl ; dl contains the bootdisk name ie; dl = 0 if floppy a:/

call load ; function to load the kernel from disk into memory

mov si, msgloadsuccess
call putstr

jmp loadkernel ; I can directly jump into kernel becoz I load the kernel
; at 7c00 + 512

;hangs if kernel not loaded
oops:
mov si, msghang
call putstr
jmp oops

retf
; loader variables

bootdrv db 0
msgresetfail db 'Disk Reset Failure!', 13, 10, 0
msgresetsuccess db 'Disk reset success…', 13, 10, 0
msgkernelload db 'Loading kernel…', 13, 10, 0
msgloadsuccess db 'Kernel loaded successfully…', 13, 10, 0

msghang db '.', 0

; loader functions

load:

; we have to reset the disk before it move to the loaded program
; say our real program is stored in sector 2
; let's load it

push ds ; reset disk system
mov ax, 0 ; forces controller to recalibrate drive heads (seek to track 0)
mov dl, [bootdrv]
int 13h
pop ds
jc resetfail

mov si, msgresetsuccess
call putstr

mov ax,0 ; loads sector into memory
mov es,ax
mov ah,2
mov al,1 ; loading 1 sector-kernel will be loaded. Increase al for loading more
mov dx,0
mov cx,2 ; ch = cylinder number and cl = sector number 1-63

mov bx,7e00h ; 7e00h = 7c00h + 512 … loading my program here makes it easy for me
int 13h ; we can directly compile bootsector and kernel together in a single
; program

jc load ; if fail then try to load it again

mov si, msgkernelload
call putstr

retn

resetfail:

mov si, msgresetfail
call putstr
retn

putstr:
lodsb
or al,al
jz short putstrd
mov ah,0x0E
mov bx,0x0007
int 0x10
jmp putstr
putstrd:
retn

times 510-($-$$) db 0 ; Filling the remaining free space in the sector
dw 0AA55h


;**********************************
;*********End Boot Loader**********
;**********************************

;**********************************
;**********Start Kernel************
;**********************************
;Kernel stored in Sector 2

loadkernel:

mov si, mymsg
call putstr

call Console ; Simple Console

db 0EAh ; machine reboot
dw 0000h
dw 0FFFFh

mymsg db "Message from Fajib's Simple Kernel…", 13, 10, 0

Console:
mov si, msgconsole
call putstr

mov si, prompt
call putstr

Waitkey:
mov ah, 00h ; Wait for key
int 016h

cmp al, 27 ; Check for ESC key
je Finish

cmp al, 13 ; Check for ENTER key
je NewLine

jmp EchoChar

NewLine:
mov si, prompt
call putstr
jmp Waitkey

EchoChar:
mov ah, 0x0E ; Display the char with out attribute
mov bx, 0x0007
int 10h

jmp Waitkey
Finish:
retn

prompt db 13, 10, 'C:/>', 0
msgconsole db 13, 10, 'Console: Press ESC to reboot', 13, 10, 0

;**********************************
;***********End Kernel*************
;**********************************

Wow! You have now loaded a kernel successfully… But do not stop here… Improvise your kernel as good possible… Experimentation is the best learning method… Do inform me about your improvements and bugs in my code to me at;

Email: fajib@mail.com
Irc: I will be there in #osdev as Devil_liveD

Orange'S:一个操作系统的实现 5月18日见到了《OrangeS:一个操作系统的实现》的样书,多少有些激动。想一想前一版本《自己动手写操作系统》是那么畅销,这一本一定不能含糊。整个出版过程我能看到作者于渊为此付出的努力,还在自己排版的过程有深入体会,通过于渊的讲座也让博文视点的员工分享到他在排版过程中的很多心得。 应该有几万个朋友读过《自己动手写操作系统》了,本书的第2版《OrangeS:一个操作系统的实现》出来肯定 阅读详情

相关推荐

解密《一个操作系统的实现》这本书

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!                解密《一个操作系统的实

akdjhf的博客 1957

操作系统实现

详细过程请参考B站Up踌躇月光。 1 开发环境搭建 1.1 模拟器与汇编器安装 基于bochs硬件模拟器开发,使用的操作系统为ubuntu20.04。 在终端执行以下命令安装bochs和汇编器。 sudo apt install bochs sudo apt install bochs-x sudo apt install nasm ...

Ingsuifon的博客 1224

探索微内核操作系统的奥秘:《操作系统设计与实现 第3版》

如果你对操作系统充满好奇,或者需要为相关课程和研究寻找一本参考书籍,《操作系统设计与实现 第3版》无疑是一个很好的选择。它不仅能够提供深入的理论知识,还能够帮助你在实践中获得宝贵的经验。探索操作系统的奥秘,从这本书开始。

一个夜猫子的博客 964

[自己实现一个操作系统]——专栏文章汇总

自己实现一个操作系统 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录自己实现一个操作系统前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正

Lw_wLO的博客 2463

一个操作系统的实现(一)

在学操作系统,老师教的是清华的moocos操作系统,是根据一些基础代码填空式学习,个人不太习惯,故开始了自己探索的历程,也就有了这一系列博客(不定期更新) 一个操作系统的实现系列是按照Orange’S:一个操作系统的实现这本书一步步做的,这是案例1(所有案例基于linux系统,下面会有在虚拟机安装ubuntu教程) 在清华镜像下载ubuntu的镜像(我用的是最新版20.04) 在virtualBox安装ubuntu(虽然版本不同,但是操作基本一致) 然后再u’bu’nubun ...

Edith的博客 3106

一个64位操作系统的实现

一个64位操作系统的实现

TYHH2001的专栏 3452

C语言实现一个简简单单的操作系统例子

在实际开发中,我们可以根据操作系统的不同功能,设计不同的数据结构和算法,来实现复杂的操作系统。例如,我们通过授权字段来模拟操作系统的授权方式,通过稳定性字段来模拟操作系统的稳定性。上面的代码,可以继续扩展操作系统结构体中的字段,例如增加操作系统支持的硬件设备、操作系统的内核版本等信息。此外,在开发操作系统时,我们还需要考虑操作系统的兼容性、性能优化等问题,以确保操作系统的稳定性、高效性。上面的代码中,我们定义了一个操作系统结构体,并实现了初始化、显示信息、安装、升级、卸载操作系统的方法。

qq_26183767的博客 6337

动手实践:操作系统设计与实现全攻略

本文还有配套的精品资源,点击获取 简介:操作系统是计算机科学的核心,负责管理和抽象计算机硬件资源,提供用户和应用程序界面。本主题深入探索操作系统的原理、设计和实现机制,包括进程管理、内存管理、文件系统、设备管理、安全与权限、网络支持、用户界面、并发与同步、启动与引导等关键组件和功能。学习者需要理解这些概念,并可能编写简单的操作系统代码实践,涉及到汇编语言、C语言以及底层计算...

weixin_42350014的博客 1770

操作系统多线程实现_操作系统中的线程实现

操作系统多线程实现Each process has an address space. There is one thread of control in every traditional OS. Sometimes, it is viable to have multiple threads of control in the similar address space which is ru...

cumtb2002的博客 1134

操作系统文件系统模拟实现项目

在现代计算机系统中,操作系统是管理硬件资源并提供用户友好的界面的软件核心。它负责处理文件和目录的组织、访问控制、存储空间分配以及数据保护等问题。文件系统则是操作系统中负责管理和控制文件的软件组件。本章将从操作系统的基础知识开始,深入探讨文件系统的基本功能和其在操作系统中的重要作用。文件系统是一个操作系统中用于管理数据的抽象数据类型。它负责在磁盘或其他存储设备上存储、检索和更新文件,同时确保数据的完整性和安全性。

weixin_32480007的博客 1071

一个操作系统的实现源码

作为中国人,能写操作系统的人很少了,本人提供了操作系统实现的源码,该源码来自于渊朋友,读后,收益匪浅

常见操作系统tcp/ip实现

常见操作系统tcp/ip实现

weixin_42306440的博客 1368

django安装mysqlclient (Windows下正确安装mysqlclient)

由于本人也是初学Python和django。本人使用的Python版本是3.7所以连接MySQL数据库需要安装数据驱动,对应Python3的数据驱动是mysqlclient。因此本人习惯性的使用命令pip install mysqlclient,结果安装失败 (venv) C:\Users\songlihui\PycharmProjects\autotest>pip install my...

songlh1234的博客 2万+

《操作系统实现之路》前言及内容提要

以讲述Hello China操作系统原理和实现为主线的《操作系统实现之路》已由机械工业出版社出版,预计4月中旬上市,欢迎朋友们评阅。以下是封面、前言和内容提要。 前言 欢迎阅读本书,首先说一下阅读本书所需要的一些基础知识。本书聚焦于操作系统的实现,对实现所需要的工具,比如C语言、汇编语言等,并没有做深入细致介绍。因此需要读者首先具备这些编程语言的基本知识和使用技能。在此基础上,建议读者

物联网操作系统HelloX技术论坛 5340

STM32F4使用CUBEMX ADC定时采样+DMA

STM32、FFT、ADC、TIM

m0_62379750的博客 4227

RedHat 9 替换yum源镜像

引言:我们个人通常使用的红帽系统yum源是需要注册付费的,所以在使用yum源安装一些软件时无法正常安装(例如:MySQL),这时我们就需要将yum源替换为CentOS对应的源,以实现在红帽系统中正常安装软件。

YUJIAOZHU666的博客 3740

ORANGES:操作系统实现的宝藏指南

ORANGES:操作系统实现的宝藏指南 【下载地址】ORANGES一个操作系统的实现高清晰版PDF 探索操作系统的奥秘,从理论到实践,一本不可错过的经典之作。《ORANGE’S:一个操作系统的实现》高清PDF版本现已上线,专为追求极致学习体验的读者打造。本书深入浅出地解析操作系统核心原理,结合高清图文,助你轻松掌握从基础...

gitblog_06736的博客 866

使用sysbench对Oracle做基准测试(-) 安装篇

sysbench是一款开源的多线程性能测试工具, 可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL。

道行尚浅(老道)的博客 7812

华农操作系统课设-模拟操作系统实现

题目六 模拟操作系统实现 代码实现:link.

qq_43751913的博客 4111

修改windows npt时钟服务器

然而,由于网络环境的限制,微软的NTP服务器在国内体验并不好,有时,时间误差能达到5S以上。ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s2h.time.edu.cn 四川大学网络管理中心。s2j.time.edu.cn 大连理工大学网络中心。s2g.time.edu.cn 华东南地区网络中心。s2d.time.edu.cn 西南地区网络中心。s2e.time.edu.cn 西北地区网络中心。s2f.time.edu.cn 东北地区网络中心。

qq_37371407的博客 2298

HDLbits找茬实战:5个Verilog仿真Bug修复案例,新手避坑指南

本文通过HDLbits平台上的5个典型Verilog仿真Bug案例,详细解析了多路选择器位宽不匹配、模块实例化端口映射错误等常见问题,提供实用的修复方案和调试技巧,帮助Verilog新手快速掌握代码验证与仿真技能,避免常见陷阱。

weixin_42653691的博客 165

操作系统实现之路

操作系统实现之路 系统调用(System Call)是实现应用程序与操作系统核心模块物理分离的基础。如果没 有系统调用,那么应用程序必须与操作系统核心模块进行静态链接。这样在开发应用程序的 时候,还必须把操作系统源代码(或编译后的二进制模块)纳入应用程序的项目范围。 对其内容详细阅读

拟声的主扬在江湖 642

Qwen2.5-VL多模态微调超参数深度解析(上):训练流程与优化策略的量化机制分析

上篇文章通过系统化的对比实验,回答了多模态微调中的三个基础问题:“选什么方法”(LoRA/Full/Freeze/OFT对比)、“用什么配置”(60+组超参数组合)、“达到什么效果”(ROUGE-L/BLEU-4指标)。这些结果为后续工作奠定了实验基线,但也留下了更深层的疑问:为何相同方法在不同数据集上表现迥异?为何某些超参数的微小调整会引发性能剧变?如何在未见过的任务上快速找到最优配置?本文是对前文实验数据的二次挖掘与机理解析。我们将关注点从"做了什么实验"转移到"实验揭示了什么规律",实验视角从"结果对

羊城迷鹿的博客 1013

基于 Bochs 的操作系统内核实现

简介 Bochs 简介 Bochs(读音Box)是一个开源的模拟器(Emulator),它可以完全模拟x86/x64的硬件以及一些外围设备。与VirtualBox / VMware等虚拟机(Virtual Machine)产品不同,它的设计目标在于模拟一台真正的硬件,并不追求执行速度的高效,而追求模拟环境的真实,同时带有强大的调试功能,比如观察寄存器、对实地址/虚拟地址下断点、装载符号

xyjikl 4184

【应用】打造你的安全本地AI购物助手:OpenClaw + 淘宝MCP部署全攻略

本文介绍了在Ubuntu虚拟机中配置OpenClaw连接Windows宿主机的淘宝MCP服务的两种方案。核心原理是通过修改MCP服务监听地址或SSH隧道转发,实现跨网络访问。方案1推荐修改MCP服务绑定0.0.0.0并配置防火墙放行端口,适合长期使用;方案2通过SSH隧道转发,适合临时测试。文章详细说明了环境准备、配置文件修改及两种互通方案的具体实施步骤,重点解决了虚拟机与宿主机网络互通的关键问题。

海兰 1336

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

操作系统 线程的实现

线程的实现 主要有三种线程的实现方式: 用户线程:在用户空间实现; POSIX Pthreads, Mach C- threads, Solaris threads 内核线程:在内核中实现; Windows,Solaris, Linux 轻量级进程:在内核中实现,支持用户线程 Solaris(LightWe ight Process) 一个或多个用户线程可以对应多个或一个内核线程 用户线程 在用户空间实现的线程机制,它不依赖于操作系统的内核,由一组用户级的线程库函数来完成线程的管理,包括进程的创建、终止、同

Azadoo的博客 835

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

上一篇: Bochs 调试技术
下一篇: 自己动手写操作系统
xbitor
博客等级 码龄22年 3粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值