部件 。我怎样才能通过一个字符串,写出文本文件中的特殊字符、数字和字母?

ASSEMBLY . How can I go through a string, writing out the special characters, numbers and letters in a text file?

这是我在 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 ```

当文件被打开时,而不是写入整个 text 你应该一个一个地检查它的字符,在必要的时候修改它,存储字符后跟 NUL(使它成为一个零终止的字符串fprintf) 要求,然后写入此字符串。而不是

    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