2016-04-25 15 views
0

コンソールにファイルの名前を入力するときにファイルを作成するプロシージャを作成したいのですが、どうすればいいですか?ユーザ入力[asm、dos]からファイルを書き込む方法は?

filenameStr db 13,0,13 dup("$") 
createline db 10,13,"/c/ ","$" 
space db " ","$" 
filehandle dw 0 

PROC:

proc Createfile 
mov dx,offset createline 
mov ah,9h 
int 21h 
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 
xor bx,bx 
call WipeClean 

call CreateFile1 
call CloseFile 
call GetCommandLetter 
endp CreateFile 

と:

proc CreateFile1 
    mov ah,3ch 
    mov cx,00000000b 
    lea dx,[filenameStr] 
    int 21h 
    mov [filehandle],ax 
    ret 
endp CreateFile1 

注:私がしたので、これらの手順は、コードに示したされていない私は、これらの二つの手順で試してみました書き込みファイルの手続きなしでコードを私の古いコードに変更しました。

完全なプログラムはこちらです:

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 
filereaderline db "file's text:","$" 


CODESEG  
proc WipeClean 
    mov [byte ptr fileString + bx], 0 
    inc bx 
    cmp bx, 255 
    jb WipeClean 
endp WipeClean 

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 ah,0 
    mov al,2 
    int 10h 

    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,'h' 
    je _help 

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

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

    cmp bl,'/' 
    jne _Error 
    cmp bh,'e' 
    je _exit 

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


    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 

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

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 
    xor bx,bx 
    call WipeClean 
    call ReadFile 
    mov dx,offset filereaderline 
    mov ah,9h 
    int 21h 
    call DisplayFileString 
    call CloseFile 
    jmp GetCommandLetter 





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

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

このコードは非常に難しいです。 'call'と' jmp/je/jne'を使うことの違いを理解していますか?そしてルーチンの終わりに当たったときに、何が_Errorのようなルーチンで起こると思いますか? –

+0

私はコードを変更しました。私がその質問で示した手順は古いコードなので、もう存在しないことに注意してください。あなたが今それに従うことを願って – Warrior0201

+0

それで、あなたがそれを実行するとどうなりますか?私は '/ h'が期待どおりに動作すると仮定します。しかし、あなたが/ rをするとどうなりますか?私はそれが名前のためにあなたを促し、あなたがそれを入力することを推測しています。今入力した名前を印刷することになっているようです。それは動作しますか?まさにあなたはその名前のために使っていますか?そして、この行が 'mov [byte ptr filenameStr + 2 + 8]、0'をどう思いますか?具体的には、 '8 'はどういう意味ですか? OpenFileはエラーを出力していますか?それがWipeCleanにもたらされます。あなたは 'call'を使ってそれを起動します、そして、それはすべての255バイトを処理した後にprocの最後で何が起こりますか? –

答えて

1

DOS APIを使用すると、のCreateFileのOpenFileは非常に類似した機能していることがわかりますを読みます。あなたのプログラムではあなたが正常にオープンしましたが、あなたが@SepRolandがあなたを助けたときと同じ過ちをしたため、作成に失敗しました。実際のファイル名は、DOSに与えた構造のオフセット2で始まることを忘れてしまいました。

proc CreateFile1 
    mov ah,3ch 
    mov cx,00000000b 
    lea dx,[filenameStr +2]    <= Add the 2 here! 
    int 21h 
    jc ThisIsAnErrorOnCreatingAFile <= Do something with the carry flag! 
    mov [filehandle],ax 
    ret 
endp CreateFile1 
関連する問題