更改字符串(文件扩展名)

时间:2017-07-11 20:16:33

标签: assembly masm x86-16

我是新来的。这是交易: 我需要读取文件名,然后验证它是否以“.IN”结尾,如果没有,我需要添加它以打开文件。之后,我需要创建一个文件名相同的文件,但以“.OUT”结尾,替换“.IN”

GetFileName proc    near
    lea     bx,MsgEnterFilename
    call    printf_s ;It prints the string above
    mov     ah,0ah
    lea     dx,FileNameBuffer
    mov     byte ptr FileNameBuffer,100
    int     21h
    lea     si,FileNameBuffer+2
    lea     di,FileName
    mov     cl,FileNameBuffer+1
    mov     ch,0
    mov     ax,ds
    mov     es,ax
    rep     movsb
    mov     byte ptr es:[di],0 ;Puts a '\0' at the end
    ret
GetFileName endp

fileNameInfo proc near ;Puts ".IN" and ".OUT"
    lea     si, FileName
    lea     di, FileNameCopy
    mov     cx, MAXSTRING ; MAXSTRING is set to 256, yeah, I am copying beyond the string end
    rep movsb 

    ; Search for ".IN"
    mov     si, 0

start_filename_length:                
    mov     al, FileNameCopy[si]  

    cmp     al, 0
    je      end_filename_length

    inc     FileNameLength
    inc     FileNameLengthAux

    add     si, 1
    jmp     start_filename_length
end_filename_length:

    sub     FileNameLengthAux, 2 ; To begin the test in the last 3 bytes (".IN")
    mov     si, word ptr FileNameLengthAux
    cmp     FileNameCopy[si], "."
    je      test_i
    jmp     no_extension
test_i:
    inc     si
    mov     si, word ptr FileNameLengthAux
    cmp     FileNameCopy[si], "I"
    je      test_n
    cmp     FileNameCopy[si], "i"
    je      test_n
    jmp     no_extension
test_n:
    inc     si  ; last byte
    mov     si, word ptr FileNameLengthAux
    cmp     FileNameCopy[si], "N" 
    je      correct_extension
    cmp     FileNameCopy[si], "n"
    je      correct_extension
    ;jmp    no_extension

no_extensao: ;wrong extension counts as no extension
    inc     FileNameLengthAux       ;goes to '\0' position
    mov     si, word ptr FileNameLengthAux
    mov     FileNameCopy[si], "."
    inc     si
    mov     FileNameCopy[si], "O"
    inc     si
    mov     FileNameCopy[si], "U"
    inc     si
    mov     FileNameCopy[si], "T"
    inc     si
    mov     FileNameCopy[si], 0 ;End the string

    lea     si, FileNameCopy
    lea     di, FileNameOut
    mov     cx, MAXSTRING ; copy 256 bytes
rep movsb

    jmp     return_filename_info

correct_extension: ;copyies till the "."
    mov     bl, FileNameLengthAux
    mov     FileNameLength, bl
    sub     FileNameLength, 2
    lea     si, FileNameCopy
    lea     di, FileNameOut
    mov     ch, 0
    mov     cl, FileNameLengthAux
rep movsb
    mov     si, word ptr FileNameLengthAux ; it is on "." position
    inc     si
    mov     FileNameOut[si], "O"
    inc     si
    mov     FileNameOut[si], "U"
    inc     si
    mov     FileNameOut[si], "T"
    inc     si
    mov     FileNameOut[si], 0 ;End the string
    ;jmp    return_filename_info

return_filename_info:

    ret
fileNameInfo endp

感谢帮助= D

2 个答案:

答案 0 :(得分:1)

sub     FileNameLengthAux, 2 ; To begin the test in the last 3 bytes (".IN")

这是着名的关闭一个错误!您需要减去 3 通过示例验证这一点。如果文件名为“A.IN”,则长度为4,您将需要开始查找“。”。偏移SI=1处的字符。算一算:4 - 3 = 1。

test_i:
    inc     si
    mov     si, word ptr FileNameLengthAux

为什么要销毁SI的<正确]递增值?

test_i:
    inc     si
    mov     si, word ptr FileNameLengthAux

这里也有同样的问题。

mov     bl, FileNameLengthAux
mov     FileNameLength, bl
sub     FileNameLength, 2

您如何期望这给出正确的最终 FileNameLength
FileNameLengthAux 已经小于原始长度(你减去2),在这里你做得更小(减去另外2个)!
所需要的只是返回一个比你开始时更长 1 的长度。

如果您的程序没有找到扩展名(“。IN”),则根本不更新 FileNameLength 。只需添加 4 ,因为这是附加“.OUT”的长度。

答案 1 :(得分:0)

电话是主程序PS:对不起,评论是葡萄牙语

EventType.DomainID