News:

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

how to read from file in masm

Started by xerox, March 15, 2011, 07:07:30 AM

Previous topic - Next topic

xerox

guys i need help on reading a text file and put in the array and display the contents in the array using irvine library. anyone knows the masm tutorial link i guess that will help too.

Farabi

Use read_disk_file from the masmlib. And for print it use TextOut from the GDI Lib.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

dedndave

do you have to use the Irvine library ?
if not - use masm32

MichaelW

xerox,

Unless support for file operations has been added to the Irvine32 code, I think you should try to use the Windows API directly.

CreateFile
GetFileSize
HeapAlloc
ReadFile
WriteFile
CloseHandle
HeapFree
eschew obfuscation

xerox

Thankyou all of you for trying to help me out. First of all honestly I don't know how file handling  works in assembly language but written few lines of code by myself with the help of my text book. I am trying to read data from file and put in the array and then calculate those values like addition, subtraction etc. Here I am going to post my code please guys help me out with my code. Thanks in advance.


INCLUDE Irvine32.inc
INCLUDE macros.inc


.data
fileName BYTE "DataIn.txt",0
inFile DWORD ?

inBuff BYTE 100 DUP(?)
buffer_size dword 2
fileHandle HANDLE ?

.code
main PROC
   call Clrscr

; Open the file for input
   mov EDX,OFFSET fileName
   call openInputFile
   mov fileHandle,eax

;Check For errors.
    cmp eax,INVALID_HANDLE_VALUE      ;error opening file?
    jne file_ok
    mWrite <"Cannot open file.",0dh,0ah>
    jmp quit
      
file_ok:
; Read the file into a buffer.

    mov EDX,OFFSET inBuff
   mov ECX,buffer_size
   call ReadFromFile
   call WriteWindowsMsg

; Display the buffer
   ;mWrite <"Buffer:",0dh,0ah,0dh,0ah>
       ;mov edx,offset inBuff         ;display the buffer
   mov esi,offset inBuff
Loop1:
    mov eax,0
    mov eax,[esi]
    call WriteDec
    add esi,4
    cmp eax,0
    jne Loop1

    call crlf
   
; close file
    mov eax,fileHandle
    call CloseFile

quit:
    exit
exit
main ENDP

END main

baltoro

xerox,
I have Irvine's book, myself,...and, it's outdated in many respects. It's quite helpful if you are an absolute beginner, and have no idea what the processor is actually executing, when you use the various standard instructions in your code.
However, Michael is right. When you write routines for the Windows operating system, many of the user mode APIs actually call another service routine in the operating system kernel after validating parameters. This is often for security reasons, or for interoperability with any number of other components unknown to you when you write the routine, so, it's more reliable to use the standard Windows APIs mentioned in Michael's post, above.
Irvine's code is largely obsolete, especially if you are writing for anything newer than Windows 98.
Also, Irvine's routines call other routines, and, frankly, I can't remember if they are the C runtime routines or the Windows APIs.
Can you show us, for example, the code from Irvine's library for the call: openInputFile? Since it returns a HANDLE or the defined error value: INVALID_HANDLE_VALUE, I'm guessing that he is actually invoking CreateFile from within his routine. Same thing with ReadFromFile,...most of the programmers here don't use the Irvine library, and are not familiar with the code contained in it.
:bg ...I'm one of the few that's retro enough to be familiar with it.  :bg
...Another advantage to using the Windows APIs directly (with the invoke syntax), is that they all return error or success values in the eax register, which you can easily check, and then write conditional branches in your code based on the return value.   
:bg If you don't understand what I mean,...then ask lots of questions. The guys that populate this forum are incredibly knowledgeable.  :bg
Baltoro

Vortex

Hi xerox,

A simple example :



include     ReadFile.inc

.data

FileName    db 'test.txt',0     ; file to read

.data?

hFile       dd ?
FileSize    dd ?
hMem        dd ?
BytesRead   dd ?

.code

start:


    invoke  CreateFile,ADDR FileName,GENERIC_READ,0,0,\
            OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0           

    mov     hFile,eax

    invoke  GetFileSize,eax,0

    mov     FileSize,eax
    inc     eax

    invoke  GlobalAlloc,GMEM_FIXED,eax
    mov     hMem,eax

    add     eax,FileSize

    mov     BYTE PTR [eax],0   ; Set the last byte to NULL so that StdOut
                               ; can safely display the text in memory.

    invoke  ReadFile,hFile,hMem,FileSize,ADDR BytesRead,0

    invoke  CloseHandle,hFile

    invoke  StdOut,hMem

    invoke  GlobalFree,hMem

    invoke  ExitProcess,0

END start

dedndave

here is another example
the data file is filled with spaces (20h)
20202020h = 538976288   :P

you may have strings in the file
if that's the case, you'll want to convert them from ASCII to binary

tbohon

Thanks for the sample programs guys.

A question:  if, in Vortex's program, I wanted to pull the file name off of the command line, how would I do that?  Also - how would I do that if there was a path in addition to the file?

Working with command line arguments has always confused me, maybe this will be my chance to clear things up!

TIA.

Tom

qWord

hi,
here in an quick example using GetCommandLine() and CommandLineToArgvW()
(there is no ASCII version of this function, so you have to live with unicode):

Quoteinclude masm32rt.inc
.code
main proc
LOCAL nargs:DWORD   
LOCAL ppwsz:ptr PWCHAR 
LOCAL hStdOut:HANDLE   
LOCAL NumberOfCharsWritten:DWORD
 
    mov hStdOut,rv(GetStdHandle,STD_OUTPUT_HANDLE)
   
    ; recive parsed command line (pointer array)
    lea edx,nargs
    mov ppwsz,rv(CommandLineToArgvW,rv(GetCommandLineW),edx)
    mov esi,eax
   
    print "nargs: "
    print str$(nargs),13,10
   
    ; print all command line arguments
    xor ebx,ebx
    .while ebx < nargs
        print "argument "
        print str$(ebx)
        print ": "
       
        ; string len
        mov edx,ulen$(PWCHAR ptr [esi+ebx*4])
        ; print unicode string
        invoke WriteConsoleW,hStdOut,PWCHAR ptr [esi+ebx*4],edx,ADDR NumberOfCharsWritten,0
       
        print chr$(13,10)
        lea ebx,[ebx+1]
    .endw

    invoke GlobalFree,esi

    inkey

    invoke ExitProcess,0
   
main endp
end main
FPU in a trice: SmplMath
It's that simple!