以下是我如何在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