2016-04-23 8 views
2

私はコンソールプログラムを作っていますが、私のファイルプロシージャはファイル名が必要ですが、私はユーザー入力からファイル名を取得したいので、あなたはファイル名に.txtを含む最大15文字まで書くことができますが、私のコードは動作していないようです。どのように文字列か、この変数filename db "filename.txt",0にユーザ入力からファイル名を得ることができますか? (例えば)どのように私はDOSからのユーザー入力からファイル名を得ることができます

私のOpenFileのPROC:

proc OpenFile 
;Open file 
    mov ah,3Dh 
    xor al,al 
    lea dx,[filename] 
    int 21h 
    jc openerror 
    mov [filehandle],ax 
    ret 
openerror: 
    mov dx,offset ErrorOpenMsg 
    mov ah,9h 
    int 21h 
    ret 
endp OpenFile 

ORIGINALフルコード:

IDEAL 
MODEL small 
STACK 100h 
DATASEG 

szMsg1 db "Hi! What do you want to do?",10,13,10,13,"/h-help(see all the commands)",10,13,"/e-Exit",10,13,10,13,"$" 
szHelloWorld db 10,13,"Hello World!",10,13,"$" 
ErrorMsg db 10,13,"Illegal Command,Try again!",10,13,"$" 
filenameStr db 15 dup(?),0 
help db 10,13,"HELP LIST:",10,13,"-----------",10,13,"Commands are:",10,13," /e-Exit",10,13," /h-help",10,13," /1-Says: 'Hello World!'",10,13,"$" 
filename db ?,0 
filehandle dw 0 
ErrorOpenMsg db 'Error',10,13,'$' 
FileNameLength db "file name consists of 8 letters max! (dont forget to add '.txt' at the end of the name: 'example.txt')",10,13,"/r/ ","$" 
fileString db 255 dup (0) 
space db " ","$" 
CommandMsg db 10,13,"Enter your command:",10,13,"Command: ","$",10,13 
string db ? 

CODESEG          
proc OpenFile 
;Open file 
    mov ah,3Dh 
    xor al,al 
    lea dx,[filenameStr] 
    int 21h 
    jc openerror 
    mov [filehandle],ax 
    ret 
openerror: 
    mov dx,offset ErrorOpenMsg 
    mov ah,9h 
    int 21h 
    ret 
endp OpenFile 

proc ReadFile 
    mov ah,3fh 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[string] 
    int 21h 
    ret 
endp ReadFile 

proc DeleteFile 
    mov ah,41h 
    lea dx,[filename] 
    int 21h 
endp DeleteFile 

proc DisplayFileString 
    mov ah,09h 
    lea dx,[fileString] 
    int 21h 
endp DisplayFileString 

proc KeyStroke 
    xor ax,ax 
    int 16h 
endp KeyStroke 

proc WriteToFile 
    mov ah,40h 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[fileString] 
    int 21h 
    ret 
endp WriteToFile 

proc CloseFile 
    mov ah,3Eh 
    mov bx,[filehandle] 
    int 21h 
    ret 
endp CloseFile 

start: 
    mov ax, @data 
    mov ds, ax 

    mov dx,offset szMsg1 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

_Error: 

    mov dx,offset ErrorMsg 
    mov ah,9h 
    int 21h 

GetCommandLetter: 
    mov dx,offset CommandMsg 
    mov ah,9h 
    int 21h 

    mov ah, 1h 
    int 21h 
    mov bl,al 

    mov ah, 1h 
    int 21h 
    mov bh,al 
compare:  
    cmp bl,'/' 
    jne _Error 
    cmp bh,'e' 
    je _exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'E' 
    je exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'h' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'H' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'1' 
    je PrintLine 

    mov dx,offset space 
    mov ah,9h 
    int 21h 
    mov dx,offset FileNameLength 
    mov ah,9h 
    int 21h 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'r' 
    je GetFileName 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'R' 
    je GetFileName 

    jmp _Error 
_exit: 
    jmp exit  

GetFileName: 
    mov dx,offset space 
    mov ah,9h 
    int 21h 

    mov dx,offset filenameStr 
    mov bx,dx 
    mov [byte ptr bx],15 
    mov ah,0Ah 
    int 21h 
    mov dx,offset filenameStr 
    mov ah,9h 
    int 21h 
    call OpenFile 
    call ReadFile 
    call CloseFile 
    jmp GetCommandLetter 



_help: 
    mov dx,offset help 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

PrintLine: 
    mov dx, offset szHelloWorld 
    mov ah, 9h 
    int 21h 
    jmp GetCommandLetter 

exit: 
    mov ax, 4c00h 
    int 21h 
END start 



    mov [filehandle],ax 
    ret 
openerror: 
    mov dx,offset ErrorOpenMsg 
    mov ah,9h 
    int 21h 
    ret 
endp OpenFile 

proc ReadFile 
    mov ah,3fh 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[string] 
    int 21h 
    ret 
endp ReadFile 

proc DeleteFile 
    mov ah,41h 
    lea dx,[filename] 
    int 21h 
endp DeleteFile 

proc DisplayFileString 
    mov ah,09h 
    lea dx,[fileString] 
    int 21h 
endp DisplayFileString 

proc KeyStroke 
    xor ax,ax 
    int 16h 
endp KeyStroke 

proc WriteToFile 
    mov ah,40h 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[fileString] 
    int 21h 
    ret 
endp WriteToFile 

proc CloseFile 
    mov ah,3Eh 
    mov bx,[filehandle] 
    int 21h 
    ret 
endp CloseFile 

start: 
    mov ax, @data 
    mov ds, ax 

    mov dx,offset szMsg1 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

_Error: 

    mov dx,offset ErrorMsg 
    mov ah,9h 
    int 21h 

GetCommandLetter: 
    mov dx,offset CommandMsg 
    mov ah,9h 
    int 21h 

    mov ah, 1h 
    int 21h 
    mov bl,al 

    mov ah, 1h 
    int 21h 
    mov bh,al 
compare:  
    cmp bl,'/' 
    jne _Error 
    cmp bh,'e' 
    je _exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'E' 
    je exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'h' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'H' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'1' 
    je PrintLine 

    mov dx,offset space 
    mov ah,9h 
    int 21h 
    mov dx,offset FileNameLength 
    mov ah,9h 
    int 21h 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'r' 
    je GetFileName 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'R' 
    je GetFileName 

    jmp _Error 
_exit: 
    jmp exit  

GetFileName: 
    mov dx,offset space 
    mov ah,9h 
    int 21h 

    mov dx,offset filenameStr ;setting letters into string from input 
    mov bx,dx 
    mov [byte ptr bx],15 
    mov ah,0Ah 
    int 21h 
    mov dx,offset filenameStr ;output of the string,string input supposidly bad 
    mov ah,9h 
    int 21h 
    call OpenFile 
    call ReadFile 
    call CloseFile 
    jmp GetCommandLetter 



_help: 
    mov dx,offset help 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

PrintLine: 
    mov dx, offset szHelloWorld 
    mov ah, 9h 
    int 21h 
    jmp GetCommandLetter 

exit: 
    mov ax, 4c00h 
    int 21h 
END start 

NEWフルコード:

IDEAL 
MODEL small 
STACK 100h 
DATASEG 

szMsg1 db "Hi! What do you want to do?",10,13,10,13,"/h-help(see all the commands)",10,13,"/e-Exit",10,13,10,13,"$" 
szHelloWorld db 10,13,"Hello World!",10,13,"$" 
ErrorMsg db 10,13,"Illegal Command,Try again!",10,13,"$" 
filenameStr db 13,0,13 dup("$") 
help db 10,13,"HELP LIST:",10,13,"-----------",10,13,"Commands are:",10,13," /e-Exit",10,13," /h-help",10,13," /1-Says: 'Hello World!'",10,13,"$" 
filename db ?,0 
filehandle dw 0 
ErrorOpenMsg db 'Error',10,13,'$' 
FileNameLength db "file name consists of 8 letters max! (dont forget to add '.txt' at the end of the name: 'example.txt')",10,13,"/r/ ","$" 
fileString db 255 dup (0) 
space db " ","$" 
CommandMsg db 10,13,"Enter your command:",10,13,"Command: ","$",10,13 
string db ? 

CODESEG          
proc OpenFile 
;Open file 
    mov ah,3Dh 
    xor al,al 
    lea dx,[filenameStr+2] 
    int 21h 
    jc openerror 
    mov [filehandle],ax 
    ret 
openerror: 
    mov dx,offset ErrorOpenMsg 
    mov ah,9h 
    int 21h 
    ret 
endp OpenFile 

proc ReadFile 
    mov ah,3fh 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[fileString] 
    int 21h 
    ret 
endp ReadFile 

proc DeleteFile 
    mov ah,41h 
    lea dx,[filenameStr+2] 
    int 21h 
endp DeleteFile 

proc DisplayFileString 
    mov ah,09h 
    lea dx,[fileString] 
    int 21h 
endp DisplayFileString 

proc KeyStroke 
    xor ax,ax 
    int 16h 
endp KeyStroke 

proc WriteToFile 
    mov ah,40h 
    mov bx,[filehandle] 
    mov cx,255 
    lea dx,[fileString] 
    int 21h 
    ret 
endp WriteToFile 

proc CloseFile 
    mov ah,3Eh 
    mov bx,[filehandle] 
    int 21h 
    ret 
endp CloseFile 

start: 
    mov ax, @data 
    mov ds, ax 

    mov dx,offset szMsg1 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

_Error: 

    mov dx,offset ErrorMsg 
    mov ah,9h 
    int 21h 

GetCommandLetter: 
    mov dx,offset CommandMsg 
    mov ah,9h 
    int 21h 

    mov ah, 1h 
    int 21h 
    mov bl,al 

    mov ah, 1h 
    int 21h 
    mov bh,al 
compare:  
    cmp bl,'/' 
    jne _Error 
    cmp bh,'e' 
    je _exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'E' 
    je exit 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'h' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'H' 
    je _help 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'1' 
    je PrintLine 

    mov dx,offset space 
    mov ah,9h 
    int 21h 
    mov dx,offset FileNameLength 
    mov ah,9h 
    int 21h 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'r' 
    je GetFileName 

    cmp bl,'/' 
    jne _Error 
    cmp bh,'R' 
    je GetFileName 

    jmp _Error 
_exit: 
    jmp exit  

GetFileName: 
    mov dx,offset space 
    mov ah,9h 
    int 21h 
    mov dx,offset filenameStr 
    mov bx,dx 
    mov [byte ptr bx],13 ;8+1+3+1 
    mov ah,0Ah 
    int 21h 
    mov dx,offset filenameStr + 2 
    mov ah,9h 
    int 21h 


    mov byte ptr [filenameStr+2+8],0 


    call OpenFile 
    call ReadFile 
    call CloseFile 
    jmp GetCommandLetter 



_help: 
    mov dx,offset help 
    mov ah,9h 
    int 21h 
    jmp GetCommandLetter 

PrintLine: 
    mov dx, offset szHelloWorld 
    mov ah, 9h 
    int 21h 
    jmp GetCommandLetter 

exit: 
    mov ax, 4c00h 
    int 21h 
END start 
+0

オフセット:私はそれをこのように解決しましたあなたはどこかからその名前を得なければなりません。どこでどのように 'filename'変数を埋めていますか?デバッガに何が含まれているかチェックしましたか?コード内にはありません... –

+1

[dos asmのユーザー入力からファイル名を取得するにはどうすればいいですか?](http://stackoverflow.com/questions/36816865/how-can-i-get-file-name -from-ユーザ入力-で-DOS-ASM) –

+0

@Seva Alekseyev私は、全体のコード – Warrior0201

答えて

1
filenameStr db 15 dup(?),0 
filename db ?,0 

あなたはあなたの問題に対処するこれら2つの定義を持っています。 2番目のファイル(ファイル名)は小さすぎて使用できないため、deleteFileの手順は失敗します。最初の文字(filenameStr)は、DOS入力機能の入力構造を意図しています。これは十分に大きいですが、最初のバイトを15より小さい値に初期化する必要があります。これは、8文字+ 1ドット+ 3文字+キャリッジリターンが可能になるため、13を推奨します。

mov dx,offset filenameStr 
mov bx,dx 
mov byte ptr [bx],13 ;8+1+3+1 
mov ah,0Ah 
int 21h 

あなたは$ドル記号とそれを終了し、実際のテキストはで、この構造内に2をオフセット開始することを認識する必要があり、このテキストを表示したいです。正常にファイルを開くには

filenameStr db 13,0,13 dup("$") 


mov dx,offset filenameStr + 2 
mov ah,9h 
int 21h 

あなたが最初のゼロでキャリッジリターンを交換し、再びテキストがから始まることに注意する必要がありますファイルを開く前に2

+0

を追加しましたあなたが言ったように私はGetFileNameは、コードを変更した: \tのMOV DX、オフセットスペース \tのmovああ、9Hを \t INT 21hを \tのMOV DX、filenameStr \tのMOV BXオフセット、DX \t MOV [バイトのPTR BX]、15 \t MOVああ、0AH \t INT 21hを \tのMOV DX、filenameStr \t私がすることになっていたところ、しかし、それはわからないんだけど \t呼び出しのReadFile \tコールCLOSEFILE \t JMP GetCommandLetterのOpenFileのmovああ、9H \tのint 21hを \tコールをオフセットそれを変更?変数filenameStrのコードをfilenameStr db 13,0,13 dup( "$")に変更するはずですか?私がプログラムを起動したときに、ファイルを読みたいときにエラーが発生したので、 – Warrior0201

+0

このコメントに書いたコードは、変更が必要なコードです。そして、はい、また、上場の定義を高くします。まだ読んでいるときにエラーが発生したのは、私が最後の段落で示したような* OpenFile *プロシージャーを修正しなかったからでしょう。 –

+0

コードを変更しました。質問に新しいフルコードをアップロードしました。正しく変更しましたか? – Warrior0201

関連する問題