修改cx时TASM检查参数

时间:2014-11-24 12:57:37

标签: assembly x86 command-line-arguments dos tasm

我需要检查是否给出了大量的论据。目前我的程序知道什么时候没有参数,什么时候有两个以上的参数。如果给出一个,那么它很糟糕,但我的程序不能识别它。我不能做简单的检查,因为有一种可能性(当/?是参数时)和cx随循环变化。例如,如果我将其称为prog / ?,它应该打印描述,当我称之为prog arg1 arg2时,应该允许它,但是prog arg1不应该。我怎么检查呢?

  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 个答案:

答案 0 :(得分:1)

两个错误:

1)我的TASM 5.0不喜欢[ES:xxxx]。段覆盖必须写为ES:[xxxx]

2)ES:[0080h]包含命令行的长度,参数的计数。因此,这句话按照您的想法行事:

push cx
...
pop cx
cmp cx, 2
jne print_description



您可以使用我的get_argc功能。以下测试用例(希望如此):

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