Simas Pruselaitis
Simas Pruselaitis

Reputation: 23

How to read command line arguments in assembly language?

I'm using the TASM assembler and I can't seem to find a way to save my command line arguments or even output them. I tried doing int 21h while dx=0081h and ah=09h. It doesn't do anything although I've read that it is the way to do it.

I've added the code of what I think should work to copy and display an argument I type in:

mov dx, 81h
mov ah, 09h
int 21h

Upvotes: 2

Views: 5181

Answers (1)

rkhb
rkhb

Reputation: 14409

The command line resides in the Program Segment Prefix (PSP), and there from position 81h. At position 80h you find the length of the string. This string always ends with a 0Dh, not $ (Pay attention for that if you want to use int 21h/ah=09h).

At the start of an .exe program the segment registers DS and ES point to that PSP. This is the reason why you have to load DS at the beginning of the program with the DATA segment (mov ax, @data;mov ds, ax). Let's skip that one:

MODEL small
.STACK 100h

.CODE

main PROC

    ; http://www.ctyme.com/intr/rb-2791.htm
    mov ah, 40h         ; DOS 2+ - WRITE - WRITE TO FILE OR DEVICE
    mov bx, 1           ; File handle = STDOUT
    xor ch, ch
    mov cl, ds:[0080h]  ; CX: number of bytes to write
    mov dx, 81h         ; DS:DX -> data to write (command line)
    int 21h             ; Call MSDOS

    ; http://www.ctyme.com/intr/rb-2974.htm
    mov ax, 4C00h       ; AH=4Ch, AL=00h -> exit (0)
    int 21h             ; Call MSDOS
main ENDP

END main

Upvotes: 7

Related Questions