Assembly language Writing DOS memory-resident program (4)

Source: Internet
Author: User
Tags command line

Four basic resident procedures
4.1 A basic COM program
There are two forms of executable files under DOS, which are COM files and EXE files. in which, COM files can be loaded and executed quickly, but their size can not exceed 64K bytes, only one segment, The code snippet. And the starting address is 100H instruction must be the program's startup instruction. exe files can be loaded into many segments, so the program's size is not limited, but the program loading process is slow, and for the memory-resident program will cause even greater trouble.
The following is a COM file that can be executed correctly, but its contents are empty; Just a COM file framework that can add any application part of your writing to this file to form a COM-formatted memory-resident program:
; Section 1
CSEG segment
Assume Cs:cseg,ds:cseg
Org 100h
; Section 2
Start
Ret
; Section 3
Cseg ends
End Start
The above procedure can be divided into three parts, the first section defines the location of the code snippet and the data segment in the program, and the starting address of the execution code. The second part is the executable program, in this example only a RET instruction. The third part is the end of the package section, in which the ending narration contains the program start execution address.
If you put the above program through the assembly connection, you will find that the resulting COM file is only one byte long. This is because the resulting COM file does not have a program segment prefix (PROGRAMSEGMETN profix), Because in DOS all and COM files have the same program segment prefix. When DOS loads a COM file into memory, it automatically generates a correct prefix for the program segment. In the process of executing a program, you can modify the prefix of the program segment as needed, but in the beginning, All COM files have the same program prefix. The following is the format of the program prefix.
Offset position meaning
0000H program termination Processing subroutine address (INT 20H)
0002H End Address of assignment segment, segment value
0004H Retention
0005H Call DOS Service
000AH the IP and CS of the previous parent program
000EH Previous Parent program's Control_c handler address
0012H hardware error-handling subroutine address for the previous parent package
0016H Retention
Address value of 002CH environment segment
005EH Retention
005CH FCB1
006CH ' FCB2
0080H command line parameters and disk transfer area
4.2 A minimal memory-resident program
The above program is just a general DOS program. It's not memory-resident. The following is a basic memory-resident program structure:
; Section 1
CSEG segment
Assume Cs:cseg;ds:cseg
Org 100h
Start:; Section 2
Nop
Done:; Section 3
mov Dx,offset done
int 27h
; Section 4
Cseg ends
End Start
Compared to the previous program, this program simply adds an done part. This section uses an int 27H interrupt call to terminate and reside in memory (Terminate and Stay resident). When using int 27H This interrupt call, you must set a pointer, Let this pointer point to the part of the memory that can be used, in fact, this is equivalent to setting the location where a COM file can be loaded. In addition, DOS also provides int 21h,ah=31h (resident program, Keep process), but when using this interrupt call, we must set the memory size reserved Instead of setting a pointer, and this interrupt call sends out the exit code.
When using int 27H, you must set a pointer to the beginning of the available storage location so that DOS is used to load the program that is executed later. DOS itself has a pointer, which is the value of the Datum address when the COM file or EXE file is loaded. int urine 27H Will change the pointer or the new value. The storage space between the new pointer and the old pointer is not allowed to be used for DOS so doing so can lead to fewer storage locations available.
The pointer used when calling int 27H is a far pointer, where DX holds the displacement pointer (offset pointer), which can refer to a range within 64K bytes. DOS is a segment pointer (Segment pointer) that can refer to IBM Any segment of 640K bytes in the PC. In the above example, the content of the DS does not have to be set separately because when the COM file is loaded, the DS content is the same as the CS content.
Often in the preparation of the assembly process, a common mistake is: The assume ds:cseg this account is mistaken, to store a preset to the DS, in fact, the assembly language program in the assume narrative does not produce any program code, this function is to tell the assembler to do some necessary assumptions, To properly assemble the program. For example, the following procedures:
CSEG segment
.............
Assume Ds:cseg
MOV Ah,radix
.............
Radix DB 16
.............
Cseg ends
When the assembler sees MOV ah,radix this instruction, it generates a certain form of assignment instruction according to the assume Ds:cseg. In the face of the assume ds:cseg the narration is to tell the assembler, The data segment is in the current code snippet. This is an important key to the memory-resident program. If the DS content and CS are not the same, regardless of whether there are assume narration, program execution will fail.
4.3 Improved memory-resident program
The memory-resident program described above actually does nothing, but resides in memory. In fact, putting any program code between start and end will only execute once and then stay in memory forever, unless you use the transfer instruction to go to the start address, Otherwise it will never be used. Also note that the address value of start is not fixed, and it changes depending on the state of the computer as the program executes.
The following program simply loads the program code that needs to reside, but does not execute.
; Section 1
CSEG segment
Assume Cs:cseg,ds:cseg
Org 100h
; Section 2
Start
JMP Initialize
; Section 3
App_start:
Nop
Initialize
; Section 4
MOV Dx,offset initialize
int 27h
; Section 5
Cseg ends
End Start
The above program is uploaded to the Initialize flag at the beginning of execution, and the appliance resides in the application portion of the memory. The original done has been changed to initialize, while the program code residing in memory is placed between App_start and initialize.
In addition, you may notice that the starting address of the program is not initialize but start. This is because all COM programs start with a 100H address, and start in the above program is placed in 100H. If you put the initialize after the end, Initialize becomes the starting address, but such programs cannot be converted to COM files through exe2bin. If a COM file cannot be generated, then the contents of the segment must be processed directly.
4.4 Reduce the extra burden of memory
So far, no access to the program prefix, when the use of int 27H, the fact is that the pointer before the things are kept in memory, this also includes the COM program segment prefix. Because the COM file is executed, the program segment prefix can be removed.
As can be seen from the above facts: if the program segment prefix can only be removed after the COM appliance program is finished, then the program code residing in memory can be completed. To do this, you can move the entire program down by 256 bytes. But how do we do that? We can set a flag (Flag), Used to indicate whether the program has been executed. If the resident program or the first execution, move the entire program down by 256 bytes to remove the program prefix. But what if the resident program is still not executed after a long period of time after it has been installed? If several resident programs are loaded at the same time , what about the double? These important things need to be solved using different program code. If the program code exceeds 256 bytes, the storage location used is beyond the space wasted by the prefix of the program segment. Some people use some relatively short code to solve this problem, But it's still a bit of a hassle. So for most memory-resident programs, unless there's too little storage space so that 256 bytes become important, it's best not to deal with the prefix, which makes your program simple and easy to read.

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.