Use MASM and tc2.0 to write a simple x86 boot program

Source: Internet
Author: User
Tags fread

Boot. ASM and kernel. ASM uses masm5.0 to compile the corresponding executable files and tc2.0 to compile the load. c. Get the executable file and put the three executable files in the same folder.
Upload a blank disk and execute load.exe under the console to obtain a boot floppy disk;
Restart the computer and set disk A as the first boot disk to boot the system from disk A. The only thing you can do after entering the system is to input and display characters on the keyboard, this cycle.

The specific operation process is described in more detail in the comments of the load. c file below.

Purpose: To write a boot program and an ultra-simple kernel. After successful boot, you can enter characters on the keyboard and display them.
Tool: masm5.0 compiler, tc2.0, a computer (if you have a virtual PC, you can do this without damaging the machine ).
Required knowledge:
Let me briefly talk about the computer boot process. After the computer is turned on, it goes through a complicated process, including self-check. BIOS is in the memory .... finally, the BIOS puts the 512 bytes of the 0 track 1 Sector of the boot disk at the beginning of memory 0000: 7c00, and then gives the control to the boot program. At that moment, cs = 0000, IP = 7c00. (I think so), and then everything can go into your grasp.
Now we need to write the program after 7c00 in assembly. Put this program in the first sector, called boot. The function of this program is to put the kernel in the second sector into memory 8000: 0000 and jump there for execution.

File: boot. ASM

; File: boot. ASM
The uploload.exe program writes the binary file generated by the following code to disk.
; 0 track 1 sector;
The BIOS boot program automatically loads the content of this sector into the memory at startup.
; And execute it at 07c0h;

Code segment
Assume Cs: Code
Start:; cs = 0000, IP = 7c00
MoV ax, offset start
Add ax, 07c0h; ax and 07c0h are sent to DS,
MoV ds, ax; set the data segment register ds,
Make DS: 0000 the 0000: 7c00 of the memory address
MoV Si, offset MSG; MSG partial transfer Si
Call display; Display
Initialize the registers of the INT 13 H and load the kernel)
(For detailed settings, refer to the BIOS interrupt manual)
MoV ax, 8000 h; the segment address of the memory that the kernel will store
MoV es, ax; passed to es
MoV BX, 0; kernel offset address is 0

MoV DL, 0; drive number 0 h, that is, disk
MoV DH, 0; head number: 0
MoV CH, 0; channels 0
MoV Cl, 2; slice number 2
MoV Al, 1; the number of sectors to read is 1
MoV ah, 2; call the disk read interrupt program
INT 13 H
; Kernel loaded
MoV Si, offset OK; show OK
Call display
MoV BX, offset Ker; kernel stores the address 8000: 0000
According to the settings of the front side (ES: BX.
To run the kernel program.
Jmp dword ptr [BX]; jump to the address pointed to by the pointer.

Display:
Lodsb; load a byte to Al
Or Al, Al; Al = 0 indicates that the string ends.
JZ disend; jump to the end and return
MoV ah, 0eh; 0eh is the display function number
MoV BX, 7; color
Int 10 h
JMP display
Disend:
RET

MSG dB 'my OS is loading... ', 0; display information
OK dB 'OK', 13, 10, 0
Ker DW 8000 h; kernel memory address 0000:

Code ends

End start

File: kernel. ASM

; File: kernel. ASM
The uploload.exe program writes the binary file generated by the following code to disk.
; 0 channels, 2 sectors;
The boot code in the 0 track and 1 sector will load the code into the memory and skip
Go to the corresponding address to execute this code.

Code function: receives input from the keyboard and displays it in sequence.
Kernel segment
Assume Cs: Kernel
Start:
MoV ah, 0; Reading characters from the keyboard
Int 16 h
MoV ah, 0eh; display function
Int 10 h

CMP Al, 13; line feed for carriage return
Jnz start
MoV Al, 10
Int 10 h

JMP start; infinite loop

Kernel ends
End start

File: load. c

/* File: load. C */
/* This program is so simple and has only two functions. Accept characters and display, and compile the connection.
Now we have two files boot.exeand kernel.exe. In this example, boot.exe is not executed. You can run
The next operation is kernel.exe. You can check whether the write is correct.
Now, we need to write the two files into the 1, 2 sectors of the floppy disk. However, you will find a strange problem. boot has 603 characters
Section, the kernel has 522 bytes, but one slice only has 512 bytes. How can this problem be written? I am also fascinated by this question.
Confused. Later, we assembled two EXE files to find that the code we actually wrote appeared at the 513-byte location of the EXE file,
The first 512 bytes of the EXE file are the prefix of the EXE file defined by Mircosoft, which contains the identity, size,
Segment positioning pointer and other things. We don't need to wait for the 512 bytes, so we will write a program.
Write boot.exe and kernel.exe to a floppy disk. (note that the last two bytes are required when writing data to the boot sector.
Is 55aa, which is the identification of the specified Boot Sector .) I chose tc2.0 here. Of course, you can also write it in a sink,
The following figure shows writebt. C.
*/
# Include <stdio. h>
# Include <dos. h>
Union regs inreg, outreg;
Struct sregs segreg;
Main ()
{
Int I;
Char boot_buf [512];/* temporarily store the content of boot.exe */
Char kernel_buf [512];/* Contents of kernel.exe are temporarily stored */
File * FP;
For (I = 0; I <512; I ++)
{/* First clear the two buffers 0 */
Boot_buf [I] = 0;
Kernel_buf [I] = 0;
}
If (FP = fopen ("boot.exe", "rb") = NULL)
{
Printf ("cannot find boot.exe"); exit (0 );
}/* Start boot.exe */
Fseek (FP, 512l, 0);/* locate the file in 513rd bytes, that is, 512l */
I = 0;
While (1)
{
Fread (& boot_buf [I], FP);/* read all the following content until the end */
I ++;
If (feof (FP ))
{
Fclose (FP );
Break;
}
}
Boot_buf [510] = 0x55;/* the last two bytes must be 55aa */
Boot_buf [511] = 0xaa;

/* Settings: Write the boot program in boot to the 0th track and 1 sector of disk */
Inreg. H. Ah = 0x03;/* call the drive 3 function of bios13h */
Inreg. H. Al = 0x1;/* The number of sectors to read is 1 */
Inreg. H. Ch = 0;/* The track number is 0 */
Inreg. H. Cl = 1;/* The Slice number is 1 */
Inreg. H. DH = 0;/* The head number is 0 */
Inreg. H. DL = 0;/* The drive number is 0, that is, disk */
Inreg. X. bx = fp_off (boot_buf);/* write the memory offset address of boot_buf in BX */
Segreg. es = fp_seg (boot_buf);/* write its memory segment address in es */
Int86x (0x13, & inreg, & outreg, & segreg);/* call an interrupted write disk */
If (_ ah! = 0)/* Ah: 0. The disk has just been written. Otherwise, exit */
{
Printf ("error writing"); exit (0 );
}
Else
{
Printf ("OK ");
}


If (FP = fopen ("kernel.exe", "rb") = NULL)/* Open kernel.exe */
{
Printf ("cannot find kernel.exe"); exit (0 );
}
Fseek (FP, 512l, 0);/* locate the file in 513rd bytes, that is, 512l */
I = 0;
While (1)
{
Fread (& kernel_buf [I], FP);/* read all the following content until the end */
I ++;
If (feof (FP ))
{
Fclose (FP );
Break;
}
}

/* Setting: Write the program in the kernel to The 0th Track 2 sector of disk */
Inreg. H. Ah = 0x03;
Inreg. H. Al = 0x1;
Inreg. H. Ch = 0;
Inreg. H. Cl = 2;/* The Slice number is 2 */
Inreg. H. DH = 0;
Inreg. H. DL = 0;
Inreg. X. bx = fp_off (kernel_buf );
Segreg. es = fp_seg (kernel_buf );
Int86x (0x13, & inreg, & outreg, & segreg );
If (_ ah! = 0)
{
Printf ("error writing"); exit (0 );
}
Else
{
Printf ("OK ");
}


}

/*
Now we have boot.exe, kernel.exeand writebt.exe, copy them to the same directory, and then
Insert into the soft disk and execute writebt.exe. If error writing occurs, it indicates that the write disk has an error. You run it several times until okok appears.
But do not immediately switch to disk A and press Dir, so that unformatted information will appear in the system and you need to format it. Because
We have modified some information about the Microsoft-defined floppy disk. Dos naturally cannot identify it. Now you can restart from a floppy disk.
You will see your results.
Is this system too simple? Yes, the real operating system includes file management, memory management, and device management...
Slow learning.
My methods may not be very good and inefficient. Please criticize and correct them. My mailbox is cyxisgreat@sina.com
These three programs are compiled and run on my machine. Do not modify the content in inreg. H. * In writebt. Otherwise
At your own risk.
*/

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.