"30 days Homemade operating System"--Virtual machine use

Source: Internet
Author: User

"30 days Homemade operating System" is a good learning system teaching materials, it teaches us how to start from the establishment of the boot area, from zero to achieve an operating system. However, to implement the example in the book, we need to constantly write our written operating system code to the floppy disk and continue to restart the computer to test our code, we must feel very headache it. Rather than keep rebooting, use the simulator to recommend a simulator Qemu, written by Fabrice Bellard, that is very powerful. So let me show you how to use QEMU to run your own operating system in the book.
I. Installing QEMU I am using an apple notebook, Apple has a very good program management tool called Brew, installation of brew needs to enter in the terminalsudo ruby-e "$ (curl-fsslhttps://raw.github.com/mxcl/homebrew/go)"OrCURL-LSSF http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz-c/usr/local--strip 1Then use Brew to install QEMUsudo brew install qemu--env=std--USE-GCCIf successful, many files beginning with QEMU will be created in/usr/local/bin/, where qemu-img and qemu-system-i386 are the two most important files for us. Second, the self-made operating system for the convenience of readers, I put the essence of the first three days of the book in a program, which contains the FAT floppy disk format, read the sector from the floppy disk and output text on the screen. The procedure is as follows:; program name Ipl.nas; Hello-os; Tab=8
ORG 0x7c00; After the program is loaded into memory address 0x7c00
The following is a description of floppy disks used in standard FAT12 formatStart:JMP EntryDB "HELLOIPL"; Boot area name (8 bytes)DW 512; sector size (512 bytes)DB 1; cluster size (1 sectors)DW 1; Fat start positionDB 2; Fat numberDW 224; root directory size (224 items)DW 2880; disk size (2880 sectors)DB 0xf0; disk typeDW 9; Fat lengthDW 18, number of sectors per trackDW 2; head numberDD 0; Do not use partitionsDD 2880; rewrite disk sizeDB 0,0,0x29; unclear meaningDD 0xFFFFFFFF; may be a volume label numberDB "Hello-os"; Disk name (11 bytes)DB "FAT12"; Format name (8 bytes)resb 18; 18 bytes Empty
Entry:MOV ax,0; Initialize RegisterMOV Ss,axMOV sp,0x7c00MOV Ds,axMOV Es,ax; Read Diskcyls EQUMOV ax,0x0820MOV Es,axMOV ch,0; Cylinder 0MOV dh,0; Head 0MOV cl,2; sector 2Readloop:MOV si,0; record number of failures
Retry:MOV ah,0x02; Read DiskMOV al,1; 1 sectorsMOV bx,0MOV dl,0x00; A driveINT 0x13; call disk BIOSJNC next; jump FinADD si,1CMP si,5; compare SI with 5JAE error; SI >= 5 o'clock, jump to errorMOV ah,0x00MOV dl,0x00INT 0x13; Reset DriveJMP RetryNext:MOV ax,esadd ax,0x0020; Move the memory address back 0x200MOV Es,ax; Because there is no add es,0x20ADD cl,1CMP cl,18Jbe Readloop; if CL <= 18, skip to ReadloopMOV cl,1ADD dh,1; Read the other side of the diskCMP dh,2JB ReadloopMOV dh,0ADD ch,1CMP ch,cyls; read Cyls cylinderJB Readloop
; output HelloWorld
MOV si,msgPutloop:MOV Al,[si]add si,1; add 1 to SICMP al,0
JE FinMOV ah,0x0e; display a textMOV bx,15; specify character colorINT 0x10; Call the video card BIOSJMP PutloopFin:HLTJMP FinError:MOV si,errmsgErrloop:MOV Al,[si]add si,1; add 1 to SICMP al,0
JE FinMOV ah,0x0e; display a textMOV bx,15; specify character colorINT 0x10; Call the video card BIOSJMP Errloop
msg:DB 0x0a, 0x0a; wrap 2 timesDB "Hello, World"DB 0x0a; line breakDB 0ErrMsg:DB 0x0a, 0x0a; wrap 2 timesDB "Disk Error"DB 0x0a; line breakDB 0Marker:resb 0x1fe-(marker-start)DB 0x55, 0xaaThe following is the other contents of the diskDB 0xf0, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00Resb 4600DB 0xf0, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00Resb 1469432
Readers who read the first three chapters understand that the above procedure should not be difficult. In addition, the FAT boot sector format is to be http://www.ntfs.com/fat-partition-sector.htm, so if you find that the machine code length of the JMP entry in the compiled machine code (which takes up the first three bytes) The change results in a non-conforming fat format (starting with the four-byte address of 0x03), then you need to go back to the program and manually add the placeholder bytes using DB after JMP entry.
III. Assemble the NASM assembler I use with my Mac. See what type of machine code can be assembled intoNASM-HFWe need to compile the code into bin format, which is also the default format for NASM. Because all of our code is Intel's 16-bit assembler instruction, and the MAC CPU uses an Intel chip, the compiled code x86 and x64 can be executed.Nasm-f bin Ipl.nas-o ipl.bin-l ipl.lst- f followed by the output format,-o after the output file,-L is followed by a list file, the content is assembly language and machine languages of the table. You can use Xxd to view the contents of a binary file:xxd Ipl.bin |And:file Ipl.bin # shown as: DOS floppy 1440k, x86 hard disk boot sectorqemu-img Info ipl.bin # its corresponding QEMU image type is raw
Iv. virtual Machinesin the terminal, enter:QEMU-SYSTEM-I386-FDA Ipl.bin-boot awhich-fda/-fdb specifying floppy disks-HDA/-HDB/-HDC/-HDD specifying a hard drive-cdrom Specifying Discs-boot specifies which device to boot fromA (floppy), C (hard disk), D (CD-ROM), n (Network)because Ipl.bin is a floppy disk format and is a startup disk, we use-FDA ipl.bin-boot aThe results of the operation are as follows:
Five, make U disk image of course we can also write the ipl.bin to the U-disk and start from the U-disk, when the U disk is actually equivalent to a floppy disk. When I insert the U-disk, I have one more disk2 in/dev.sudo diskutil unmountdisk/dev/disk2sudo dd if=ipl.bin Of=/dev/disk2 # writes the floppy image to the U-diskOnce you've written it, reseat the U-disk once.sudo diskutil unmountdisk/dev/disk2sudo qemu-system-i386-fda/dev/disk2-boot aThe results of the operation are as above.

"30 days Homemade operating System"--Virtual machine use

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.