[Reprint] How to write a bootstrapper Hello world

Source: Internet
Author: User

There is a special area in the storage medium (hard disk, floppy disk, CD-ROM) called the Boot area. After the computer starts, theBIOS reads the code in the boot area into memory, and then executes the code. The location and size of the boot area is related to the computer's platform, and for IBM-PC compatible machines, the boot area is located in the first sector of the storage medium, with a size of five bytes. The code in the boot area is called the bootstrapper. The boot area contains a boot program that is identified by a tag within the boot area. For ibm-pc compatible machines, if the last two bytes in the boot area are 0x55,0xaa, the BIOS detector considers the boot area to contain the bootstrapper. The bootloader does not necessarily implement the functionality of the load operation, it can be any program.

Ibm-pcThe specific boot process for the compatible machine is as follows:
1.When you press the Power key of the computer,x86 CPUStart running in real mode at the physical address0xFFFFFFF0(Usually this address is pointed to is located inRomIn theBiosAt the entry point), this command is a jump instruction, which jumps to thebios start-up program. The startup program runs hardware detection and initialization operations.
2. After initializing the required hardware, biosbios by checking if the last two bytes of the boot area are 0x55,0xaa To determine if a bootstrapper is included in the boot area.
3. once bios found a bootable device, It loads the boot area code into the physical address of the memory 0x7c00segment:offset 0000h:7c00h< Span lang= "ZH-CN" and some bios07c0h:0000h bios give control to the bootloader, cpu start the bootloader, boot loader starts loading OS kernel or other.

After installing Windows on a formatted hard disk , the first sector (boot area) of the hard disk contains the MBR boot code, andafter the BIOS loads the MBR code, theMBR Start by checking if there is an active partition (with a boot partition) on the hard disk, and if there is an active partition, load the boot program that is located on that partition (often called VBR, which is related to the operating system), and then execute it. The main function of VBR is to load the operating system's kernel.

Having learned the relevant knowledge, how do we write a bootloader that prints Hello world?

Tools to prepare:
Text editing software, MASM compiler (I'm using VS2008 with ML 9.0), 16-bit link program, VMware, PE (used to format VMware-created virtual disks), Ntexplorer

Specific steps:
1. Open the text editor you are accustomed to using and write the code below. Because the BIOS will load our bootloader to the 7c00h of the memory address, we need the org instruction to set the address value of our code instruction.

;------------------------------------------------------------. 286; CPU type;------------------------------------------------------------. model TINY; Memory of model;------------------------------------------------------------;-----------------------CODE SEGMENT- ----------------------. code org 07c00h; for bootsectormain:jmp short start;    Go to main NOP;---------------------Print a char in the screen------------Printchar PROC mov ah, 0Eh ;    Tell BIOS, we need to print one charater on screen. mov bh, 0h;    Page No. mov bl, 07h; Text attribute 0x07 is Lightgrey font on black background int 10h; Call video interrupt Retprintchar ENDP,-------------------Print a String on the screen-------------printstr procnext_ Character:; Lable to fetch next character from String mov al, [si]; Get a byte from string and storeIn al Register Inc si; Increment si pointer or al, AL; Check if value in AL was zero (end of string) JZ exit_function; If End then return to call Printchar; Else Print the character which is in AL register JMP Next_character; Fetch next character from Stringexit_function:; End label Retprintstr ENDPSTART:CLI; Clear Interrupt flags mov Ax,cs; Setup segment registers mov ds,ax; Make DS correct mov es,ax; Make ES correct mov ss,ax; Make SS correct mov bp,7c00h mov sp,7c00h; Setup a stack sti hellostring BYTE ' Hello world! ', 0 mov si, offset hellostri ng Call Printstr; Print the ' Hello world! ' string ret BYTE 510-($-Main) DUP (0); Fill the reminder of the first sector with zeros BYTE 55h, 0AAh; Fill the last of the bytes with 55h, 0AAh int the first sectorend main; End of Program

[Reprint] How to write a bootstrapper Hello world

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.