Compilation of assembly language command parameter programs
I. Introduction:
If you have used turboc2.0/3.0 or borlandc3.x and other compilers to compile dos applications, it is very easy to compile an application in the form of command line parameters, you only need to add a few parameters in the main () of the main letter. OK (INT main (INT argc, char * argv [], char * env []) {}). Compared with the assembly language, it is difficult to compile a command line parameter program. It requires the DOS program segment prefix PSP (program segment prefix) and other related dos knowledge. (This article only introduces parameters and does not discuss environment blocks)
Ii. Related Knowledge:
Enter the name of a command (internal/external command) or program at the DOS prompt, DOS shell (command. com) First, determine whether it is an internal command or an external command or a user program based on the name. If it is an internal command, call command. the dos internal command code of COM temporarily resident in the memory. If it is an external command or user program, the DOS shell searches for matching file names in the current directory and search path, and finds the matching file name and loads the program, the error message is displayed when an error occurs. If no error is found, bad command or file name is displayed.
Enter a string at the DOS prompt. The dos shell uses the string ending with the carriage return (0dh) as a command and parameter for explanation, the character string before the first space is the command name (must comply with DOS naming Rules), and the character between the first space (including space) and the carriage return is used as the parameter of the command or program.
Program segment prefix -- PSP is an external command or user program loaded by DOS (the extension is com or EXE), set a boundary before the program segment, A storage block with a fixed length of 10 h (that is, 256 bytes, one segment is 16 bytes). The PSP and the program segment have a memory control block (MCB), and the PSP is located at the beginning of each program, both COM and exe have the same data structure. PSP is the interface between programs and DOS. Dos uses PSP to manage processes. A DoS user process refers to an executable program that has been loaded into the memory or a program that has been transferred to the memory but not executed, command. com is the first program to be loaded into memory. Therefore, it can be considered as an ancestor process. External commands or user programs are used as sub-processes. It is loaded by DOS using the 4bh sub-function of int 21h. User Programs can also use the 4bh sub-function of int 21h to load their own sub-processes, control the execution of sub-processes, and obtain the sub-processes running status through the 4dh sub-function call.
PSP contains many important information about program initiation, execution, termination, process scheduling, process environment address, and process flag. The program uses PSP to control the communication between parent and child processes. For details about PSP data structure, refer to relevant books.
When dos loads a com or EXE program, the segment register DS and ES all point to the PSP segment address (the PSP segment address is the unique identifier of the process), rather than to the program's data segment and additional segment. The CS and SS of the COM file also point to the PSP segment address. The CS, SS, IP, and SP of the EXE file must be relocated.
When dos loads an external command or user program, it takes the string between the file name and the carriage return, up to 127 characters as the parameter, and sends these strings to the region where PSP is started from hours, A byte with a displacement of 80 h is used to store the length of the parameter string (the carriage return is not counted ). You can use DEBUG. EXE to load a program with parameters, and then run the d ds: 80 sub-command to view the parameters of the program. The command line parameters generally start with a space (20 h) and the carriage return (0dh) is the end, but the redirection, pipeline, and related information in the command line are not passed as parameters to PSP.
Iii. Example program:
In this example, paratest. when there is no parameter (a series of spaces are also considered as no parameter) in ASM, a prompt is displayed. The program uses the character '/' as the parameter flag, the character after '/' is a parameter. Different strings are displayed based on different parameters, and spaces before '/' are ignored. The program also displays invalid parameters. Because the Unit for saving parameters in the program is set to only two bytes, if the first character after '/' is a valid parameter, the program considers it legal no matter how many characters after the character.
By the way, I will introduce a compilation programming technique. The example source code (paratest. the debug subroutine in ASM uses the H sub-function of int 21h to wait for the user's keyboard input. It is equivalent to the getch () function in turboc and serves as the breakpoint of the program. We can also use it to display breakpoint debugging information (including the value of each register ). However, be sure to save the site and perform on-site recovery.
Iv. Conclusion:
A month ago, I was just looking at other people's assembler programs. Since I started writing programs, I have made great progress in assembly programming. When writing a program, you may encounter problems, and then read the book to solve the problem by yourself. In this way, learning assembly programming is more effective than simply reading a book. I am still a newbie in the level of assembly programming. I will constantly improve my skills and hope to communicate with programmers.
Appendix: source program (paratest. ASM)
; **************************************** ********
; * Program: Use ASM language to creat a command *
; * Line and parameter program .*
; * ===================================================== ======= *
; * Designer: Howard Original Place: Wuhan *
; * Creat Date: 09/30/1999 *
; * Modification date: 10/05/1999 *
; * Now version: 1.0 *
; * Pass: tasm 5.0, tlink 3.1 *
; * ===================================================== ======= *
; * Modification history *
;*----------------------------------------------*
; * Version 1.0 1. Command Line and parameter *
; * 09/30/1999 test program .*
;*----------------------------------------------*
; * Version 1.1 2.add the spaces parameters Jud -*
; * 10/05/19999 gement .*
; **************************************** ********
;
. Model small
. 386
. Code
Org 100 h
Start:
Main proc far
Push CS
Pop Ds; DS = PSP seg address
CLD; cf = 0
MoV Si, 81 h; PSP + 81 h is the first parameter char
Lea BX, parameter; parameter address (offset) saved to BX
; Unit parameter is used to save the Parameter
Lodsb; load a byte from [Si] to Al, and SI = Si + 1
CMP Al, 0dh; enter?
JZ scanexit; If yes then scan parameter end
CMP Al ,''
JZ judgespace
Lodsb
Continue:
Push Si
CMP Al ,'/'
JZ parascanloop
JMP error; wrong parameter
Judgespace:
Lodsb
; Call debug; set break point
CMP Al, 0dh
Je scanexit
CMP Al ,''
Je judgespace
JNE continue
Parascanloop:; saved the parameter to unit Parameter
MoV [BX], Al; save Al to [BX], just save the parameters
Lodsb
CMP Al, 0dh; enter?
JZ choise; If yes then jump choise
INC BX
JNB parascanloop; the next char
Scanexit:
Lea dx, noparametermsg
Call disp
Call rettodos
Choise:
Lea Si, parameter
MoV Al, [Si + 1]; load the parameter to Al
CMP Al ,'? '; Judge the parameter and choose; the different process
JZ help
CMP Al, 'P'
JZ print
CMP Al, 'P'
JZ print
JMP error; wrong parameter
Print:
Lea dx, message
Call disp
Call rettodos
Help:; print the help message
Lea dx, helpmsg
Call disp
Call rettodos
Error:; print the error parameter message
Lea dx, wrongparamsg
Call disp
MoV ax, 0200 H
Pop Si
Dec Si
Prnwrongparameter:
Lodsb
CMP Al, 0dh
JZ retdos
MoV DL, Al
Int 21 h
Loop prnwrongparameter
Retdos:
MoV DL ,'"'
Int 21 h
MoV DL ,'! '
Int 21 h
Call rettodos
Main endp
Disp proc near
MoV ah, 09 h
Int 21 h
RET
Disp endp
Rettodos proc near
MoV ah, 4ch
Int 21 h
Rettodos endp
;
; Set a break point
; Debug proc near
; PUSH AX DX
; MoV ax, 0900 H
; MoV dx, offset debugmsg
; Int 21 h
; MoV ax, 0700 H
; Int 21 h
; Pop DX
; Pop ax
; Debug endp
; Debugmsg dB 0dh, 0ah, 'program stop here, press any key to continue... ',' $'
Noparametermsg dB 0ah, 0dh, 'there is no parameter, enter paratest /? For help. ',' $'
Message dB 0dh, 0ah, 'This is a parameter test program. ',' $'
Wrongparamsg dB 0dh, 0ah, 'the wrong parameter: "',' $'
Helpmsg dB 0dh, 0ah, '1. paratest /? '
DB 0dh, 0ah, 'print the help message .'
DB 0dh, 0ah, '2. paratest/P'
DB 0dh, 0ah, 'print the Test message. ',' $'
Parameter DB 2 DUP (?)
End start