flat assembler
Message board for the users of flat assembler.
Index
> Windows > outputing to a text file |
Author |
|
tthsqe 30 Jun 2009, 22:38
How can I ouput a bunch of numbers computed in a program to a text file?
The text file should look like num1, num2, num3, ... |
|||
30 Jun 2009, 22:38 |
|
Picnic 30 Jun 2009, 23:19
Hi, tthsqe.
Format the numbers the way you like to a string buffer and then save it to file. Search for CreateFile, WriteFile, ReadFile e.t.c, You'll find many examples inside forum, here is something simple to get you started. Code: format pe console 4.0 include 'win32ax.inc' .data FileTitle db 'c:\fasm.txt',0 FileHandle dd ? BytesWritten dd ? .code main: invoke CreateFile, FileTitle, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 test eax, eax je ApiError mov [FileHandle], eax invoke WriteFile, [FileHandle], FileTitle, 11, BytesWritten, 0 test eax, eax je ApiError .exit: invoke CloseHandle, [FileHandle] invoke ExitProcess, 0 .end main ApiError: ; api error routine ; jmp main.exit Here is one written by shoorick, related to file usage. |
|||
30 Jun 2009, 23:19 |
|
tthsqe 01 Jul 2009, 00:54
That did work, but I still have some questions:
How do you do it for 64-bit programs? Where do you actually code the contents of the file in the program? (a buffer, maybe?) Could I append strings to the file one by one, instead of writing it all at once? |
|||
01 Jul 2009, 00:54 |
|
eskizo 01 Jul 2009, 02:00
Code:
push ebp
mov ebp, esp
why to do this? |
|||
01 Jul 2009, 02:00 |
|
LocoDelAssembly 01 Jul 2009, 02:11
Because the entry point is not a leaf function so to do this in "the right way", I use that prologue sequence, and before calling ExitProcess I execute leave. If you remove "push ebp" and "leave", and change "lea edi, [ebp-4]" with "mov edi, ebp", the code will work OK too, but just to be safe I better leave the code as it is now.
|
|||
01 Jul 2009, 02:11 |
|
bitRAKE 01 Jul 2009, 04:04
tthsqe wrote: That did work, but I still have some questions: _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
01 Jul 2009, 04:04 |
|
tthsqe 01 Jul 2009, 05:13
ok, I guess I'll have to be more specific.
Could someone show me the 64-bit code to create a file with 1) a given file name 2) contents given by the null-terminated character string starting at a given memory location |
|||
01 Jul 2009, 05:13 |
|
tthsqe 01 Jul 2009, 05:15
a text file that is
|
|||
01 Jul 2009, 05:15 |
|
Picnic 01 Jul 2009, 09:38
LocoDelAssembly wrote: Because the entry point is not a leaf function so to do this in "the right way", I use that prologue sequence, and before calling ExitProcess I execute leave. If you remove "push ebp" and "leave", and change "lea edi, [ebp-4]" with "mov edi, ebp", the code will work OK too, but just to be safe I better leave the code as it is now. So, LocoDelAssembly leave instruction is same like writing... (?) Code:
mov esp, ebp
pop ebp
Sorry to ask here tthsqe. p.s unfortunately i don't have a 64-Bit machine. |
|||
01 Jul 2009, 09:38 |
|
LocoDelAssembly 01 Jul 2009, 14:52
thimis, yes.
tthsqe, I don't have a 64-bit OS to prepare the example correctly for you but the difference is not really big, and if you continue to use "invoke" then you won't have to worry about the calling convention used in Win64. |
|||
01 Jul 2009, 14:52 |
|
tthsqe 01 Jul 2009, 23:42
Ok, it seems that this is doing what I wanted:
Code: format PE64 GUI entry start include 'win64a.inc' section '.text' code readable executable start: invoke CreateFile,FileTitle,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0 test rax,rax je exit mov [FileHandle], rax invoke WriteFile,[FileHandle],FileContent,17,BytesWritten,0 exit: invoke CloseHandle,[FileHandle] invoke ExitProcess,0 section '.data' data readable writeable FileTitle db 'C:\Users\---\Documents\itworked.txt',0 FileHandle dq ? BytesWritten dq ? FileContent db 'It really worked!',0 section '.idata' import data readable writeable library kernel32,'KERNEL32.DLL',\ user32,'USER32.DLL' include 'api\kernel32.inc' include 'api\user32.inc' Now I'm wondering what the arguments on invoke WriteFile actually do. It looks like the 17 indicates that the first 17 bytes of the character string get written. But then what is the point of the last two arguments - BytesWritten and 0? Also, CreateFile seems to be returning with 0x10 in rax. How is this related to the actual location of the file? |
|||
01 Jul 2009, 23:42 |
|
arigity 01 Jul 2009, 23:56
tthsqe wrote:
you should see msdn for what function arguments/return values are for. |
|||
01 Jul 2009, 23:56 |
|
tthsqe 02 Jul 2009, 02:01
Thanks
|
|||
02 Jul 2009, 02:01 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.