Hi All,

I have just started to play around with masm. A few questions have cropped up for which I did not find an answer. Please keep in mind that I just started to programm in assembler, so maybe my questions may sound stupid.

My first real proc uses overlapped io to read data in chunks from a file and provide that data to a supplied callback function. The callback function should process at the same time when a file read is in progress. I did this by first writing a c++ class and than translating it by hand to assembler. The proc is long so I attached it, together with a visual studio .net 2003
project to compile and test it.

My questions:

The proc needs to allocate two big memory chunks. Often the proc will be called many times, e.g. when iterating over a directory. Is it possible to allocate the buffers once and reuse them for every call to overlapped_read? In C++ this would be the responsibility of the constructor/desctructor.

Is there a better easier way for doing the error handling for the ReadFile and GetOverlappedResult? I need to use a lot of labels and jmps to do it which looks a little ugly to me.

Would you split up a proc like this into smaller ones? E.g. Init, Cleanup and the Modulo operation.

If I understood the register preservation stuff only three registers are available for free use across winapi calls: ebx, esi and edi. The others could be trashed by api calls. Is this correct or is it possible to use more than these? And could I assume they remain unchanged across other asm/c/c++ function calls of my own if I don't modify them?

I can imagine there is a lot of code in this proc that could be optimized. At the moment my c++ class and the asm proc are almost exactly the same speed (using a c function callback for crc calculation). I simply don't know better / faster opcodes for the operations I need. What would you change to optimize this proc?

Sorry for this long first post but at the beginning of something new there are always a lot of questions :-)

Posted on 2004-02-13 19:37:42 by Patr3ck
Just allocate the buffer globally, either statically or dynamically, if you want to re-use it for multiple calls (use a global pointer).

You're right about the registers, but you probably forgot about ebp and esp, they have to be preserved aswell ofcourse. So that only leaves eax, edx and ecx 'free'.

Splitting code into multiple functions is nice and clean, but in asm it's a disadvantage: it's also slow.
Compilers normally inline functioncalls when possible, asm doesn't do this, so you lose performance.
You can use macros instead, if you want to have 'inline functions'. Or just indent and comment your code nicely, so the different parts are apparent, even though they're in the same physical proc.
I haven't looked at the code yet, so I cannot comment on that part. Although obviously translating C++ to asm is not the right way to write asm, and if C++ and asm are about the same speed, something must be wrong... either your asm is suboptimal, or you are limited by something other than code performance (I/O in this case, probably), and you should not have been using asm in the first place ;)
Posted on 2004-02-13 20:53:01 by Henk-Jan