Linux Kernel series-HelloWorld and linuxhelloworld for Operating System Development

Source: Internet
Author: User

Linux Kernel series-HelloWorld and linuxhelloworld for Operating System Development

Org 07c00h; a pseudo command that tells the compiler program to be loaded to mov ax, csmov ds, axmov es, axcall DispStr at 7c00; call the display string routine jmp $; infinite loop DispStr: mov ax, bootMessagemov bp, ax; ES: BP = string address mov cx, 16; CX = string lengths mov ax, 01301 h; AH = 13, AL = 01 hmov bx, 000ch; page number is 0 (BH = 0) black background red letter (BL = 0Ch, highlighted) mov dl, 0int 10 h; 10 h interrupted ret; pop IPBootMessage: db "Hello, OS world! "Times 510-($-$) db 0; fill in the remaining space to make the generated binary code exactly 512 bytes dw0xaa55; End mark

The above is a very simple operating system helloworld source code. After bochs are loaded, for example (bochs is a virtual machine and simulated operating system loading), the red Hello, OS world, actually, the system is loaded like this:

Source code parsing:

1. org 07c00h, dw 0xaa55, and times 510-($-$) db 0

When the computer is powered on, it will first perform a self-check (POST), and then find the boot disk. If you choose to start from a floppy disk, the computer will check the disk's 0-sided, 0-track, and 1-sector, if it is found to end with 0xaa55, the BIOS considers it as a boot sector. In addition to ending with 0xaa55, a correct boot sector should also contain a 512-byte Execution Code. Once the BIOS detects the boot sector, it will load the 512 bytes of content to the memory address 0000: 7c00, and then jump to 0000: 7c00 to give control to this boot code.

Org 07c00h tells the compiler that this code is to be loaded to this address. Therefore, the BootMessage offset address starts with 07c00h. BootMessage is actually an address, not a relative volume

2. mov ax, cs, mov ds, ax,Mov es, ax

Point ds and es to the cs segment, that is, 07c00h. Because the program will be loaded to 07c00h, CS points to 07c00h during this step.

3. call DispStr

Call the function that displays strings. In NASM, any label or variable name that is not included in [] is considered as an address. If it is an access tag, you must use []. So mov ax and BootMessage will put "Hello, OS world! "The first address of this string is transmitted to the Register ax. Then mov bp and ax are assigned to bp. If the org command is not written, the BootMessage is the offset address loaded to address 0 relative to the program during compilation, however, here is the offset address relative to 07c00h as the base address, so here it shows the role of org.

4. int 10 h

A 10 h interrupt is a service program provided by the BIOS to the display and screen. When using the int 10 h service program, you must first specify the ah register.

Function 13 h:

Function Description: Display strings in Teletype mode.
Entry parameter: AH = 13 H
BH = page number
BL = attribute (if AL = 00H or 01 H)
CX = display String Length
(DH, DL) = coordinates (rows, columns)

ES: BP = display string address
AL = display output mode
0 -- the string contains only the display characters, and its display attribute is in BL. The cursor position remains unchanged after display
1 -- the string contains only the display characters, and its display attribute is in BL. The cursor position changes after the display
2 -- the string contains the display characters and display attributes. The cursor position remains unchanged after display
3 -- the string contains the display characters and display attributes. The cursor position changes after the display
Exit parameter: None

5. $ and $

$-$ Indicates the relative distance from the beginning of the program. Times 510-($-$) db 0 indicates that the byte 0 is repeated for 510-($-$) times, that is, the remaining space is continuously filled with 0, until the program has 510 bytes. In this way, the end sign 0xaa55 occupies 2 bytes, Which is exactly 512 bytes.

Related Article

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.