"Implementation of an operating system" reading notes--Chapter one--the smallest "operating system"

Source: Internet
Author: User
Tags base64 numeric json md5 regular expression relative

One, the simplest "operating system"

The simplest "operating system" is the simplest boot sector (boot Sector). Although it does not have any functionality, it can run directly on bare metal without relying on other software. A boot sector is 512 bytes, and the sector is identified with 0xaa55 as the end. Here is the most simple boot sector.

org 07c00h                   		; Tell compiler program to load to 07C00 at
       mov ax, CS
       mov ds, ax
       mov es, ax
       call Dispstr                    	; invoke display string routines
       jmp $              		; infinite loops
DISPSTR:
       mov ax, bootmessage
       mov bp, ax                    	; es:bp = string address
       mov cx,                    	cx = string length
       mov ax, 01301h
  ; Ah =, Al = 01h
       mov bx, 000Ch              	; page number 0 (bh = 0) Black Bottom red (bl = 0Ch, highlight)
       mov dl, 0
       int 10h                          ; 10h interrupt 
  
   ret
bootmessage:  db "Hello,os world!"
Times 510-($-$$)   db   0            	; Fills the remaining space so that the generated binary code is exactly 512 bytes
DW 0xaa55                               ; end Flag
  

1, org 07c00h

org pseudo-directive: Org + numeric expression

Among them, org is the operation code, can not be omitted. The numeric expression gives the offset address value, which is the instruction or data after the ORG statement as the starting offset address with the value given by the numeric expression. The numeric expression must be a positive integer that can be computed, and the range of values is between 0~65535.

The org pseudo-directive is used to indicate the offset of the starting address after which the program segment or block of data is stored. The assembler compiles the value of the expression in the statement as the starting address, continuously storing the program and data after the ORG statement until a new org instruction appears. If you omit the org statement, it is stored continuously from the beginning address of this paragraph.

In most cases, you do not need to set the location pointer with the ORG statement. Since the segment definition statement is the starting point of the segment, it has an offset address of 0000H, and the position pointer automatically adds 1 for each byte allocated thereafter, so each instruction has a definite offset address. The Org statement needs to be arranged only if the program requires that the position pointer be changed. usually org statements can appear anywhere in the program.

Org Example:

In the data segment, define the following variables in turn, because the ORG statement is not used at this time, the offset address of the variable word1 is 0.
Word1 DW 1234h
Byte1 DB 56h
Word2 DW ABCDH
The storage location of the Word1,byte1,word2 in the data segment is shown in the following figure (small-ended storage, single-byte alignment).

The following variable is defined in the data segment in turn, and the variable word1 has an offset address of 1 because the ORG statement is used at this time.
ORG 0001h
Word1 DW 1234h
Byte1 DB 56h
Word2 DW ABCDH
The storage location of the Word1,byte1,word2 in the data segment is shown in the following figure (small-ended storage, single-byte alignment).

We know that the compiler itself in the assembly when the address calculation of the instruction is a relative address, and for the boot sector, is the absolute address execution, then the relative address compiled by the execution code will be converted to an absolute address. In general, the "boot sector that is actually starting to execute" will be mounted to 07c00h.

Since the compiler's address at compile time starts with a relative calculation at 0000h starting with the first line, and we're going to write the "Boot Sector" program, so we're going to load the following code at address 07c00h, so we need org 07c00h, through that pseudo-directive, Load code and data into an 07c00h address.

2. JMP $

$ is called the current position counter

In the assembler process of compiling the source program, the address counter is used to guarantee the address of the instruction currently being assembled. The address counter value can be expressed as "$", and assembly language allows the user to refer directly to the current value of the address counter using "$", so org $+5 can represent the current value of the address counter by skipping the 5-byte storage unit from the current address, or directly using "$" in directives and pseudo-directives. So jmp $ enters an infinite loop.

3, int 10h

int 10H Interrupt

int 10H is a service program provided by the BIOS to the screen and monitor. When using the INT 10H Interrupt Service program, first specify that the AH register is one of the following table numbers, which indicates the function to invoke, and the details of other registers, refer to the text after the table, and then call int 10H when everything is set.

Here we only explain in detail the interruption of number 10th in this program. Because of ah=13, it calls the function numbered 13: the display string. where es:bp= string address, cx= string length, ah=13, when al=01h, the cursor will follow the display to move. BH is the page number, bh=0 indicates that the page number is 0,bl=0ch, which indicates the attribute, that is, the black-bottom red character highlighting. Therefore, before invoking the number 10th interrupt, it is simply initializing each register.

For additional information about number 10th interrupts, please refer to the http://blog.csdn.net/yes_life/article/details/6778834

4. Times 510-($-$$) DB 0

Times : Repeating instructions or data

The Times prefix causes the instruction to be assembled multiple time. Where $$ represents the address of the program's initial code snippet, the instruction will be executed 510-($-$$) times. That is, fill the remaining space with the zeros, reaching 510 bytes.

second, how to run the "operating system"

1. Environment Preparation

(1) Computer operating system (Windows)
(2) virtual machine (VMware)
(3) Assembly compiler (NASM)
(3) Floppy disk (virtual floppy ramdisknt)
(4) rawrite.exe/book CD-ROM comes with os/tools/floppywriter, which is responsible for writing the program to the floppy disk.

2. Compiling boot.asm

Name the above code file Boot.asm, and then compile the file using the NASM command.
NASM Boot.asm–o Boot.bin

3. Virtual floppy disk a

Click Start to see if there is a virtual floppy disk A in my computer, and if not, restart.

4. Write boot.bin to diskette a

Click Enter. (Note that Boot.bin and Rawrite.ext are in the same file directory.)

5. Create a virtual machine to install VMware, create a virtual machine in VMware, name Tinix, and set the system boot disk to floppy disk a.


Start the system and draw the result graph as:

third, the operating system startup process

Turn on the computer, power-on self-test (POST), look for the boot disk, the system is set to boot from the floppy disk, the computer checks the floppy disk 0 side 0 Tracks 1 sectors, if it is found to end with 0xaa55, then the BIOS will consider it a boot sector The BIOS loads the 512-byte sector content into the memory address 0000:7c00, and jumps to 0000:7c00 to give control over to the boot code. To this end, the computer is no longer controlled by the programs inherent in the BIOS, but is controlled by a portion of the operating system.

The above tools download address http://download.csdn.net/detail/zgh1988/6922887


Recently made a Json format, online timestamp conversion, MD5 encoding, URL encoding, BASE64 codec, regular expression, Linux command Daquan and other functions, welcome everyone to use and join.

Json format, online timestamp conversion, MD5 encoding, URL encoding, BASE64 codec regular expression Linux command encyclopedia

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.