2016-04-24 70 views
2

當我正在讀取和獲取文件的數據,並試圖讀取和獲取另一個文本文件的數據,它正在使用的字符串 新的數據,例如,字符串包含「哦是啊!「,得到另一個文件的數據」heyyeah!「。 (顯然,我讀的第二個文件中包含嘿這個詞)。在顯示第一個文件的數據後,我想在讀取第二個文件時從第一個文件獲取的數據中清除字符串。我怎樣才能清除字符串[dos asm]

全碼:

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 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,'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 
    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 
+3

您用於打印字符串(INT 21h,AH = 9)的MS-DOS函數僅打印字符串中的字符,直到找到第一個美元符號字符(''$'')。當打印文件的內容時,除非讀入文件的一部分中有'$'字符,否則它發現的第一個'$'符號實際上位於您命名爲'space'的字符串中,它跟隨着緩衝區將文件讀入。如果您修復該錯誤,您將解決您在帖子中描述的問題。 –

+0

我的意思是,我有2個文件,一個(讓我們稱之爲bova.txt)有'啊是啊!'在它中,secod文件(讓它稱爲help.txt)裏有'hey',我正在調用bova.txt的閱讀函數,並且它設置'ah yeah!'在字符串和比顯示字符串,比,我打電話給help.txt的閱讀功能,而不是隻有'嘿'在字符串中,它有'heyyeah!',這意味着它踩在字母相同的3個地方它需要,並用第二個文件中的單詞的字母替換它們,我想要的是從文本 – Warrior0201

回答

1

如果你的程序運行正常的「bova.txt」那麼你會得到類似的結果第二「help.txt」文件,如果你只是清理緩衝液標籤fileString。

call OpenFile 

xor bx,bx 
WipeClean: 
    mov [byte ptr fileString + bx], 0 
    inc bx 
    cmp bx, 255 
    jb WipeClean 

call ReadFile 
mov dx, offset filereaderline 
mov ah, 09h 
int 21h 
call DisplayFileString 
call CloseFile 
jmp GetCommandLetter 
+1

顯示文字後清理字符串,謝謝!它工作完美! – Warrior0201

相關問題