這是我在 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 ```
uj5u.com熱心網友回復:
當檔案打開時,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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398685.html
上一篇:2021年,可謂是識訓滿滿,CRUD程式員的逆襲之路
下一篇:如何在MASM中定義嵌套結構?
