问题
I need to check whether good amount of arguments is given. Currently my program knows when there are no arguments and when there are more than two arguments given. If one is given, then it is bad, but my program doesn't recognise it. I can't do simple check because there is one possibility when it is allowed (when /? is argument) and cx changes with loop. For example, if I call it prog /?, it should print description, when I call it prog arg1 arg2, it should be allowed, but prog arg1 shouldn't. How can I check that?
mov ch, 0h
mov cl, [es:0080h]
push cx
cmp cx, 0
je print_description
mov bx, 81h
jmp search_help
jmp exit
search_help:
cmp [es:bx], '?/'
je print_description
inc bx
loop search_help
pop cx
cmp cx, 2
jne print_description
mov bx, 82h
mov si, offset input_filename
mov cx, 255
search_input_filename:
mov dl, [es:bx]
inc bx
cmp dl, 20h
je search_output_filename_prep
mov ds:[si], dl
inc si
loop search_input_filename
search_output_filename_prep:
mov si, offset output_filename
search_output_filename:
mov dl, [es:bx]
inc bx
cmp dl, 0Dh
je program
cmp dl, 20h
je print_description
mov ds:[si], dl
inc si
loop search_output_filename
回答1:
Two errors:
1) My TASM 5.0 doesn't like [ES:xxxx]
. The segment override has to be written as ES:[xxxx]
.
2) ES:[0080h]
contains the length of the command line, not the count of the arguments. Thus this lines don't do what you think:
push cx
...
pop cx
cmp cx, 2
jne print_description
You can use my get_argc
function instead. Following test case works (hopefully):
LOCALS @@
.MODEL small
.STACK 1000h
.DATA
description db 'description',13,10,'$'
input_filename db 80 DUP ('$')
output_filename db 80 DUP ('$')
.CODE
main PROC
mov ax, @data
mov ds, ax
mov ch, 0h
mov cl, es:[0080h]
cmp cx, 0
je print_description
mov bx, 81h
jmp search_help
jmp exit
search_help:
cmp word ptr es:[bx], '?/'
je print_description
inc bx
loop search_help
call get_argc
cmp ax, 2
jne print_description
mov bx, 82h
mov si, offset input_filename
mov cx, 255
search_input_filename:
mov dl, es:[bx]
inc bx
cmp dl, 20h
je search_output_filename_prep
mov ds:[si], dl
inc si
loop search_input_filename
search_output_filename_prep:
mov si, offset output_filename
search_output_filename:
mov dl, es:[bx]
inc bx
cmp dl, 0Dh
je program
cmp dl, 20h
je print_description
mov ds:[si], dl
inc si
loop search_output_filename
program:
mov dx, OFFSET input_filename
mov ah, 09h
int 21h
call crlf
mov dx, OFFSET output_filename
mov ah, 09h
int 21h
exit:
mov ax, 4C00h
int 21h
print_description:
lea dx, description
mov ah, 09h
int 21h
mov ax, 4C01h
int 21h
main ENDP
get_argc PROC
mov bx, 0
mov di, 80h
mov cx, es:[di]
@@L1:
inc di
mov al, es:[di]
cmp al, 20h
je @@L1
cmp al, 09h
je @@L1
cmp al, 0Dh
je @@done
inc bx
@@L2:
inc di
mov al, es:[di]
cmp al, 20h
je @@L1
cmp al, 09h
je @@L1
cmp al, 0Dh
je @@done
jmp @@L2
@@done:
mov ax, bx
ret
get_argc ENDP
crlf PROC
mov ah, 2
mov dl, 13
int 21h
mov ah, 2
mov dl, 10
int 21h
ret
crlf ENDP
END main
来源:https://stackoverflow.com/questions/27105410/tasm-checking-arguments-when-cx-is-modified