"Write the OS series from scratch" Environment building and the first Hello World

Source: Internet
Author: User
Tags microsoft dynamics

Written in the first

I write this operating system is a college sophomore students, there may be many places in the article I did not explain clearly, there may also be a lot of mistakes, I urge you readers can challenge and correct my mistakes, thank you!

Reference books:

    • "30 days Homemade operating System" Sichuan-Hopewell Show
    • "ORANGE's: An operating system implementation"
Development environment

The author uses the following development environment

    • Apple Macbook Air
    • Ubuntu 12.04 LTS (+ bit)
Reader preparation

After all, is to write an operating system, the requirements of the reader will also be higher, the computer programming 0 Basic reader is not recommended to see this series of articles directly. Hope that the reader can have the assembly language, C Language Foundation, the operating system is best to understand (recommended book "Operating system Concept"), in addition, because the development is in the environment of Ubuntu, the reader is best for Linux commands, operations and more familiar.

Environment installation Bochs, etc.

Bochs is an open source emulator for the x86 hardware platform. It can simulate the configuration of various hardware. The Bochs simulates the entire PC platform, including I/O devices, memory, and BIOS. Even more interesting is the ability to run Bochs without using PC hardware. In fact, it can emulate x86 hardware on any platform that compiles and runs Bochs. By changing the configuration, you can specify the CPU used (386, 486, or 586), the size of the memory, and so on. In a word, Bochs is the "PC" in the computer. Bochs can also simulate multiple PCs as needed, and it even has its own power button.

The operating system that we write is simulated in Bochs to run and debug.

We use the following command to install some tools such as Bochs

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

First Hello World Create a floppy image
    • Using the Bximage command
    • Configure them as follows
======================================================================== Bximage Disk Image Creation Tool for Bochs $Id: bximage.c,v 1.34 2009/04/14 09:45:22 sshwarts Exp $================= ======================================================= DoYou want to CreateA floppy disk imageorA hard disk image? Please type HDorFd. [HD] Fdchoose thesize  ofFloppy disk image to Create,inchMegabytes. Please type0.16,0.18,0.32,0.36,0.72,1.2,1.44,1.68,1.72,or 2.88. [1.44]1.44I'llCreateA floppy image withCyl= theheads=2Sectors per track= -Total sectors=2880Total bytes=1474560What should I name the image? [A.img] helloworld.imgwriting: [] Done.i wrote1474560bytes toHelloworld.img.The following line should appearinchYour bochsrc:floppya:image="Helloworld.img", status=inserted
    • Eventually we got a file called "Helloworld.img", which is the floppy image we created.
Operating system source code
    • Hello.asm
Org -c00hmovAX, CSmovDS, AxmovES, axPagerDispstrjmp$Dispstr:    movAX, STRmovBP, AxmovCx -    movAx01301HmovBx theChmovDl0IntTenHretSTR:Db"Hello, world!."Times510-($-$$) DB0Dw0xaa55  
    • Let's analyze this assembler code line by row

org 07c00hSpecify the start address of the program and tell the compiler to start here

ORG is the abbreviation for Origin: The origin address source. At the beginning of the assembly language source program, we usually use an org pseudo-directive to implement the starting address of the prescribed program.

mov ax, csAssign values from CS (Code snippet Register) to AX Register

mov ds, axAssigning values in Ax to DS (a segment register)

mov es, axAssign values in Ax to ES ()

call DispStrJump execution to Dispstr

jump $Dead loop

mov ax, StrPut the first address of STR into Microsoft Dynamics AX

mov bp, axAssign the value of AX to BP (Stack-bottom pointer)

mov cx, 13Put 13 into the AX register

mov ax, 01301hAx register high ah=13, low al=01h

mov bx, 000chPut 000ch into BX register

mov dl, 0Set the cursor to 0 rows and 0 columns

int 10hCall 10h Interrupt

    • int 10h Interrupt Function Introduction
AH function Calling Parameters return Parameters
1 Position cursor Type (CH) 0―3 = cursor start line
(CL) 0―3 = Cursor End line
No
2 Place cursor Position BH = page number
DH = line
DL = Column
No
3 Read cursor position BH = page number CH = cursor start line
CL = Cursor End line
DH = line
DL = Column
4 Reading pen Position Ah=0 Stylus not triggered
Ah=1 Light Stroke Hair
Ch= Elephant Motoyuki
bx= Pixel Column
Dh= Word lines
dl= character Column
5 Show Page AL = page number is displayed
6 Screen is initialized or rolled up AL = volume of rows
AL =0 full screen for blank
BH = Entangled line property
CH = upper left corner line number
CL = upper left corner column number
DH = lower right corner line number
DL = lower right column number
7 Screen initialization or volume down AL = volume of rows
AL =0 full screen for blank
BH = Entangled line property
CH = upper left corner line number
CL = upper left corner column number
DH = lower right corner line number
DL = lower right column number
8 Read the properties and characters of the cursor position BH = Display Page AH = Property
AL = character
9 Display characters and their properties at the cursor position BH = Display Page
AL = character
BL = attribute
CX = number of characters repeats
A Show only characters at cursor position BH = Display Page
AL = character
CX = number of characters repeats
E Display character (cursor forward) AL = character
BL = foreground color
13 Display string ES:BP = string Address
CX = string length
DH, DL = Start row
BH = page number
AL = 0,BL = Property
String: Char,char,......,char
AL = 1,BL = Property
String: Char,char,......,char
AL = 2
String: char,attr,......,char,attr
AL = 3
String: char,attr,......,char,attr

times 510-($-$$) db 0Fill the remaining positions with 0

dw 0xaa55End Flag

Making an operating system image
    • Compiling source code

nasm hello.asm -o hello.bin

    • Write Floppy mirror sector

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

Run HelloWorld Write Bochs configuration file-bochsrc
############################################################# # # Configuration file for bochs################################################################ How much memory the emulated machine would haveMegs: +# filename of ROM imagesRomimage: File=/usr/share/bochs/bios-bochs-latestVgaromimage: File=/usr/share/vgabios/vgabios.bin# What disk images 'll be usedFloppya:1_44=helloworld.img, status=inserted# Choose the boot disk.Boot: Floppy# Where do we send log messages?# Log:bochsout.txt# Disable the mouseMouse: enabled=0# Enable key mapping, using US layout as default.keyboard_mapping: enabled=1, Map=/usr/share/bochs/keymaps/x11-pc-us.mapdisplay_library: SDL
Run Bochs

bochs -f bochsrc

Running results such as:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Article Source: http://blog.luoyuanhang.com

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. (Article Source: http://blog.luoyuanhang.com)

"Write the OS series from scratch" Environment building and the first Hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.