First, the introduction:
If you write a DOS application using a compiler such as turboc2.0/3.0 or borlandc3.x, writing an application in the form of a command-line argument is a very easy thing to do, as long as you add a few arguments to the main function main (), OK (int main ( int Argc,char *argv[],char *env[]) {}). It is difficult to write a command-line parameter relative to the assembly language, which uses the DOS prefix PSP (program Segment Prefix) knowledge and other related DOS knowledge. (This article only describes the parameters, the environment block is not discussed)
Second, relevant knowledge:
Type a command (internal/external command) or the name of the program under DOS prompt, DOS SHELL (Command.com) first to determine whether it is an internal command or an external command or user program, if the internal command calls Command.com in memory part of the DOS internal command code, if the external command or user program, DOS The shell searches the current directory and search path for a matching filename, finds the loader, displays an error message with the load error, and displays bad command or file name if it is not found.
The user carries a string at the DOS prompt, and the DOS shell ends with a carriage return (0Dh) as a command and argument, the first space before the string is the command name (must conform to the DOS naming rules), The character of the first space (including a space) to the carriage return as a parameter to the command or program.
The program segment prefix--PSP is when DOS loads an external command or user program (with an extension of COM or EXE), before the program section, set a block with a fixed length of 10H (256 bytes, 16 bytes), and a Memory control block (MCB) for the PSP and the program segment. The PSP is at the beginning of each program, both COM and EXE,PSP data structures are the same. PSP is a program and DOS interface, DOS using the PSP management process, the DOS user process refers to a loaded memory executable program or has been transferred into memory but not executed program, COMMAND. COM is a program that was first loaded into memory, so it can be considered as an ancestor process, external command or user program as a child process, by the DOS through the 4BH chant function of int 21H. The user program can also load its own subprocess through the 4BH chant function of int 21H, control the execution of the subprocess, and get the health of the child process through the 4DH chant function call.
The PSP contains a number of important information about program startup, execution, completion, and process scheduling, process environment address, and process flags. Programs can also control the communication between parent-child processes using the PSP. As for the PSP data structure details please refer to the relevant books, this article is not detailed.
DOS loading a COM or EXE program, the segment register Ds,es point to the PSP segment address (PSP address is the unique identifier of the process), rather than pointing to the program's data and additional segments. The cs,ss of the COM file also points to the PSP section address. EXE files are cs,ss,ip and the SP needs to be relocated.
DOS load an external command or user program, the file name to the string between the carriage return, up to 127 characters as parameters, and send these strings to the PSP 81H to start the area, displacement 80H of a byte to hold the parameter string length (carriage return is not counted). You can use DEBUG.EXE to load a program with parameters, and then see the loader parameters with the D DS:80 subcommand. Command line arguments typically start with a space (20H), a carriage return (0DH) ends, but redirects in the command line, pipe characters, and related information are not passed to the PSP as parameters.
Third, the sample program:
This example program paratest.asm to display a hint if there are no parameters (the parameter is a series of spaces also considered to be without parameters). The program takes the character '/' as the label of the parameter, '/' after the character is the argument, depending on the parameters to display a different string, and ignore the '/' before the space. The program also shows the illegal parameters, because the program to save the parameters of the unit only set two bytes, if the character '/' after the first character is a valid parameter, the program regardless of the character '/' after the number of characters are considered legal.
By the way, the Debug subroutine in the sample source program (PARATEST.ASM), which is included in the article, uses the 07H chant function of int 21H to wait for the user's keyboard input, which is equivalent to the Getch () function in Turboc, as a breakpoint of the program. We can also use it to display debug information for breakpoints (including the values of each register). But pay attention to save the site, and to restore the site.