2016-04-23 8 views
2

Ich mache ein Konsolenprogramm, und meine Dateiprozeduren benötigen Dateiname, obwohl ich den Dateinamen von Benutzereingabe erhalten möchte, wenn Sie also in der Konsole/r schreiben, Sie können maximal 15 Buchstaben schreiben, die die .txt im Dateinamen enthalten, aber es sieht so aus, als ob mein Code nicht funktioniert, wie kann ich den Dateinamen von einer Benutzereingabe in eine Zeichenkette oder zu dieser Variablen filename db "filename.txt",0 bekommen?Wie bekomme ich Dateinamen von Benutzereingabe in DOS asm

meine Openfile proc (zum Beispiel):

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-Full-Code:

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-Full-Code:

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

Offset beginnt, bevor Sie eine Datei öffnen Du musst seinen Namen irgendwo her bekommen. Wo und wie füllt man die Variable 'filename'? Haben Sie den Debugger eingecheckt, was enthält? Es ist nicht im Code ... –

+1

Mögliches Duplikat von [wie kann ich den Dateinamen von Benutzereingabe in DOS asm] (http://stackoverflow.com/questions/36816865/how-can-i-get-file-name -von-user-input-in-dos-asm) –

+0

@Seva Alekseyev ich den ganzen Code hinzugefügt haben – Warrior0201

Antwort

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

Sie haben diese zwei Definitionen, die sich mit Ihrem Problem befassen. Die zweite (Dateiname) ist zu klein, um von irgendwelchen Nutzen zu sein, und deshalb wird Ihre deleteFile Prozedur fehlschlagen! Die erste (DateinameStr) ist die Eingabestruktur für die DOS-Eingabefunktion. Das ist groß genug, aber Sie müssen sein erstes Byte auf etwas kleiner als 15 initialisieren, ich schlage 13 vor, weil das 8 Zeichen + 1 Punkt + 3 Zeichen + Wagenrücklauf erlaubt.

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

Wenn Sie diesen Text, den Sie mit einem $ Dollar-Zeichen, und beachten Sie beenden müssen angezeigt werden, dass der eigentliche Text bei Offset beginnt 2 innerhalb dieser Struktur. Ich löste es auf diese Weise:

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


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

Zum Öffnen der Datei erfolgreich müssen Sie zunächst durch ein Null den Wagenrücklauf ersetzen und wieder bewusst sein, dass der Text am 2.

+0

ich den Code geändert haben, wie Sie gesagt haben, GetFileName: \t mov dx, Offset-Raum \t mov ah, 9h \t int 21h \t mov dx, Offset- filenameStr \t mov bx, dx \t mov [byte ptr bx], 15 \t mov ah, 0Ah \t int 21h \t mov dx, Offset filenameStr \t mov ah, 9h \t int 21h \t Aufruf Openfile \t Anruf ReadFile- \t Aufruf Closefile \t jmp GetCommandLetter i aber nicht sicher bin, ist es, wo ich angeblich zu ändern Sie es? und soll ich den code der variablen filenameStr zu filenameStr db 13,0,13 dup ("$") ändern?seit ich das Programm gestartet habe, sagte es Fehler, wenn ich eine Datei lesen wollte – Warrior0201

+0

Der Code, den Sie in diesem Kommentar geschrieben haben, ist der Code, der geändert werden muss. Und ja, auch die Definition oben in der Auflistung. Wenn Sie beim Lesen immer noch einen Fehler bekommen, liegt das wahrscheinlich daran, dass Sie die Prozedur * OpenFile * nicht wie in meinem letzten Absatz beschrieben korrigiert haben. –

+0

Ich habe den Code geändert, und ich habe den neuen vollständigen Code in der Frage hochgeladen, habe ich es richtig geändert? – Warrior0201

Verwandte Themen