Orange's an operating system implementation study (1)

Tools to prepare and guide the preparation of the program

Orange's an operating system implementation study (1)

E-book edition (PDF) to write himself operating system

1.Virtual PC Installation

Virtual PC says the original book has been downloaded a bad download, after several days of exploration, and finally found a compliant version.

Virtual PC 5.0

2.DOS 6.3 Installation

(1) Install DOS

Come here next msdos6.22 mirror.

And then start Virtual PC, create a new PC, memory 32m, hard 50m, OK start!

Menu -> floppy -> load the image selection 622c.img

OK, boot into the dos. And then the disk.

Execute command: fdisk Enter in the end all the way (note: in 2007 needs 5.2 version does not require).

Then format the c drive format C: / s

Then transfer the system files sys C:

Floppy disk and then copy the file: copy a: \ *.* c: \ (careful not to overwrite existing files c drive)

OK. Menu -> floppy -> release the mirror "622c.img"

Restart, so you enter the dos environment.

(2) installation shared folder module.

Reboot into dos, the menu -> floppy -> Choose dos add-on modules loading image (in the installation directory of Virtual PC \ Virtual Machine Additions \ DOS Virtual Machine Additions.vfd) is loaded, switch to the floppy disk

Execute the command: c: \> a:

Execute the command: a: \> dosadd

Successful and then you can see the. Oh.

Then is to reboot into dos system, Menu -> Edit -> Properties to set the shared folder you can then add and add a, set to letter Y:

Then execute the command Y:

You can see the shared files.

VMware requires only the first step, you can set up a shared folder. Without having to install additional modules!

3 boot process

The computer is powered, to complete a series of detection by the BIOS, and if all devices are working properly, then start testing the next BIOS boot device; computer will first boot device in the sector offset of 510 places to find a magic number (Magic Number) 0xAA55, if not the magic number is not a boot device. Each boot device in the first sector offset 510byte Department will have this number, then a computer if you have multiple boot device (floppy disk, hard disk, CD-ROM, etc.), the PC must follow a certain order of boot devices check these ; this sequence is set in the BIOS boot sequence. We will start the first 512 bytes of the device called the MBR (full name is the Master Boot Record); MBR refers to the hard drive is usually the first sector, BIOS is responsible for finding the first boot device's MBR into memory 0x7C00 start a space, then passes control to this MBR; then the CPU begins executing the contents of the MBR; because only 512 bytes of MBR, such programs are often short of a real operating system used to import procedures. (Reference MBR code analysis )

4.BootSector programming guide

4.1 prepare a floppy disk

Prepare a boot floppy disk, floppy disk, but now something is antique, and looking for a floppy disk is not an easy task, and the floppy disk is easy to read and write fast enough bad, we do a virtual floppy disk. Specific steps are as follows:

Virtual PC -> File -> Virtual Disk Wizard ->

Orange's an operating system implementation study (1)

Orange's an operating system implementation study (1)

Orange's an operating system implementation study (1)

Next-> select the storage location and file name, select size 1.4M -> Finish.

Writing Disk 4.2 a tool to prepare

We are to write a floppy boot process, but how we write the program into the floppy disk (virtual floppy) it? If you do not put through to how we test our program is able to run it? In fact, we use a binary viewer to open the floppy disk image that already exists, the beginning of the file is 512 bytes Pirates magnetic floppy disk 0 sector 0, cylinder 1, so we can image file read and write absolute sector, will guide our program included 512 bytes of image. CD-ROM in the original book provides a tool, but manually adding each time is too much trouble, (in fact if you write, then hands you have to estimate several ten times the light debugging will be compiled manually every time the bin file into a floppy disk, then , I guess you mad), so we will transform his code automatically compiled, the compiler automatically write the floppy disk.

After transformation, look at the code to write the floppy disk (VS2005):

Compiled executable: WriteFlopy

/ WriteFlopy.cpp :   Define the entry point for the console application  .

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
    //   Read the following documents   ------------------------------------------------
    unsigned char uchBootData[512];
    char szFile[512] = "Boot.bin";    // buffer for file name
    HANDLE hf = ::CreateFile(szFile,
    DWORD dwFileSizeHigh = 0;
    DWORD dwFilesize = ::GetFileSize(hf, &dwFileSizeHigh);
    if (dwFilesize <= 0) {
        ::MessageBox(0, "  File error  !", "Error", MB_OK);
        return 0;
    DWORD dwRead = 0;
    if (!ReadFile(hf, uchBootData, dwFilesize, &dwRead, NULL)) {
        int iErr;
        char szError[128];
        iErr = GetLastError();
        ::sprintf(szError, "  File read error  !\n  Error code  : %d", iErr);
        ::MessageBox(0, szError, "Error", MB_OK);
        return 0;
    //   The following written documents   --------------------------------------------------------
    ::strcpy(szFile, "Tinix.img");
    HANDLE hfImage = ::CreateFile(szFile,
    ::SetFilePointer(hfImage, 0, 0, FILE_BEGIN);
    DWORD dwBytesWritten;   
    if (!::WriteFile(hfImage,
        NULL) )
        int err;
        char error[10];
        err=GetLastError ();
        //itoa (err, error, 10);
        //MessageBox (0, "Writing sectors ...Failed  ");
        return 0;

    ::MessageBox(0, "  Success  !", "Floppy writer", MB_OK);
    return 0;

4.3 Boot process of project construction

boot.asm Bootloader source code
WirteFlopy.exe The compiled file included in the virtual floppy TINIX.IMG boot.bin
TINIX.IMG Virtual Floppy
include For placing some of the constants and FAT12 file format defined folders
compile.bat Oh, with automatic compile and will be compiled with WriteFlopy.exe written into the virtual floppy file boot.bin

compile.bat reads as follows:

nasm -I .\include\ boot.asm -o boot.bin
4.4 program written boot.asm

The program's main function is to format the floppy disk in the FAT12 file find Loder.bin file and loaded into memory, then passes control to Loader.bin. This program and the FAT12 file format test program similar in function and format of the FAT12 also can refer to the FAT12 file format. After the first given to achieve the following code:

;**boot.asm   Floppy boot partition  
org 07c00h                              ;BIOS  Will be loaded into the boot Boot Sector   
                                        ;  Address  0000:7c00   And implementation  

;==  Macro  ===========================================================================
BaseOfStack     equ     07c00h          ;boot  State of the stack base address  (  Note that the stack is down  
                                        ;  Growth  )
;Loader address
BaseOfLoader            equ      09000h ;LOADER.BIN   Is loaded into the segment address location ----  
OffsetOfLoader          equ       0100h ;LOADER.BIN   Is loaded into the offset to the location ----  
                                        ;  Site   (  The size of 63K  )
SectorNoOfRootDirectory equ     19      ;Root Directory   The first sector number  = 
                                        ;BPB_RsvdSecCnt + (BPB_NumFATs * FATSz)
RootDirSectors          equ     14      ;   Root space  : RootDirSectors = 
                                        ;((BPB_RootEntCnt * 32) + (BPB_BytsPerSec
                                        ;   -   1)) / BPB_BytsPerSec;   However, if the public in accordance with this  
                                        ;  Type the code is too long  
jmp short LABEL_START
nop                                     ;  Essential  
;**  The following is the definition of FAT12 disk head  ************
BS_OEMName      DB 'SongYang'           ;OEM String,   Must be 8 bytes  

BSB_BytePerSec  DW 512                  ;  Bytes per sector  
BSP_SecPerClus  Db 1                    ;  How many sectors per cluster  
BPB_RsvdSecCnt  DW 1                    ;Boot   Record number of sectors occupied by  
BSP_NumFats     DB 2                    ;  Total number of FAT tables  
BSP_RootEntCnt  DW 224                  ;  Root of the maximum number of files  
BSP_TotSec16    DW 2880                 ;  The total number of logical sector  
BPB_Media       DB 0xF0                 ;  Media descriptor  
BPB_FATSz16     DW 9                    ;  Number of sectors per FAT  
BPB_SecPerTrk   DW 18                   ;  Number of sectors per track  
BPB_NumHeads    DW 2                    ;  Number of heads  (  Sides  )
BPB_HiddSec     DD 0                    ;  The number of hidden sectors  
BPB_TotSec32    DD 0                    ;  If wTotalSectorCount is  0  This value is recorded by the  
                                        ;  Sectors  

BS_DrvNum       DB 0                    ;  Interrupt 13 drive letters  
BS_Reserved1    DB 0                    ;  Unused  
BS_BootSig      DB 29h                  ;  Extended Boot Mark   (29h)
BS_VolID        DD 0                    ;  Volume Serial Number  
BS_Volab        DB 'Tinix0.01  '        ;  Label must be   11   Bytes  
BS_FileSysType  DB 'FAT12   '           ;  File system type must be   8  Bytes  

;**  Initialize register  *************************
        mov ax, cs
        mov ds, ax
        mov es, ax
        mov ss, ax
        mov sp, BaseOfStack
;**  Clear screen  *********************************
        mov     ax, 0600h               ; AH = 6,  AL = 0h
        mov     bx, 0700h               ;   White on Black  (BL = 07h)
        mov     cx, 0                   ;   The upper left corner  : (0, 0)
        mov     dx, 0184fh              ;   The lower right corner  : (80, 50)
        int     10h                     ; int 10h
;**  Show prompt string  ***********************
        mov dh, 0
        call DispStr
;**  Floppy Reset  *****************************
        xor ah, ah      ;   ┓  
        xor dl, dl      ;   ┣ floppy reset  
        int 13h         ;   ┛  
;**  Looking for a floppy disk  Loader.bin(FAT12)********
        ;**  Will have read the entire root  es:bx  Office  
        mov     ax, BaseOfLoader
        mov     es, ax                  ; es <- BaseOfLoader
        mov     bx, OffsetOfLoader      ; bx <- OffsetOfLoader         Then  , es:bx = BaseOfLoader:OffsetOfLoader
        mov     ax, SectorNoOfRootDirectory     ; ax <- Root Directory   A number of Sector  
        mov     cl, RootDirSectors      ;  The root directory are read out  
        call    ReadSector
        ;mov    ax, BaseOfLoader
        ;mov    es, ax
        ;mov    ax, cs
        ;mov    ds, ax

        mov     si, LoaderFileName      ; ds:si -> "LOADER  BIN"
        mov     di, OffsetOfLoader      ; es:di -> BaseOfLoader:0100 = BaseOfLoader*10h+100
        cld                             ;  Set direction flag  
        mov cx, [BSP_RootEntCnt]
        mov [RootDirectItemNum], cx     ;  Root of the number of  

        cmp word [RootDirectItemNum], 0
        jz NOLOADER
        dec word [RootDirectItemNum]
        mov     cx, 11
        cmp     cx, 0
        jz      LABEL_FILENAME_FOUND    ;   If you compare the 11 characters are the same  ,   That found  
        dec     cx
        lodsb                           ; ds:si -> al
        cmp     al, byte [es:di]
        jz      LABEL_GO_ON
        jmp     LABEL_DIFFERENT         ;   As long as the same character is found to be not that the DirectoryEntry is not  

        inc di
        and di, 0FFE0h
        add di, 020h
        and si, LoaderFileName
        mov dh, 2
        call DispStr
        jmp $
        and di, 0FFE0h
        add di, 01Ah            ; di ->   First   Sector(  Offset  ) 
        mov cx, word [es:di]
        push cx                 ;cx  Loader is placed inside the first sector of good  (  Data area starting at 2  )
;**  Read Fat Table   
        mov ax, BaseOfLoader
        sub ax, 140h            ;  5k memory allocation  (512*10/1024)
        mov es, ax
        mov bx, 0               ;  Read the Fat  es:bx
        mov ax, [BPB_RsvdSecCnt];Fat  Start sector   1
        mov cl, [BPB_FATSz16]   
        call ReadSector
;**  Loader read into memory  *******
        mov ax, BaseOfLoader
        mov es, ax
        mov bx, OffsetOfLoader
        pop ax
        push    ax                      ;   ┓  
        push    bx                      ;   ┃  
        mov     ah, 0Eh                 ;   ┃ read a sector on each   "Booting  "   A point behind the play, the formation of such an effect  :
        mov     al, '.'                 ;   ┃  
        mov     bl, 0Fh                 ;   ┃   Booting ......
        int     10h                     ;   ┃  
        pop     bx                      ;   ┃  
        pop     ax                      ;   ┛  

        push    ax              ;  Save  ax
        add     ax, 17 + 14
        mov     cl, 1   ;  A sector  
        call    ReadSector
        pop     ax              ;  Reply  ax
        call    GetFATEntry;  Get the next number        
        cmp     ax, 0FFFh
        jz      LABEL_FILE_LOADED
        add     bx, [BSB_BytePerSec]

        mov     dh, 1                   ; "Ready."
        call    DispStr                 ;   Display string  

; *****************************************************************************************************
        jmp     BaseOfLoader:OffsetOfLoader     ;  This is an officially jump into the already loaded into memory at the beginning of LOADER.BIN  
                                                ;   Started LOADER.BIN code  
                                                ; Boot Sector   The mission ends  
; *****************************************************************************************************

;  String  
LoaderFileName          db      "LOADER  BIN", 0        ; LOADER.BIN   The file name  
RootDirectItemNum       DW       0
;   To simplify the code, following the length of each string are   MessageLength
MessageLength           equ     9
BootMessage:            db      "Booting  "             ; 7  Byte  
Message1                db      "Ready.   "             ; 9  Byte  
Message2                db      "No LOADER"             ; 9  Byte  

;   Function name  : DispStr
;   Effect  :
;         Display a string, the beginning of the function   dh   Serial number in the string is  (0-based)
        push ax
        push bx
        push cx
        push dx
        push es
        mov     ax, MessageLength
        mul     dh
        add     ax, BootMessage
        mov     bp, ax                  ;   ┓  
        mov     ax, ds                  ;   ┣   ES:BP =   String address  
        mov     es, ax                  ;   ┛  
        mov     cx, MessageLength       ; CX =   String length  
        mov     ax, 01301h              ; AH = 13,  AL = 01h
        mov     bx, 0006h               ;   Page No.  0(BH = 0)   White on Black  (BL = 07h)
        mov     dl, 0
        int     10h                     ; int 10h
        pop es
        pop dx
        pop cx
        pop bx
        pop ax
;   Function name  : ReadSector
;   Effect  :
;         Ax from the first two   Sector   Will begin   cl   A read Sector   es:bx   In  
        ; -----------------------------------------------------------------------
        ;   How to find the number of sectors in the disk sector location   (  Sector No.   ->   Cylinder number, starting sector  ,   Head number  )
        ; -----------------------------------------------------------------------
        ;   Sector number is set   x
        ;                             ┌ cylinder number   = y >> 1
        ;       x             ┌ business   y   ┤ 
        ; -------------- =>   ┤ └     Head number   = y & 1
        ;    Sectors per track │  
        ;                     └ I   z =>   Start Sector Number   = z + 1
        push    bp
        mov     bp, sp
        sub     esp, 2                  ;   Stocked with two-byte stack area to save the number of sectors to read  : byte [bp-2]

        mov     byte [bp-2], cl
        push    bx                      ;   Save   bx
        mov     bl, [BPB_SecPerTrk]     ; bl:   Divisor  
        div     bl                      ; y   In al the  , z   In the ah  
        inc     ah                      ; z ++
        mov     cl, ah                  ; cl <-   Start Sector Number  
        mov     dh, al                  ; dh <- y
        shr     al, 1                   ; y >> 1 (  Is y / BPB_NumHeads, where  BPB_NumHeads=2)
        mov     ch, al                  ; ch <-   Cylinder number  
        and     dh, 1                   ; dh & 1 =   Head number  
        pop     bx                      ;   Resume   bx
        ;   At this point, "cylinder number  ,   Starting sector, head number  "   All been   ^^^^^^^^^^^^^^^^^^^^^^^^
        mov     dl, [BS_DrvNum]         ;   Drive letter   (0   A disk that  )
        mov     ah, 2                   ;   Read  
        mov     al, byte [bp-2]         ;   Al sectors read  
        int     13h
        jc      .GoOnReading            ;   If the reading error will be set to CF   1,   Then you keep reading, until it's right  

        add     esp, 2
        pop     bp

;   Function name  : GetFATEntry
;   Effect  : ax
        push bp
        mov bp, sp
        sub esp, 2
        mov word [bp-2], 0
        push es 
        push bx
        push dx
        mov bx, 3
        mul bx                          ;ax = ax * 3
        mov bx, 2
        div bx                          ;  Business in the remainder of the ax  dx
        cmp dx, 0
        jz LABEL_EVEN                   ;  Even  
        mov word [bp-2], 1              ;  Odd number  
        mov bx, ax
        mov ax, BaseOfLoader
        sub ax, 140h
        mov es, ax
        mov word ax, [es:bx]
        cmp word [bp-2], 0
        jz LABEL_EVEN_2
        shr ax, 4
        and ax, 0FFFh                   ;  Low twelve  
        pop dx
        pop bx
        pop es
        add esp, 2
        pop bp
times 510-($-$$) db 0                   ;  Fill the remaining space, the generated code is exactly  512B
dw      0xaa55                          ;  End tag     
compiled files: boot.bin
标签: system implementation, e book, command c, c drive, boot process, system menu, number magic, magic number, format c, dos system, dos environment, boot device, loading image, bios boot, sys c, image selection, pc memory, virtual pc 5, implementation study, compliant version
分类: OS
时间: 2011-04-13


  1. [Reprint] to construct a 51 single-chip real-time operating system

    [Reproduced] Date of xgywinner 2009-3-18 11:53:00 Stu ...
  2. Simple operating system (orange os sample).

    In Yuan's "write himself operating system" tells us how to write an operating system, of course, wri ...
  3. Oracle performance tuning study notes (r) --- Operating system tuning

    Different system operating system tuning single-processor system architecture: common multi-processing system ...
  4. Linux operating system upstart MeeGo

    March 13, 2010 In the just-concluded World Mobile Congress 2010 in Barcelona, Linux operating system family of ...
  5. Information seminars to prepare the operating system

    Computer industry, hacker culture has been a huge shock. When the software license and copyright to become ind ...
  6. Linux operating system in the seven super-powerful weapons

    Linux is a free to use and free dissemination of the UNIX-like operating systems, mainly for the Intel x86 ser ...
  7. Linux operating system using the RPM command parameters Xiangjie

    In the Linux operating system, a system package, its function is similar to Windows inside the "Add / Rem ...
  8. Linux System Programming Study Notes (1) File I / O

    File I / O 1, open the file int open(const char *name, int flags); int open(const char *name,int flags, mode_t ...
  9. Linux System Programming Study Notes (9) signal management 2

    Signal 2: 1, the signal set: Signal set and its operation that we often encounter, such as setting some of the ...
  10. How to install the Windows operating system, Linux operating system installed on your computer

    One had to know one thing Most individuals believe that the Chinese used to buy back in when the computer has ...
  11. Windows operating system so that we develop what foul problems

    1, crazy refresh <br /> believe me many people as before, one into the Windows desktop, start crazy refr ...
  12. [On] Windows operating system so that we develop what foul problems

    1, crazy refresh <br /> believe me many people as before, one into the Windows desktop, start crazy refr ...
  13. The failure of the operating system installation summary

    Because it does not make sense because the recent period has been Daoteng operating system, first win7 downloa ...
  14. Linux System Programming Study Notes (13) thread 2

    Thread 2 Thread 1 thread and we introduced the thread synchronization, this section will learn the details of ...
  15. Python modules commonly used types of system description and associated with the operating system calls and operations

    Built-in module (can not import directly) are commonly used built-in functions: help(obj) Online help, obj, bu ...
  16. History of Java-based operating system

    Reprinted from: ********************************* ...
  17. Explore the concurrent programming (1 )------ operating system articles (change)

    Original Address: here In multi-threaded, multi-processor or even a distributed programming environment, time, ...
  18. linux operating system to achieve pure manually install the MySQL source

    linux operating system to achieve pure manually install the MySQL source This paper is to introduce a more eff ...
  19. UNIX operating system commands

    UNIX operating system commands -------------------------------------------------- ---------------------------- ...