Written in front of the words:
Have always wanted to write a simple operating system, unfortunately the level is limited, in Linux to compile and install a program for several days did not succeed, let alone to study the Linux kernel code. Later bought Minix books, read for half a month, also do not know how to start writing their first line of code ...
Accidentally found Source wrote "Do-it-Yourself Write operating system", finally let me really set foot on the first step to write the operating system. Here to thank source greatly for the vast number of learning to write the operating system for beginners to make a great contribution.
Unfortunately, because the second chapter of the book to configure the experimental environment is not fully passed, in the fifth chapter on the Jam. After that, because of the tight computer resources, the experimental virtual machines were deleted. The way of learning is silent.
Recently, in the stroll csdn, undoubtedly found Yang Xiaobing big blog and the compiler compiled YC09 and above as one of the Tinix operating system, just know that the original test operating system code can be so simple--just a yc09 compiler and virtual machine bochs on it, No more effort is needed to configure the experimental environment to install Virtual PCs, VMware, and DOS and Linux, to use NASM and GCC, and to start DOS or Linux in the virtual machine again and again. So the enthusiasm for writing the operating system was rekindled.
Experimental environment: Windows XP
Compiler: YC09
To run the Debugging tools: Bochs
Experimental Code Structure Description:
All experiments are made up of RUN.C and the actual experimental code of the operating system. The function of RUN.C is to generate Run.exe program, automatically compile experiment code by Run.exe program, generate IMG and call run Bochs. As long as the compilation of good run.c, after the modification of the experimental code, directly run Run.exe can. I specifically set run.c for an infinite loop. So it becomes very easy to modify the test experiment code:
(1) Running Run.exe
(2) View the virtual machine running effect
(3) Turn off the virtual machine
(4) Modify the experimental code
(5) In the Run.exe console click Return (First round, two times after the Click Return)
(6) Return to step two ...
Here is the experimental code:
A simple program written in the boot sector--Displays a string on the screen.
Although there are countless versions of the online example, here, I still have the shameless to share with you.
Code:run.c
Files: run.c//function: Compile the operating system code and create IMG, generate Bochs configuration file, run Bochs//run: Please compile the run with yc09 compiler. Author: Miao//Time: 2010-1-12//virtual machine set char *bootsrc = "megs:32/n" "Romimage:file=bios-bochs-latest, address=0xf0000/n" "VGA romimage:vgabios-elpin-2.40/n "" "Floppya:1_44=boot.img, status=inserted/n" "Boot:a/N" "log:log.out/n" "Mouse:enabl" ed=0/n "" "Keyboard_mapping:enabled=1, map=x11-pc-us.map/n"; Gets the current folder path char * GetFilePath () {static char filepath[max_path];//file path, global valid//Gets the complete path of the executable file int filenamelength = Getmodul Efilename (Null,filepath,max_path); if (!filenamelength) {printf (get current Run program file path failed!/n); return FilePath}//Remover name: Run.exe, only folder path for (int i = Filenameleng Th;filepath[i]!= '//'; i--filepath[i] = '/0 '; return filePath; void Main () {_start: #define FDISK_SIZE 1474560//Mirror size: 1.4mb byte *imgbuffer = new byte[fdisk_size];//Mirrored buffer char * fil Epath = GetFilePath (); Current folder path Char Filename[max_path]; Used to cache individual file names//Get executable paths, plus boot file name strcat (strcpy (Filename,filepath), "boot.c"); printf ("Boot filename:%s/t/n", fileName); Char *bootbuffer; Save the bootstrapper buffer//compile bootstrapper, and the result is placed in Bootbuffer int bootlength = Yc_compilecpp (&bootbuffer,filename,0,0); if (bootlength <= 0) {printf (file:%s with some errors/n, fileName); return} printf (file:%s compiled successfully, size:%d bytes. /n ", filename,bootlength); Place the bootstrapper in the mirrored buffer memcpy (imgbuffer,bootbuffer,bootlength); Free (bootbuffer); 0000H-01FFH for FAT boot information with a 55AA flag end length is 200H (512) byte [No. 0 sector] imgbuffer[510] = 0x55; IMGBUFFER[511] = 0xaa;//tag floppy boot end//create OS mirror boot.img//Get executable path, plus mirror name strcat (strcpy (Filename,filepath), "boot.img") ; An error occurred during the Yc_writefile (filename,imgbuffer,fdisk_size)!= fdisk_size) {printf (write:%s file). /r/n ", fileName); Return } Delete Imgbuffer; printf ("/n%s created successfully. /n ", fileName); Build operating system virtual machine configuration file Boot.src yc_writefile (strcat (strcpy (Filename,filepath), "Boot.src"), Bootsrc,strlen (BOOTSRC)); Run virtual machine yc_winexec (strcat (strcpy (Filename,filepath), "Bochs.exe"), "-q-f boot.src"); printf ("N + + click Return) to recompile the run." /n/n/n "); GetChar (); Goto _start; }
Code:boot.c
File: boot.c//function: A simple program//run that displays a string in the boot sector: Run.exe automatically compiles boot.c and generates IMG and invokes Bochs to run this program// Hint: Please compile run.c file with yc09 First, generate Run.exe program//Then modify BOOT.C code, can run directly run.exe view effect #define YCBIT 16//Tell the compiler to compile the program in 16-bit format #define Ycorg 7c00h//Tell the compiler to load the program at 7C00 Place #define MSGLNGTH 12//String length char msg[] = "Hello, miao!"; ASM Void Main () {MOV ax, CS mov ds, ax mov es, ax; clear screen mov ah, 06h; screen initialization or roll up of mov aL, 00h; AH = 6, AL = 0h mov bx, 01111h; Set the background color to blue mov cx, 0; The upper left corner begins: (0, 0) mov dl, 4FH; To column x mov dh, 1fh; to x line int 10h; Show interrupts; Display string mov ax,&msg mov bp,ax es:bp= string address mov ah, 13h; Ah:13 display string mov al, 1h; AH =, AL = 01h mov bx, 0014h; Page number is 0 (BH = 0) Blue bottom red word (BL = 14h) mov cx, msglngth; CX = string length mov dl, 00h; Start out with mov dh, 00h; start line int 10h; Show Interrupts}
Friends who want to experiment with the above code, please note:
1. To Yang Xiaobing large blog Download installation yc09, installation only a minute or so.
2. Copy the above run.c and BOOT.C code into an experimental folder.
3. Locate the Bochs.exe, Bios-bochs-latest, vgabios-elpin-2.40, x11-pc-us.map four files in the Yc09/example folder and copy them to the test folder.
4. Use yc09 to compile and run RUN.C
There are examples of Tinix operating systems in the Ps1:yc09/example folder, and interested friends can study it directly. There are also a number of interesting Open-source program codes as examples of yc09 that are worth looking at.
PS2: Recently visited Xinhua bookstore, found source big "do-it-Yourself writing operating system" out of the second edition, poor my first edition has not touched the heat ...