Implementation of the boot sector

Source: Internet
Author: User

Write for the first time, refer to the realization of others under.

Experimental environment and tools

    • Ubuntu 14.0
    • Nasm

Ideas:

    • Write a piece of assembly code, outputHello, World
    • Write the segment code to the first sector of the floppy image
    • Install VirtualBox Create an operating system, boot from floppy disk mirroring, view results
Boot Area code
1;; The BIOS will load the 512-byte boot sector into0000: 7c00 Office,2 ;; then jump to 0000:7c00 and give control to the boot code. 3 org 07c00h; this line tells the compiler that our code will be loaded at 7c00. 4 mov ax, CS; Send the value of the code snippet register to ax5 mov ds, ax; Place the address of the data segment as the address of the code snippet? 6 mov es, ax; Place the address of the additional segment as the address of the code snippet? 7 call Dispstr; invoke display string Routines8 jmp $; infinite Loop, $ represents the address after the current line has been compiled9 this is the whole process of execution.Ten ;; The following is the Dispstr subroutine One Dispstr: A mov ax, bootmessage; pass the first address of the string to register ax - mov bp, ax; The CPU will use ES:BP to address the string -MOV cx, -; know the length of a string by Cx,cpu themov ax, 01301h; Ah=13 means interrupt number 13th, al=.01H, indicating that the target string contains only characters, attributes are included in BL, move cursor -MOV bx, 000ch, black-bottom scarlet letter, bl=0CH, highlight -MOV dl,0;d h indicates that the DL indicates the number of columns shown in the first line -     int10h; BIOS 10H Interrupt 13th number interrupt for displaying string + ret -Bootmessage:db"Hello, OS world!"For NASM, the function of the label and the variable, the DB represents the Definebyte + ;; $ The current line is assembled after the address, $$ represents a section at the beginning of the address, this program has only one sections, so refers to 0x7c00 ATimes510-($-$$) DB0fill the remaining space so that the generated binary is exactly 512 bytes atDw0xaa55The end flag, if the discovery sector ends in 0xaa55, the BIOS considers it to be a boot sector, and DW represents define Word
View CodeCompile Build Binary

Need nasm .

Compile our sector code to generate the binary:

NASM Boot.asm-o Boot.bin
Write the first sector of a floppy disk

We use a floppy image to simulate a floppy disk.

if=/dev/zero of=emptydisk.img bs= count=2880if=boot.bin of= Boot.img bs= count=1if=emptydisk.img of=boot.img skip=1 seek=1 bs= count=2879 #在 bin to fill up the image file, become a suitable size floppy disk image

In this way, we are ready to boot the virtual floppy disk for the system.

Install VirtualBox, set up the system

VirtualBox can be installed directly from the Linux software warehouse.

The installer creates the Vboxusers user group and compiles the required kernel modules. Now, you cannot start VirtualBox because your current user is not yet part of the Vboxusers user group. You can use the following instructions to add the current user to the Vboxusers user group:

sudo adduser username vboxusers

Change the username above to the name of the current user.

Start VirtualBox (direct input VirtualBox).

You may be prompted that Vboxdrv failed to load successfully. If this happens, you will need to recompile the Vboxdrv module. Install linux-headers and dkms then sudo dkms autoinstall compile the build vboxdrv module.

Manually sudo modprobe vboxdrv loading the Vboxdrv module, starting VirtualBox should not be a problem.

To build a virtual machine, there are no hard drives available, add a floppy drive, put our boot.img load into the floppy drive, start the virtual machine, and if successful, you can see:

VirtualBox issues that may occur:

    1. Failed to open a session for the virtual machine a. Failed to load VMMR0.r0 (VERR_SUPLIB_OWNER_NOT_ROOT). Unknown error creating VM (VERR_SUPLIB_OWNER_NOT_ROOT).Workaround:sudo chown -R root:root /usr/lib/virtualbox

    2. Effective UID is not root. Workaround: sudo chmod 4711 /usr/lib/virtualbox/VirtualBox .

Implementation of the boot sector

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.