News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

read from binary file

Started by moha1, April 12, 2014, 06:37:22 AM

Previous topic - Next topic

moha1

Hi guys,
I have a project to do in wich i must use a variable(byte) which is in a binary file so I must read for example the first byte or any byte depending on the port i want to read from,how should I do this?


Thanks


dedndave

use CreateFile to open the file
use SetFilePointer or SetFilePointerEx to set the file pointer
use ReadFile to read the byte into a buffer
use CloseHandle to close the file handle

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365542%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211%28v=vs.85%29.aspx

use the forum search tool for examples of each function
or - use Windows Explorer search tool to look in masm32\examples for files containing those function names

dedndave

if you like, you can do it without setting the file pointer
make the buffer large enough to read the entire file up to the desired byte
then access the buffer at the desired offset

moha1

I ve found the fileio example from masm32 but it doesn't t help me because it reads from a text file and not from a binary file in which I have a number of ports and each port is represented by a byte.I must get the byte that is in port 0 that means the first byte and put it into a variable in my program.

KeepingRealBusy

In MASM it doesn't matter what the underlying format is, you always read BYTES. You can then determine what to do with that BYTE data. In your case, just read and use the first BYTE.

Dave.

jj2007

Here is a complete example that reads one byte from a known file. We expect that byte to be "M" (Ascii 77) as in "MZ" ;-)

include \masm32\include\masm32rt.inc

.code
start:
  ; open a file - the handle returned in eax gets pushed on the stack:
  push fopen("\Masm32\examples\exampl05\hlldemo\fileio\fileio.exe")
  push 0      ; create an empty 4-byte buffer on the stack
  mov ecx, esp      ; and store its address to ecx
  push eax            ; create another 4-byte buffer on the stack
  mov edx, esp      ; and store its address to edx
  invoke ReadFile, eax, ecx, 1, edx, 0      ; invoke ReadFile, handle, pBuffer, #bytes, pBytesRead, 0
  .if !eax
      print LastError$(), 13, 10      ; good to know if an error occurred
  .endif
  pop ecx                  ; get value from the second stack buffer
  print str$(ecx), " bytes read", 13, 10
  pop ebx                  ; get value from the first stack buffer
  call CloseHandle      ; the handle is still on the stack, so this is invoke CloseHandle, eax
  print str$(ebx), " is the first byte"
  exit

end start


MasmBasic version:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  Let esi=FileRead$("\Masm32\examples\exampl05\hlldemo\fileio\fileio.exe")
  Inkey "The first two bytes are [", Chr$(Cvi(esi)), "]"
  Exit
end start


Output: The first two bytes are [MZ]

dedndave

here's another example
notice that i set the file pointer to 0
that is not a necessary step, as when you open a file, it is set to 0
but - if you want to read some other offset, the code is there

moha1


dedndave

i wanted to mention....

the buffer only needs to be 1 byte long, in this case
i modified a program that i had previously written to create emu8086Read
it read the entire contents of the file into the buffer

moha1

I've tried to make what you have said in my code and it doesn't work,it works well separate reading from emu8086.io.
So i have a fan wich i must controll from emu8086 so i must write from emu in emu8086.io file and i must take the value and controll my fan.
The variable i must modify is named Buffer wich inittialy i defined 5 to show that the code works.
I've created a proc wich can read from file and when i'm calling it before i do things with my buffer variable,my fan image won't apear.
I have no ideea why it is doing like that because separate,the two programs works perfectly.
So i have commented the part where i have done the file reading proc and where i call it.
So al i must do is to read from emu8086.io and put the value in buffer variable.

Any ideeas?

Thanks in advance

dedndave

i don't think you really want to open, read, close the file every 30 mS   :icon_eek:
that's a little over 30 times per second

dedndave

#11
you could also have the "file" routine return an error condition
and - i would choose a more unique name for it   :P
GetFileByte proc

;--------------------------------
;get file size
;--------------------------------

        INVOKE  GetFileAttr,offset szFileName
        or      eax,eax
        jz      Exit00

        mov     dwFileSize,eax

;--------------------------------
;open file
;--------------------------------

        INVOKE  OpnFile,offset szFileName,OPF_FILERANDOM
        sub     eax,INVALID_HANDLE_VALUE
        jz      Exit00

        add     eax,INVALID_HANDLE_VALUE
        mov     hFile,eax

;--------------------------------
;set file pointer (to 0)
;--------------------------------

        INVOKE  SetFilePointer,hFile,0,NULL,FILE_BEGIN

;--------------------------------
;read 1 byte into buffer
;--------------------------------

        INVOKE  ReadFile,hFile,offset Buffer,1,offset dwNumBytes,NULL

;--------------------------------
;close file
;--------------------------------

         INVOKE  CloseHandle,hFile

Exit00:   
ret


GetFileByte endp

if the routine exits with EAX = 0, it means an error occured

moha1

I have done what you have said,i have modified the routine,it doesn't give me any error regarding eax.
The only problem is when i call the proc,the image dissapears.I don't have any errors.
I also tryied pushing eax,abx,acx because they are used after but it still doesn't work,need help!
Thanks

dedndave

here's the question...

how often is the value in the file going to change ?

you are trying to read it 33 times per second
if you can read it once - then do that outside of the WM_TIMER code (WM_CREATE, perhaps)

if the file value is updated often, then you might take another approach
set up a counter (dword) variable (initial value = 1)
each time WM_TIMER is received, decrement the counter
when the counter reaches 0, read the file byte and reset the counter variable
now, how often it is updated depends on the timer elapse and the counter variable
i would think you could update once every minute or so
so, the counter variable might be reset to something like 2000

moha1

The value is changed often beause i controll the fan from emu 8086 so i can change the value after 2 seconds for example.
The thing is i'm knew in masm and i don't know how to work with a counter.
Can you help me please:)