flat assembler
Message board for the users of flat assembler.

Index > Windows > Console read/write

Author
Thread Post new topic Reply to topic
MIHIP



Joined: 14 Feb 2013
Posts: 130
MIHIP 19 Jan 2015, 19:37
Hello everybody. Help me please to create reading from file and writing in windows.
There is code:
Code:
format pe console

 pushad
  mov ecx,[size]
  mov esi,[dat]
  lea edi,[esi+ecx]
  xor eax,eax
  @@:lodsb
     mov edx,eax
     mov ebx,eax
     and eax,7
     shr edx,3
     and edx,7
     shr ebx,6
     mov ah,byte[wct+eax]
     mov al,byte[wct+edx]
     shl eax,8
     mov al,byte[wct+ebx]
     mov [edi],eax
     add edi,3
  loop @b
  popad
.end start

dat dd ?
size dd ?     

See: 'dat' stores text file, size stores the lenght of 'dat'. Help me to finish writing code, please! For example (windows CMD):
program.exe READ.TXT OUTPUT.BIN

Help me please, sorry for my bad english, i am from Russia. Program is created, i need only cmd commands, input and output. Good to you, who helped me. I need it soon, guys, please, don't ignore it, please, please, please! THANKS and sorry, if i tired of you... Embarassed
Post 19 Jan 2015, 19:37
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 19 Jan 2015, 20:13
It is not so simple. You need several things:

1. In order to read/write files in Windows you need to import needed functions from the Windows DLLs - You will need at least CreateFile ReadFile, WriteFile - Read the examples in FASM documentation, there is enough examples how the functions are imported.

2. In order to get the command line arguments (the filenames specified in your post) you will need the functions GetCommandLine and CommandLineToArgv

3. Read some more examples from FASM package. Download and read the help files about Win32 - there are described all Win32 functions you will need. As an alternative way - search for the function names in google - probably the first link will bring you to the description.
Post 19 Jan 2015, 20:13
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 130
MIHIP 19 Jan 2015, 20:25
JohnFound wrote:
It is not so simple. You need several things:

1. In order to read/write files in Windows you need to import needed functions from the Windows DLLs - You will need at least CreateFile ReadFile, WriteFile - Read the examples in FASM documentation, there is enough examples how the functions are imported.

2. In order to get the command line arguments (the filenames specified in your post) you will need the functions GetCommandLine and CommandLineToArgv

3. Read some more examples from FASM package. Download and read the help files about Win32 - there are described all Win32 functions you will need. As an alternative way - search for the function names in google - probably the first link will bring you to the description.

How i can get file names from console? PROGRAM IN.txt OUT.txt ??
Post 19 Jan 2015, 20:25
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 130
MIHIP 19 Jan 2015, 20:30
Help me please, here is error while i launch program it crashes:

Code:
format pe gui 4.0

include 'win32ax.inc'
section '.code' executable
start:
        invoke CreateFile,fi,GENERIC_READ,NULL,NULL,\
               OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
               mov [hFile],eax

        invoke GetFileSize,[hFile],NULL
               mov esi,eax

        invoke CreateFile,fo,GENERIC_WRITE,NULL,NULL,\
               CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL
               mov [hFout],eax

        invoke SetFileAttributes,fi,FILE_ATTRIBUTE_NORMAL
        invoke SetFileAttributes,fo,FILE_ATTRIBUTE_NORMAL
@@:
        invoke ReadFile,[hFile],lpBuffer,1,szReadByte,NULL
               inc [lpBuffer]
pushad
  mov ecx,[size]
  mov esi,[dat]
  lea edi,[esi+ecx]
  xor eax,eax
  @@:lodsb
     mov edx,eax
     mov ebx,eax
     and eax,7
     shr edx,3
     and edx,7
     shr ebx,6
     mov ah,byte[wct+eax]
     mov al,byte[wct+edx]
     shl eax,8
     mov al,byte[wct+ebx]
     mov [edi],eax
     add edi,3
  loop @b
size=$-dat ;$
  popad 
        invoke WriteFile,[hFout],lpBuffer,1,szFileWritten,NULL
               dec esi
               jnz @r

        invoke CloseHandle,[hFile]
        invoke CloseHandle,[hFout]

exit:
        invoke ExitProcess,NULL

.end start

section '.data' readable

        fi db 'i.txt',NULL
        fo db 'o.txt',NULL
        dat dd ?
wct db 'ABCDEFGO'
section '.data' readable writable

        hFile dd NULL
        szFile dd NULL
        lpBuffer dd NULL
        hFout dd NULL
        szFileWritten dd NULL
        szReadByte dd NULL    

And text is reversing, why??? Shocked
Post 19 Jan 2015, 20:30
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 19 Jan 2015, 21:32
Use OllyDbg and you will see where you code crashes.

And what is reversing?
Post 19 Jan 2015, 21:32
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 19 Jan 2015, 21:34
MIHIP
Because you don't write a single line of code yourself, but just loiter around the Internet and cadge code snippets:

demotivator 1
demotivator 2

_________________
Faith is a superposition of knowledge and fallacy
Post 19 Jan 2015, 21:34
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 19 Jan 2015, 21:35
- reads file I.TXT then writes to file: O.TXT
- opens only 1 file at a time
- add own munge code...
Code:
format pe gui 4.0
entry start

include 'win32ax.inc'

section '.code' code readable executable

start:
        invoke CreateFile,"I.TXT",GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
        mov [hInputFile],eax

        invoke GetFileSize,[hInputFile],NULL
        mov [dwFileSize],eax

        invoke LocalAlloc,LPTR,[dwFileSize]
        mov [lpBuffer],eax

        invoke ReadFile,[hInputFile],[lpBuffer],[dwFileSize],dwBytesRead,NULL

        invoke CloseHandle,[hInputFile]

;munge: ...

        invoke CreateFile,"O.TXT",GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov [hOutputFile],eax

        invoke WriteFile,[hOutputFile],[lpBuffer],[dwFileSize],dwBytesWritten,NULL

        invoke CloseHandle,[hOutputFile]

        invoke LocalFree,[lpBuffer]

        invoke ExitProcess,NULL

section '.data' data readable writable

        hInputFile      dd 0
        hOutputFile     dd 0
        dwFileSize      dd 0
        dwBytesRead     dd 0
        dwBytesWritten  dd 0
        lpBuffer        dd 0

section '.idata' import data readable

 library kernel32,'KERNEL32.DLL',\
         user32,'USER32.DLL'

 include 'api\kernel32.inc'
 include 'api\user32.inc'    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 19 Jan 2015, 21:35
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 19 Jan 2015, 21:45
for a simple xor encryption you could munge something like so...
Code:
        mov ecx,[dwFileSize]
        mov esi,[lpBuffer]
        mov edi,esi
munge:  lodsb
        xor al,MAGIC
        stosb
        loop munge    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 19 Jan 2015, 21:45
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 19 Jan 2015, 22:16
bitshifter
Do you realize that you've just been fooled by a mediocre beggar?

_________________
Faith is a superposition of knowledge and fallacy
Post 19 Jan 2015, 22:16
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 19 Jan 2015, 23:28
sometimes i feed the beggars when im bored...
Post 19 Jan 2015, 23:28
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 20 Jan 2015, 00:35
bitshifter
Well, then I probably just didn't achieve your level of tolerance. I do that too, but I'm much more discriminating. Otherwise I'd feel like fostering a wrong kind of personal qualities.

_________________
Faith is a superposition of knowledge and fallacy
Post 20 Jan 2015, 00:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.