About 16-bit OS attempts (2)

Source: Internet
Author: User
About 16-bit OS attempts (2)

At the end of the last article, I posted a large part of my boot loader, which may be confusing. here I wrote another boot loader in the 16-bit real-time mode of the tiny version. Let's take a look.
Here I will explain again that this article only records my own 16-bit OS experience and is no standard tutorial. however, I will not end a boot loader like other articles. later, I will do many things like kernel and file system.

Bits 16
Org 0x7c00

Entry:
MoV ax, 0
MoV ds, ax
MoV Si, welcomemsg
Call putstr
Hang:
JMP hang

Welcomemsg dB 'Welcome to my Operating System', 0

Putstr:
Lodsb
Or Al, Al
JZ putstrd
MoV ah, 0x0e
MoV BX, 0x0007
Int 0x10
JMP putstr
Putstrd:
Retn

Size equ $-entry
% If size + 2> 512
% Error "code is too large for Boot Sector"
% Endif
Times (512-size-2) db 0

Db 0x55, 0xaa

The real assembly code is displayed in the dark red, and the macro or some control statements of NASM are displayed in the blue.
The first is bits 16, which indicates that we need to compile this program into a 16-bit code, org 0x7c00 tells the compiler that this code will be executed starting from the address 0x7c00 (because the assembly compiler uses an absolute address for references to static variables, therefore, you must set the address where your program starts. for example, under DOS. setting org 0x100 for the COM file is also true. otherwise, your data address will be incorrect)

Entry:
MoV ax, 0
MoV ds, ax
MoV Si, welcomemsg
Call putstr
The entry in the program starts to run. the following Assembly Code does not need to be explained. upload 0x0000 to DS first. because our program is executed at 0x0000: 0x7c00, the segment address should be 0x0000; otherwise, our welcomemsg will not be displayed.
Then call a Child Program putstr. its function is to display a string. the parameter is placed in the SI register. I pass the welcomemsg address to Si, and then call putstr to display the welcome to my operating system. many people will be excited here. after all, it is really nice to see your "Operating System" print a sentence for yourself! However, this boot loader cannot even be connected to a real boot loader. How can it be said to be an operating system.

Putstr:
Lodsb
Or Al, Al
JZ putstrd
MoV ah, 0x0e
MoV BX, 0x0007
Int 0x10
JMP putstr
Putstrd:
Retn
This subprogram will not be mentioned. It is very simple, that is, the interruption of the 10 h display character of the BIOS is called. Please note that the 21 h dos interrupt should not be used to display the characters! This is because it is a DOS thing. Here we are starting another "Operating System". How can we use dos interruptions?

Size equ $-entry
% If size + 2> 512
% Error "code is too large for Boot Sector"
% Endif
Times (512-size-2) db 0

This blue code is not for our program, but to control the size of our program to 512 bytes. because this program is placed in the first sector of the floppy disk and is tailored to the first sector of the floppy disk, the size must be 512 bytes, which is also the size of a sector.

Db 0x55, 0xaa

This is a bit strange. Why do we add 0x55, 0xaa at the end of the program? This is what the computer BIOS requires. I didn't say that, will the BIOS automatically read the first sector of the floppy disk and execute it? When the BIOS reads 0x55, 0xaa, it will know that the information of the read sector has ended, and then it will automatically execute the code just read. so we must add 0x55, 0xaa.

Now, the introduction of this mini boot loader is complete. however, this boot loader program feature is not complete yet. because the most important function of boot loader program is to export the OS kernel for running. it is impossible for us to include the OS to the boot loader program. The maximum boot loader program can only be 512. How can this problem be solved?

The method for exporting kernel should be very simple. in particular, when we choose a floppy disk as the storage medium for our OS, it is easy and pleasant to read data. there is a 13 H interrupt program in the BIOS for disk management, which provides us with many features. I will not elaborate on this interruption here. You can refer to the compilation language books for details.

My OS is very small and in 16-bit real-world mode, so the kernel is also very small. I set it to 8 K, which is exactly the size of 16 slices. a mb Floppy disk has 18 sectors in one track (cylindrical. the size of 17 slice in my Kernel plus my boot loader. good! It can be mounted on a track on a disk.

By the way, I forgot to introduce how to write the boot loader program to the disk.
Many methods, especially for floppy disks. you can find a registered winhex software that supports read/write to the disk sector and is convenient. another method is to download a partcopy program from the website I introduced earlier, but I have never used this program. because my winhex function is comprehensive and intuitive to use, you can copy the file data to any sector on the disk using the clipboard. the debug tool in the old dos can also be used to write disks.

For example, your boot loader program is named Boot. ASM. Then it is compiled using NASM.

Nasmw boot. ASM-O boot. Bin

Debug
-N boot. Bin
-L 0
-W 0 0 0 1
-Q

Run the yellow command in your DOS to compile boot. ASM and write it to the disk. Although it seems very convenient to use DEBUG, I think it is better to use winhex.

Didn't we say that our boot loader is not fully functional? Now let's add all of its functions. for the o in 16 real-time mode, we use the BIOS 13 H interrupt to read the data in the last 16 sectors of the disk. it is up to you to determine the location in the memory. some people like to read 0x800: 0x0000. My OS reads 0x500: 0x0000. you may encounter unexpected errors when reading the kernel to other places, so you have read 0x500: 0x0000. the kernel of my OS runs normally.

In the previous article, the boot loader has read the kernel code. You can also go and see the <16-bit OS attempt (1)>.

Only the code that calls 13 h to read the kernel is provided here.

MoV ax, 0x500; first pass the buffer segment address of the data stored in sector 01 to ax
MoV es, ax; pass through ax, and then pass the buffer segment address to es
MoV BX, 0; buffer offset address is 0
MoV DL, 0; the drive to be read is 0 h, which is a soft drive
MoV DH, 0; the number of heads to be read is 0
MoV CH, 0; the track number to be read is 0
MoV Cl, 2; the number of sectors to be read is 2
MoV Al, 16; the number of sectors to be read is 16, because my kernel has 16 sectors in total.
MoV ah, 2; call the disk read interrupt program
INT 13 H

Okay. After reading kernel to 0x500: 0x0000, you should execute kernel.
This is easy to implement in assembly language. Isn't it okay to use a JMP command?
However, you must pay attention to some details when using JMP.

MoV ax, 0x500; jump command to 0x500: 0000, and change es and DS to 0x500, but note that CS cannot be changed before JMP command
MoV es, ax
MoV ds, ax
JMP 0x0500: 0x0000

The red above is the code for jump from boot loader to kernel. we can see that in addition to JMP 0x0500: 0x0000, we also need to set the ES, DS and other registers. the green comment above records a small mistake I have made. at that time, in addition to setting es and DS to 0x500, I also set CS to 0x500. CS records the segment address of my current code. When we use JMP 0x500: 0x0000, CS will automatically change to 0x500. therefore, we must not change CS before JMP. once CS is changed, the Code executed by the computer will jump to the segment address 0 x for execution. these are just a few things. I don't know if I have made it clear. the CPU Execution Code of our computer is executed in the order specified in Cs: IP address. Every time a command is executed, the IP address is moved to the next one. so once the Cs or IP address changes, the execution order will change. therefore, we generally cannot touch CS and IP addresses.

 

Well, after your boot loader is compiled successfully, it is written to the disk, put it into the computer, restart the computer, and start it on a floppy disk, you can see the running effect of your OS boot loader.

It would be very troublesome if we had to restart the computer every time to see our OS running results. fortunately, many good simulators have been written by outsiders to simulate the scenario where the computer starts running our OS.

Bochs-2.0 is a very good 80 x86 simulator. The same, you can go to fuse.

First decompress, enter the bochs-2.0, there is already a dlxlinux use example. I directly take it to modify it can be used. Open start. bat, add

Set bxshare = F:/bochs-2.0
../Bochs

I don't know how it works. Its Environment directory bxshare is not set, so we need to go to start. BAT set up a bit, my bochs-2.0 is installed in F:/bochs-2.0, so set to set bxshare = F:/bochs-2.0

Then open bochstr.txt and find

# Choose the boot disk.
Boot:
Change boot to A: so that the bochs-2.0 will simulate starting from:

# What disk images will be used
Floppya: 44 = floppya. IMG, status = inserted
Floppyb: 44 = floppyb. IMG, status = inserted
Floppya: 44 = floppya. IMG illustrates that the image file of disk A is floppya. IMG.

How to Create floppya is involved here. IMG scene file problems. boch's scene file is a completely 1.44mb file. you can copy the data of each sector on the floppy disk to this file in sequence.

You can use the bximage.exe program in bochto create a floppya. IMG image file.
Then, compile your boot loader. bin to floppya. IMG can be removed in the first 512 bytes. you can use winhex to copy data. It is very convenient. You can copy data directly using the clipboard. so I keep saying that winhex is really a good thing!

Okay, everything is done. run start. bat, there is a choice, don't worry about it, our parameter settings are set, directly "simulate the run. then the Boch pop-up window will pop up, which is the image of running the OS simulation by Boch.

So tired, I 've talked so much about it. Even the kernel hasn't started yet. Let's start to enter the kernel section in the next section!

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.