How to pass/retrieve DOS command-line parameters in a 16-bit assembly program?

不问归期 提交于 2019-12-03 08:07:13

There is no specific API to retrieve the command-line in MS-DOS. Instead, you have to read the value from the appropriate offset of the Program Segment Prefix (PSP), which is a data structure that DOS uses to store program-specific data.

At offset 80h, there is a 1-byte value that gives the length of the command-line arguments. The actual command-line argument string starts at offset 81h, and can be up to 127 bytes in length. You know how long it is based on the value at offset 80h, but it will also be terminated with a carriage return (0Dh).

You can use these offsets relative to the pointer in the DS register when the program is first executed. Otherwise, you call INT 21h with AH set to 62h to retrieve a pointer to the current PSP in the BX register. (Function 62h requires DOS 3 or later; on DOS 2, you can use the undocumented function 51h).

The old, 16-bit DOS version of Randall Hyde's Art of Assembly is available for free online (in HTML and PDF formats). In Chapter 13, section 13.3.11 describes the PSP, and the following two sections (13.3.12–13) explain how to access and parse command-line parameters, including sample code.

According to this site, the length of the command-line is stored at DS:80h (single byte), and the actual command line itself starts at DS:81h. Here's some example code from that article that prints the command line:

; ----------------------------------------------------------------------------
; echo.asm
;
; Echoes the command line to standard output.  Illustrates DOS system calls
; 40h = write to file, and 4ch = exit process.
;
; Processor: 386 or later
; Assembler: MASM
; OS: DOS 2.0 or later only
; Assemble and link with "ml echo.asm"
; ----------------------------------------------------------------------------

        .model  small
        .stack  64                      ; 64 byte stack
        .386
        .code
start:  movzx   cx,byte ptr ds:[80h]    ; size of parameter string
        mov     ah, 40h                 ; write
        mov     bx, 1                   ; ... to standard output
        mov     dx, 81h                 ; ... the parameter string
        int     21h                     ; ... by calling DOS
        mov     ah, 4ch
        int     21h
        end     start             
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!