【问题标题】:ASSEMBLY . How can I go through a string, writing out the special characters, numbers and letters in a text file?部件 。如何遍历字符串,在文本文件中写出特殊字符、数字和字母?
【发布时间】:2021-12-29 11:05:53
【问题描述】:

这是我将文本写入 txt 文件的方式。我使用了外部的 fclose、fopen、fprintf 函数。我的问题是如何分别引用每个字符,如果需要如何修改某些字符?

global start
extern exit, fopen, fclose, fprintf, printf  
import exit msvcrt.dll
import fclose msvcrt.dll
import fopen msvcrt.dll
import fprintf msvcrt.dll
import printf msvcrt.dll
segment data use32 class=data
    ; ...
    name_file db "newfile.txt",0
    descriptor_file dd -1
    mod_acces db "w",0
    text db "Every letter and n9mber should be written out separetely.",0
    error_message db "Something went wrong",0

segment code use32 class=code
    start:
        ;  ... eax= fopen(name_file,mod_acces)
        push dword mod_acces
        push dword name_file
        call [fopen]
        add esp, 4*2
        
        cmp eax, 0
        JE message_error
        
        mov [descriptor_file], eax
        ; eax = fprintf(descriptor_file, text)
        push dword text
        push dword [descriptor_file]
        call [fprintf]
        add esp,4*2
        
        push dword [descriptor_file]
        call [fclose]
        add esp,4
        
        jmp end
            
        message_error:
            push dword error_message
            call [printf]
            add esp,4
            
        end:
        
        ; exit(0)
        push    dword 0      ; push the parameter for exit onto the stack
        call    [exit]       ; call exit to terminate the program ```

【问题讨论】:

    标签: assembly x86 masm


    【解决方案1】:

    当文件打开时,而不是写整个text,你应该一个一个地检查它的字符,在必要时修改它,存储字符后跟NUL(使其成为@987654322所需的以零结尾的字符串@) 然后写这个字符串。而不是

        push dword text
        push dword [descriptor_file]
        call [fprintf]
        add esp,4*2
    

    使用这个:

             cld
             mov esi,text  
        Next:lodsb                   ; Copy one character addressed by esi to al. Increment esi.
             cmp al,0                ; Test if its the terminating NUL.
             je Close:               ; Close the file if so.
             call TestIfModificationIsRequired 
             mov [ModifiedChar],al   ; Store modified or unmodified character.
             push dword ModifiedChar ; Print the zero-terminated string with 1 character as usual.
             push dword [descriptor_file]
             call [fprintf]
             add esp,4*2
             jmp Next                ; Continue with the next character addressed by incremented esi.
        Close:
    

    您需要在segment data 中保留一个临时字符串

      ModifiedChar db '?',0
    

    【讨论】:

    • 哎呀,为每个字符单独调用 fprintf?至少使用fputc,它采用char arg。或者更好的是,循环整个缓冲区并就地修改您想要的任何字符,然后fwritefprintf 整个缓冲区。 (或者,如果您想丢弃一些,请创建一个您想要保留的字符的缓冲区。例如,读取位置和写入位置开始时相同,但只会增加您想要保留的字符的写入位置。所以你结束了在原始缓冲区的开头加上你想要的字符。)
    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 2012-02-06
    • 1970-01-01
    • 2014-12-09
    • 2018-09-29
    • 2019-12-01
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多