我尝试通过Assembler将数字写入文件
Include Irvine32.inc
.data
fileName DB 'number.txt',0
FileHandle DD ?
numberBytes DD ?
number DD 101
numberChar DD $-cislo
.code
WriteToFileP proc
;create file
push NULL
push FILE_ATTRIBUTE_NORMAL
push CREATE_ALWAYS
push NULL
push 0
push GENERIC_WRITE
push offset fileName
call CreateFileA
mov FileHandle,eax
;write
push NULL
push offset numberBytes
push numberChar
push number
push FileHandle
call WriteFile
; close file
push FileHandle
call CloseHandle
ret
WriteToFileP ENDP
end
它不起作用。我试图为push number
更改push offset number
但它也没有工作。不知道如何将数字写入文件?
答案 0 :(得分:1)
您的代码存在一些问题,它不会检查错误,如果您想以“文本格式”编写数字,则需要将其设为字符串,如下所示:number DB '101',0
< / p>
知道如何将号码写入文件吗?
你应该能够修复你的代码,你几乎就在那里。
这是我很久以前写的一些代码,如果你想看看。它是MASM32 asm并使用了一些宏。 invoke
就像用手推动和呼叫一样,但是在一行中。
; Opens and writes data to a file, will create it if needed
; path must be a valid cstring path
; data and dataSize must describe a readable chunk of memory
; Returns 0 on sucess and -1 on error
WriteFileNohandle proc path:ptr, data:ptr, dataSize:dword
invoke CreateFile, path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
cmp eax, INVALID_HANDLE_VALUE
je openFail
push ebx
mov ebx, eax
sub esp, 4
mov edx, esp
invoke WriteFile, ebx, data, dataSize, edx, 0
add esp, 4
test eax, eax
jz fail
ok:
invoke CloseHandle, ebx
xor eax,eax
done:
pop ebx
ret
fail:
invoke CloseHandle, ebx
openFail:
mov eax, -1
jmp done
WriteFileNohandle endp
答案 1 :(得分:0)
汇编程序已经转换了小数&#34; 101&#34;在行number DD 101
到内部CPU格式&#34; dword&#34;。 WriteFile
将该字节序列写入文件,编辑器将显示垃圾,因为它将其解释为字符序列。首先必须将双字转换为字符串。
WinApi包含一个可用作转换例程的函数:wsprintf
。由于Irvine32库声明了这个函数,因此可以在没有其他情况下使用它:
Include Irvine32.inc
IncludeLib Irvine32.lib
IncludeLib Kernel32.lib
IncludeLib User32.lib
.data
...
numberstring db 16 DUP (0)
numberChar DD 0
fmt db "%d",0
.code
WriteToFileP proc
;create file
push NULL
push FILE_ATTRIBUTE_NORMAL
push CREATE_ALWAYS
push NULL
push 0
push GENERIC_WRITE
push offset fileName
call CreateFileA
mov FileHandle,eax
;convert number to string
push number ; Argument for format string
push offset fmt ; Pointer to format string ("%d")
push offset numberstring ; Pointer to buffer for output
call wsprintf ; Irvine32.inc / SmallWin.inc / User32.lib / User32.dll
mov numberChar, eax ; Length of the stored string
add esp, (3*4) ; CCALL calling function! Adjust the stack.
;write
push NULL
push offset numberBytes
push numberChar
push offset numberstring
push FileHandle
call WriteFile
; close file
push FileHandle
call CloseHandle
ret
WriteToFileP ENDP
end