Operating system experiment 1: compile a boot program that displays strings in the boot sector

Source: Internet
Author: User

Preface:

I have always wanted to write a simple operating system by myself. Unfortunately, the level is limited. Compile and installProgramNot to mention studying the Linux kernel.Code. I bought a minix book and read it for half a month. I don't know how to start writing my first line of code ......

I accidentally discovered that the "write an operating system by myself" written by the source finally gave me the first step in writing an operating system. I would like to thank you very much for your great contributions to the beginners who have studied and compiled the operating system.

It is a pity that it is not a long time, because the second chapter in the book does not completely pass the experiment environment configuration, and the fifth chapter is stuck. Then, the virtual machines used in the experiment were deleted due to the shortage of computer resources. The road to learning is also silent.

Recently, when I visited csdn, I undoubtedly found Yang Xiaobing's big blog and the compiled compiler yc09 AND THE tinix operating system, one of the examples above, the original code for writing a test operating system can be as simple as this-only one yc09 compiler and the virtual machine bochs can be used, and it is no longer necessary to make great effort to configure the experimental environment to install Virtual PC, VMWare, DOS, and Linux, it also uses NASM and GCC, and starts DoS or Linux in the virtual machine again and again. As a result, the enthusiasm for writing the operating system was re-ignited.

 

 

Lab environment: Windows XP

Compiler: yc09

Run the debugging tool bochs.

 

Experiment code structure description:

All experiments are composed of run. C and the actual experiment code of the operating system. Run.c generates the run.exe program. run.exe automatically compiles the experiment code, generates the IMG, and calls the program to run bochs. You only need to compile and compile run.c. After modifying the micro-debugging experiment code, run run.exe directly. I specially set run. C to an infinite loop. Therefore, modifying the test code becomes very simple:

(1.exe run run.exe

(2) view the virtual machine running effect

(3) Shut Down virtual machines

(4) modify the experiment code

(5.exe click Enter in the run.exe Console (one in the first round, two in the future)

(6) return to step 2 ......

 

The following is the experiment code:

A simple program written in the boot sector-displays a string on the screen.

Although there are already countless versions of examples on the internet, I would like to share with you.

 

Code: Run. c

// File: Run. c <br/> // function: Compile the operating system code, create an IMG, generate the bochs configuration file, and run bochs <br/> // run: Use the yc09 compiler to compile and run the code. <Br/> // Author: Miao <br/> // time: <br/> // VM Settings <br/> char * bootsrc = <br/> "Megs: 32/N" <br/> "romimage: file = BIOS-bochs-Latest, address = 0xf0000/N "<br/>" vgaromimage: VGABIOS-elpin-2.40/N "<br/>" floppya: 44 = boot. IMG, status = inserted/N "<br/>" Boot: A/N "<br/>" log: log. out/N "<br/>" Mouse: enabled = 0/N "<br/>" keyboard_mapping: enabled = 1, map = x11-pc-us.map/N "; <br/> // obtain the path of the current folder <br/> char * getfi Lepath () <br/>{< br/> static char filepath [max_path]; // file path, globally valid <br/> // obtain the complete path of the executable file <br/> int filenamelength = getmodulefilename (null, filepath, max_path); <br/> If (! Filenamelength) <br/>{< br/> printf ("An error occurred while obtaining the path of the current running program file! /N "); <br/> return filepath; <br/>}< br/> // remove the program name: run.exe, only keep the folder path <br/> for (INT I = filenamelength; filepath [I]! = '//'; I --) <br/> filepath [I] = '/0'; <br/> return filepath; <br/>}< br/> void main () <br/>{< br/> _ start: <br/> # define fdisk_size 1474560 // image size: 1.4 MB <br/> byte * imgbuffer = new byte [fdisk_size]; // image buffer <br/> char * filepath = getfilepath (); // path of the current folder <br/> char filename [max_path]; // used to cache various file names <br/> // obtain the path of the executable file, add the boot file name <br/> strcat (strcpy (filename, filepath), "boot. C "); <br/> printf (" boot file name: % S/T/N ", Filename); <br/> char * bootbuffer; // Save the buffer of the Bootstrap program <br/> // compile the Bootstrap program, put the result in bootbuffer <br/> int bootlength = yc_compilecpp (& bootbuffer, filename,); <br/> If (bootlength <= 0) <br/>{< br/> printf ("file: % S contains some errors/N", filename); <br/> return; <br/>}< br/> printf ("file: % s compiled successfully, size: % d bytes. /N ", filename, bootlength); </P> <p> // place the Bootstrap program in the Image Buffer <br/> memcpy (imgbuffer, bootbuffer, bootlength ); <br/> free (bootbuffer); <br/> // fill h-01ffh is the fat boot information. The ending length of 55aa is 200 h (512) byte [0th sectors] <br/> imgbuffer [510] = 0x55; <br/> imgbuffer [511] = 0xaa; // mark the boot end of a floppy disk </P> <p> // create an operating system image boot. IMG <br/> // obtain the path of the executable file, and add the image name <br/> strcat (strcpy (filename, filepath), "boot. IMG "); <br/> If (yc_writefile (filename, imgbuffer, fdisk_siz E )! = Fdisk_size) <br/>{< br/> printf ("error occurred during file write: % S. /R/N ", filename); <br/> return; <br/>}< br/> Delete imgbuffer; <br/> printf ("/n % s created successfully. /N ", filename); <br/> // generate the operating system virtual machine configuration file boot. SRC <br/> yc_writefile (strcat (strcpy (filename, filepath), "boot. SRC "), bootsrc, strlen (bootsrc); <br/> // run the VM <br/> yc_winexec (strcat (strcpy (filename, filepath)," bochs.exe "), "-Q-F boot. SRC "); <br/> printf ("/n click Enter to re-compile and run the program! /N "); <br/> getchar (); <br/> goto _ start; <br/>}< br/>


 


Code: boot. c

// File: boot. c <br/> // function: A simple program that displays a string in the boot sector <br/> // run: run.exe automatically compiles boot. c. generate an IMG and call bochs to run this program. <br/> // The following message is displayed: compile the run.c file with yc09and generate the run.exe Program <br/> // and then modify the boot.c code. Run run.exe directly to view the effect. <br/> # define ycbit 16 // notify the compiler, compile the program in 16-bit format <br/> # define ycorg 7c00h // tell the compiler, load the program at 7c00 <br/> # define msglngth 12 // String Length <br/> char MSG [] = "Hello, Miao! "; <Br/> ASM void main () <br/>{< br/> mov ax, CS <br/> mov ds, ax <br/> mov es, ax <br/>; clear screen <br/> mov ah, 06 h; screen initialization or upstreaming <br/> mov Al, 00 h; Ah = 6, al = 0 h <br/> mov BX, 01111 h; set the background color to Blue <br/> mov CX, 0; start in the upper left corner: (0, 0) <br/> mov DL, 4fh; to column x <br/> mov DH, 1fh; to row x <br/> int 10 h; display interrupt <br/>; display string <br/> mov ax, & MSG <br/> mov bp, ax; es: BP = string address <br/> mov ah, 13 h; Ah: 13 display string <br/> mov Al, 1 h; Ah = 13, Al = 01 H <br/> mov BX, 0014 h; the page number is 0 (bH = 0) Blue-bottom red letter (BL = 14 h) <br/> mov CX, msglngth; Cx = String Length <br/> mov DL, 00 h; start column <br/> mov DH, 00 h; Start row <br/> int 10 h; display interrupt <br/>}< br/>

 

If you want to experiment with the above Code, please note:

1. Download and install yc09 from Yang Xiaobing's blog. It takes about one minute to install yc09.

2. Copy the preceding run. C and boot. C code to the folder used for an experiment.

3. In the yc09/examplefolder find bochs.exe, BIOS-bochs-Latest, VGABIOS-elpin-2.40, x11-pc-us.map four files copy to the test folder.

4. Compile and Run. C using yc09

 

PS1: The yc09/example folder contains the example of the tinix operating system. If you are interested, you can study it directly. There are also a lot of interesting open-source program code as an example of yc09, which is worth looking.

PS2 ......


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.