30-day Homemade OS-from boot zone to disk

Source: Internet
Author: User

The previous article described how to use a virtual machine to run our written operating system, but the previous operating system only exists in the boot sector, and then we will see how the program jumps from the boot sector to another location.
The boot area content is loaded at0X00007C00-0X00007DFF, the man who established the position was the engineer who developed ibm-pc in that year. 0X7E00-0X9FBFF is a memory location that the operating system can use freely. As described in the 30-day homemade operating system, we read the first 10 tracks of data into memory and the memory location starts at 0x8000. Therefore, in the disk image file, the word Fugazai to memory at address X is 0x8000+x. Now we need to execute the program in the 0X004200 address on the disk image, then the memory address is 0x8000+0x4200=0xc200.
Procedure one, Ipl.nas;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Hello-os; Tab=8
ORG 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 0xc200; jump to 0xc200, i.e. Hanbote.nas program addressMOV ah,0x0e; display a textMOV bx,15; specify character colorINT 0x10; Call the video card BIOSJMP PutloopJMP 0xc200Fin: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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Program II, Hanbote.nas;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Haribote-os; Tab=8ORG 0xc200; The address in memory at the beginning of the programMOV si,msgPutloop:MOV Al,[si]add si,1; add 1 to SICMP al,0JE Black    ; JE FinMOV ah,0x0e; display a textMOV bx,15; specify character colorINT 0x10; Call the video card BIOSJMP Putloop
Black:MOV al,0x13; VGA graphicsMOV ah,0x00INT 0x10Fin:HLTJMP Fin
msg:DB 0x0d, 0x0a, 0x0a; newline 2 timesDB "haha"DB 0x0a; line breakDB 0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;program3, Makefile;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Default:Make img
Img:hanbote.bin Ipl.binDD if=hanbote.bin of=ipl.img bs=512 seek=33 count=1 conv=notrunc
Ipl.bin:ipl.nasNasm-f bin Ipl.nas-o ipl.img-l ipl.lst
Hanbote.bin:hanbote.nasNasm-f bin Hanbote.nas-o hanbote.bin
run:imgQEMU-SYSTEM-I386-FDA Ipl.img-boot a
Clean :RM ipl.lst;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
This way you can see the results of the run by typing make run in the terminal.where we use DD to write hanbote.bin to the position we want in Ipl.nas. The position of the Hanbote.bin in Ipl.nas starts with the 0x4200, since DD takes 512 bytes as a piece, so the output file is positioned to 0x4200/512=0x21=33,notrunc with seek to ensure that the ipl.img is not truncated.

30-day Homemade OS-from boot zone to disk

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.