News:

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

fread or readfile?

Started by Ani_Skywalker, October 31, 2011, 05:36:49 PM

Previous topic - Next topic

Gunner

QuoteBOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);

so it would be:
push lpOverlapped
push lpNumberOfBytesRead
push nNumberOfBytesToRead
push lpBuffer
push hFile
call ReadFile


Of course you have to push the address of buffers were needed
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

Ani_Skywalker

Quote from: Gunner on October 31, 2011, 10:50:20 PM
so it would be:
push lpOverlapped
push lpNumberOfBytesRead
push nNumberOfBytesToRead
push lpBuffer
push hFile
call ReadFile


Which is what I am doing, right?

Ani_Skywalker

Quote from: qWord on October 31, 2011, 10:39:52 PM
local buffer[1024]:BYTE
local bytesRead:DWORD
...
push 0
lea eax,bytesRead
push eax
push SIZEOF buffer
lea eax,buffer
push eax
push hFile
call ...


qWord: As always, interesting posts. I have 2 questions about it:

1. Why do we need to load effective address before push, isn't enough that the value is poped upon call?
2. I understand it as (right or wrong) that we can use mov instead of lea? Which one is preferable? Which one uses least clock cycles?

I never seem to find where to look for clock cycles in Intel Software Developers Manual. Looking at page 649 in the pdf, I find lea explained, but nothing about clock cycles..

Gunner

Your beginning right?  Stop trying to save clock cycles...

You use lea because the buffer is local, and ReadFile needs an address.  Now if the buffer was global you CAN'T just push the variable (you would be pushing the value) you would have to push offset variable
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

Ani_Skywalker

Quote from: Gunner on October 31, 2011, 11:58:54 PM
Your beginning right?  Stop trying to save clock cycles...

Yes but I disagree with you here. If you learn something wrong to start with, it's just even harder to get it right the second time. Relearning is much harder then learning. I'll rather have a very slow start and take my time then start coding windows frames using invokes and the Win 32 api. That kind of programming is what I have C and Java for :)

Thanks for the answer though, appreciated!

jj2007

Quote from: Ani_Skywalker on October 31, 2011, 10:56:47 PM
Quote from: Gunner on October 31, 2011, 10:50:20 PM
so it would be:
push lpOverlapped
push lpNumberOfBytesRead
push nNumberOfBytesToRead
push lpBuffer
push hFile
call ReadFile


Which is what I am doing, right?

No. What you are doing is as follows:

Quote from: Ani_Skywalker on October 31, 2011, 07:58:04 PM
Dave: Yeah, that is one of the reasons I refuse to use macros. Got it to work by the way, had forgot one parameter when calling. But bellow code both compiles and runs without error.

mov hFile, fopen("fread-readfile.dat")
push 0 ; lpOverlapped
push bytesRead   ;lpNumberOfBytesRead, i.e.LONG POINTER TO ...
push ptr$(lpBuffer)   ; should be nNumberOfBytesToRead, i.e. the numberof bytes
push 81     ; should be lpBuffer, i.e LONG POINTER TO ...
push hFile   ; that one is correct.
call ReadFile


awais_x

#21
Amazing and nice looking site please love it and make more effective.. keep it up baby..

URL removed.

We do not allow this forum to be used for advertising.

qWord

Quote from: Ani_Skywalker on October 31, 2011, 11:50:24 PM1. Why do we need to load effective address before push, isn't enough that the value is poped upon call?
2. I understand it as (right or wrong) that we can use mov instead of lea?
1. if the address is unknown at compile time (local -> ebp-relative -> esp-relative), you must calculate it at runtime - LEA can do this.
2. only if not referencing a local variable
(Also, to make it clear: LEA is an arithmetic instruction, which never access any memory)
FPU in a trice: SmplMath
It's that simple!

NoCforMe

Quote from: Ani_Skywalker on November 01, 2011, 12:02:16 AM
I'll rather have a very slow start and take my time then start coding windows frames using invokes and the Win 32 api. That kind of programming is what I have C and Java for :)

Like you, I'm something of a n00b to Windows assembly-language programming (though I've been writing DOS-based '86 assembly for a long time). I think you may have a somewhat distorted picture of what INVOKE does.

When I started Win32 asm just a couple months ago, I had never used INVOKE, just plain CALLs. I would recommend to you that you use it. Why?

First of all, it incurs absolutely no more overhead than what you're doing when you push parameters "by hand". That's basically all it does, apart from some extra code that may occasionally be generated to move static addresses into a register (EAX) and then pushing the register. So all in all, it's pretty benign. It's not doing a lot of stuff "behind the curtain." In any case, I would also recommend that you code the two methods (pushing by hand vs. INVOKE) and look at the generated code in the listing file to see exactly what's being generated.

The reason I like to use INVOKE is that it makes it a hell of a lot easier to keep all those pesky parameters straight, especially for functions like CreateWindowEx() that take a lot of them. I HATE trying to chase down crap like [ESP + xxx]; honestly, it makes my head hurt, and I don't code well with headaches. Spend less time trying to be a macho "down-to-the-bare-metal" assembly programmer, and you'll have more energy to devote to debugging the damn program and dreaming up new stuff. It's hard enough to keep track of stuff as it is; why make it harder?

You'll still get very slick, streamlined, slim and fast code out of it, even if you let the assembler help you now and then. Myself, I draw the line at macros (I only use ones I invent myself) and the .if-.else-.endif constructs. Hell, if I wanted to code like that, why not just use C++?

ToutEnMasm


I see that the no use of invoke generate only questions and problems.Thinks are simple using:
Quote
      INVOKE     ReadFile, hFile,addr Buff, NumberOfBytesToRead,\
            addr NumberOfBytesRead ,0            
and what happen with the no use of invoke if you made a mistake ?
Invoke verify (at compile time) if the size of the parameter is correct and if the number of parameters is correct.
Use the push method at your own risks.



dedndave

there are times when using PUSH/CALL is advantageous
in certain cases, i may want to push values while they are in register for a call later
or - there may be times when i want to perform calculations or call other functions between parameter pushes
one example is if some of the parameters are in a table, and i want to use LODSD to get them
these are somewhat advanced tecniques, as they require extreme vigilance over the stack and, perhaps, registers

otherwise, use INVOKE   :U
not only does INVOKE check the parm count for you, but it is easier to read and maintain

dedndave

Ob1 Cannoli: "Become one with The Stack, my young padawan learner"


Ani_Skywalker

Quote from: NoCforMe on November 01, 2011, 03:12:19 AM
It's hard enough to keep track of stuff as it is; why make it harder?

Let's take this discussion once and for all :)

Answer to your question is: Because I like the learn->understand->do chain better then the learn->do chain.

Look at my original post in this thread, I used fread to read a file instead of the native ReadFile. After that it took almost 2.5 pages on this forum to sort out all questions about push/call operations. If I was about to code something useful in assembly, you probably would be right. At the moment, I'm just solving Project Euler problems to l-e-a-r-n.

Also, it has never been about being a "macho" guy. I ask noobish questions on open forums and show my lack off knowledge, hoping more experienced people have the time and willing to share their knowledge with me so I can learn and improve - that's the exact opposite of being "macho".

That said, I appreciate that shared your experience of the MASM-macros from an assembly programmers point of view.

dedndave

well - a lot of it has to do with personal choice

my choice - if it isn't speed critical, make it small
but then, programming is not my business
i am not worried about someone else maintaining my code

many of the professional programmers look at it this way:
if it isn't speed critical, make it as clear and readable as practical (maintainable)

there are other choices, as well
macros - how often - when
basically, i use macros for something quick - or to debug code
otherwise, i prefer to write my own stuff   :P

i also do not like indented lines or if/else/elseif/endif structures

it is all a matter of personal choice
each of us will defend our choices to the death   :lol

NoCforMe

Quote from: Ani_Skywalker on November 01, 2011, 11:56:37 AM
Quote from: NoCforMe on November 01, 2011, 03:12:19 AM
It's hard enough to keep track of stuff as it is; why make it harder?

Let's take this discussion once and for all :)

Answer to your question is: Because I like the learn->understand->do chain better then the learn->do chain.

Look at my original post in this thread, I used fread to read a file instead of the native ReadFile. After that it took almost 2.5 pages on this forum to sort out all questions about push/call operations. If I was about to code something useful in assembly, you probably would be right. At the moment, I'm just solving Project Euler problems to l-e-a-r-n.

Also, it has never been about being a "macho" guy. I ask noobish questions on open forums and show my lack off knowledge, hoping more experienced people have the time and willing to share their knowledge with me so I can learn and improve - that's the exact opposite of being "macho".

That said, I appreciate that shared your experience of the MASM-macros from an assembly programmers point of view.

Whoa just a second there, pardner. I meant no disrespect to you. Your point about learning is well taken. It's good go know what's actually going on in your code, which is why I suggested looking at the assembler listing if you do use INVOKE.

And  I certainly wasn't accusing you of macho programming. There are those who practice it, and I just wanted to warn you of that tendency, is all.

Good luck with your programming learning. Once you get over the hump you should be able to have fun with it.