2014-11-24 38 views
0

我需要檢查是否給出了大量的參數。目前我的程序知道什麼時候沒有參數,什麼時候有兩個以上的參數。如果給出了一個,那麼它是不好的,但我的程序不會識別它。我不能做簡單的檢查,因爲在允許的時候有一種可能(當/?是參數時),並且cx隨着循環而改變。例如,如果我把它叫做prog /?,它應該打印說明,當我把它叫做prog arg1 arg2時,它應該被允許,但是prog arg1不應該。我如何檢查?當cx被修改時TASM檢查參數

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 
+0

這很容易,只是檢查你沒有找到2 /參數後,你有2個參數。 – Jester 2014-11-24 14:21:24

+0

但mycx被改變,我不知道如何得到它比較,推動CX,然後彈出不起作用:( – Tomas 2014-11-24 15:04:11

+0

推動和彈出應該工作,以及從內存中重新加載(或者甚至只是使用' cmp'與內存操作數) – Jester 2014-11-24 15:05:35

回答

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 
+0

我的TASM 3.1在我的uni中是必需的,它不理解get_argc。 – Tomas 2014-11-25 14:15:04

+0

@Tomas:TASM 2.01,3.0,3.2,4.0:沒有問題,但我稍微改變了代碼(刪除了'.386'-directive) – rkhb 2014-11-25 16:19:33

+0

LOCALS @@實際上做了什麼?我複製了call get_argc; cmp ax, 22; jne print_description。編譯器說:「未定義的符號:get_argc」 – Tomas 2014-11-25 21:15:41