Implement your own operating system. Part 1

Source: Internet
Author: User

1-1

Implements a simple function call.

Focus on Stack-oriented applications.

RET is called when a call is returned, and a number is followed to indicate the number of bytes of data into the stack during the call.

1-2

Implements code self-migration.

The include statement cannot start with a file because the program starts from the first line during execution. (It may be relocated. It is unknown currently)

The memory addresses marked after the move must be adjusted again.

When short jump or short call is used, the label does not need to be adjusted because the indirect address of the operand here is the difference between the two addresses; when calling a long jump or long call, the operand is the absolute value and needs to be adjusted.

In the indirect addressing of the access stack, only BP can be used for offset.

Do not write anything to CS at any time, or the program will crash.

1-3

Copy the fat and root directories of a floppy disk to the memory.

Current memory usage:
9000: Running --9000: 01ff Boot Code, total length: 512
9000: 0200--9000: 13ff fat content, total length: 1200 h
9000: 1400--9000: 2fff root directory content, total length 1c00h

In the fat2 file system, the first three bytes are the jump command, the last 59 bytes are the disk parameter, then the 448 byte code area, and finally the 0xaa55 boot flag.

The problem is that the compiled code is too long, with only 167 bytes of code segment left.

The next step is to use macro definitions instead of short function calls to further compress the code.

1-4

After changing dispstr to a macro definition, the code is successfully compressed into 22 bytes.

The header file containing macro must start with the file, so the macro definition should be placed in a separate file.

Loop uses a relative offset.

When loop is used, the Cx value is the number of cycles, and there is no plus or minus one offset.

1-5

Current memory usage:
9000: Running --9000: 01ff Boot Code, total length: 512
9000: 0200--9000: 13ff fat content, total length: 1200 h
9000: 1400--9000: 2fff root directory content, total length 1c00h

You can find the loader. com file.

In the fat2 file system, all file names are automatically converted to uppercase.

Method for returning dos in real mode:
% Macro returndos 0; here 0 must be written
MoV ax, 4c00h
Int 21 h
% Endmacro

Next step:
Load loader.com
Then the memory content should be:
9000: Running --9000: 01ff Boot Code, total length: 512
9000: 0200--9000: 13ff fat content
9000: 1400--9000: 2fff root directory content, total length 1c00h
9000: 3000--9000: fc00 loader.com

1-6

Load loader.com
The memory content is:
9000: Running --9000: 01ff Boot Code, total length: 512
9000: 0200--9000: 13ff fat content, total length: 1200 h, that is, 4608 bytes
9000: 1400--9000: 2fff root directory content, total length 1c00h
9000: 3000--9000: fc00 loader.com, total length: 0cc00h, that is, 52224 bytes

 

 

Will jump to 9000: 3000

Code address:

Http://d.download.csdn.net/filedown2008/aHR0cDovL2RsMi5jc2RuLm5ldC9kb3duNC8yMDA4MDMzMC8zMDIxMzU0MTgyMi56aXA=! 398454

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.