News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

File Operations, Please Help

Started by tuto, May 18, 2005, 10:34:09 AM

Previous topic - Next topic

tuto

I need to open a text file and put the text in a variable for further use, how can I do it in MASM32?

thomasantony

Hi,
  Look up Iczleion's tutorials and the Win32.HLP file. Use CreateFile and the Writefile functions. And plz do use the HUMOUNGOUS SEARCH BUTTON!!!

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

tuto

Thanx, but the memory allocation stuff seems to me a bit... hard to understand, used the search, but couldn't think of proper keywords... I am more interested in actually putting the file text value in a variable, how to do that? Sorry for my unknowiness...  :(

Vortex


.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib

.data

filename db 'test.txt',0

.data?

size1   dd ?
hMem    dd ?
memsize dd ?
hFile   dd ?

.code

start:

invoke  CreateFile,ADDR filename,GENERIC_READ,\
        0,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
mov     hFile,eax
invoke  GetFileSize,eax,0
inc     eax
mov     memsize,eax
invoke  GlobalAlloc,GPTR,eax
mov     hMem,eax
invoke  ReadFile,hFile,eax,memsize,ADDR size1,0
invoke  CloseHandle,hFile
invoke  StdOut,hMem
invoke  GlobalFree,hMem
invoke  ExitProcess,0

END start

[attachment deleted by admin]

tuto

Thanx, so it puts the value in stdout, but how to work with the value e.g. change it or store it somewhere in memory for further use? Sorry if my questions are starting to annoy you...  :red

hutch--

tuto,

If you have the most current version of MASM32 and the latest macros as well, try the file IO macros to get you started, then you can start trying out seperate windows API calls if you need to.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

AeroASM

Quote from: tuto on May 18, 2005, 08:05:27 PM
Thanx, but the memory allocation stuff seems to me a bit... hard to understand, used the search, but couldn't think of proper keywords... I am more interested in actually putting the file text value in a variable, how to do that? Sorry for my unknowiness... :(

In MASM there are no variables, just bits of memory. There are three main uses for memory: storing numbers as dwords, words, or bytes, or storing text as "strings" of bytes or words, ended with a zero byte or word.

Therefore, to store text somewhere, you need a large block of free memory. You can get this from windows with:


invoke GlobalAlloc,GMEM_FIXED,1024
mov pMem,eax

;you now have 1024 bytes of free memory starting at the address stored in pMem

invoke GlobalFree,pMem

tuto

Thank you for all the great answers, I'm most greatful to you all!  :clap: :clap: :clap: BUT here goes the last question of all... I promise that this is the last one!  :bdg Let's say I need to read one word from a file and compare it to values, if it's "box" than show a messagebox and if it's "exit" than the program exits. If anyone could give me a source code of that kind, I would stop bothering you with my annoying questions!  :U

Vortex

tuto,

Seiously, you should try to do some coding and post it to the forum so that the members could help you. You are encouraged to post your questions but please try to do some serious research about coding.

Nilrem

Yeah this just seems like homework. Everytime (most times) I try and prove I have done some work before asking questions, like addresses of articles I read (but failed to comprehend), or actually finding an api and trying to impliment it into asm. Just little bits like that will get more answers. It will also help you understand because you are trying yourself not just copying and pasting code straight away.

tuto

You don't actually think I need a program that shows a messagebox or exits? :P I understand almost every line of what you have answered and really appreciate it, but I just stepped from Delphi to Asm, and have to completely change the way of thinking, it takes time, I simply can't think of a way to accomplish this problem. My program works as far as I put the file value in the memory, but how do I read values out of memory, compare them simply through .IF pmem==offset stringthatineed? P.S. Just saw the no homeworks rule and now I understand what you were thinking... Well the truth is at school they haven't even started to teach programming for us (Pascal) and maybe won't do that next year, not a word about assembler, I'm simply breaking my way through this hard_but_exciting language myself... Did they teach you assembler at school when you were 16?  :eek Lucky you...  :bg

Vortex


.
.
mov     edx,hMem
@@:
cmp     byte ptr [edx],13
jz      @f
inc     edx
jmp     @b
@@:
mov     byte ptr [edx],0
invoke  szCmp,hMem,ADDR string
or      eax,eax
jz      @f
invoke  MessageBox,0,ADDR string,ADDR string,MB_OK
@@:
invoke  GlobalFree,hMem
.
.


This piece of code reads the first line of the text file test.txt and compares it to a string named box. If the two strings match then a message box is displayed.

QuoteMy program works as far as I put the file value in the memory

tuto, now would you mind if I ask you to post your own code?

[attachment deleted by admin]

tuto

.386
.model flat, stdcall
option casemap:none
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\windows.inc
include \masm32\include\advapi32.inc
includelib \masm32\lib\advapi32.lib
include \masm32\include\urlmon.inc
includelib \masm32\lib\urlmon.lib

.data
dfile   db  "msdns.txt",0
tester  db  "hahahahahahaaaaa",0

.data?
resultz            dd    ? ;updated or created key?
filetmp           dd    ?
readdata          dd    ?
hFile          HANDLE    ?
SizeReadWrite  DWORD    ?   
hMemory HANDLE ?

pMemory DWORD ?
.code
_main:
invoke CreateFile,ADDR dfile,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov hFile,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,65535
mov  hMemory,eax
invoke GlobalLock,hMemory
mov  pMemory,eax
invoke SetFilePointer,hFile,0,NULL,FILE_END
invoke ReadFile,hFile,pMemory,65534,ADDR SizeReadWrite,NULL
invoke CloseHandle,hFile
invoke MessageBox,0,offset pMemory,offset dfile,MB_OK
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
invoke DeleteFile,offset dfile
invoke ExitProcess, 0 ;exit program
end _main

As you can see, the code is quite unreadable and I'm sorry for that, it is actually a combination of sourcecodes, the command is being read from the file, I tried to show the value in a messagebox, I guess that wasn't a good idea... P.S. If you notice any unnecesery things there, keep in mind that I was wildly experimenting with it, but with no results... :( Lots of thanx to all of you who helped me, I've been asking for help in a lot of places and you can't imagine how glad I am, that I can continue my assembler journey, because I was stuck for quite a while now. THANK YOU!!!  :U :U :U

pbrennick

tuto,
It takes a while but it is always worth the effort.  As you learn, you will also learn to think bigger.  Just let it come in its own time, don't force the issue.  And the icing on the cake is you have a lot of friends here just waiting for the opportunity to help you.   :thumbu

Paul

thomasantony

Quote from: tuto on May 19, 2005, 08:16:38 PM

invoke MessageBox,0,offset pMemory,offset dfile,MB_OK
.....
[/quote]
That should be

invoke MessageBox,0,pMemory,offset dfile,MB_OK

pMemory is a pointer to the memory so you can use it directly. When you use OFFSET pMemory, you complicate it by giving it an address of a pointer to the memory. :dazzled:
Thomas
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free