The assembly write the number to the file

advertisements

I try to write number to file via 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

It doesn´t work. I have tried to change push number for push offset number but it doesnt´work too.Any idea how to write number to file?


Your code has a couple of problems, it doesn't check for errors, and if you want to write a number in "text format", you need to make it a string, like this: number DB '101',0

Any idea how to write number to file?

You should be able to fix your code, you're almost there. Here's some code I wrote a long time ago if you want to take a look. It's MASM32 asm and makes use of some macros. invoke is just like pushing and calling by hand, but in one line.

; 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