Hi, how would you call a windows api call using MSVC++ 6.0 inline assembler with out causing a windows general protection fault?
Posted on 2001-09-04 13:28:11 by UnregisteredUser
push the necessary parameters in the right order, then call the
API. Simple as that :). Can't see any reason to do the API calls
in inline asm though... It's not like it's going to make it any faster.
Posted on 2001-09-04 13:44:58 by f0dder
Actually, the compilier does a fair job of optimizing API calls - a feature which would be very hard to implement in MASM. :(
Posted on 2001-09-04 18:12:22 by bitRAKE

Actually, the compilier does a fair job of optimizing API calls - a feature which would be very hard to implement in MASM.


... which is why you shouldn't do api calls in inline asm, but should
rather call them "natively" from your C code :)

And don't worry about it being hard to implement in masm... it's
not all that important, just a little bonus when you use a good compiler :)
Posted on 2001-09-04 18:56:02 by f0dder
f0dder,

What is an optimised API call ?.
PUSH parameters, call function, end of story. Cant be done faster or smaller so what is the point. :)

UnregisteredUser,

There is no point to doing them manually in C as they are just done the same way, in MASM you have the choice of how you code an API call but coding them manually has no advantage over an automated call.

Regards,

hutch@pbq.com.au
Posted on 2001-09-04 20:37:48 by hutch--
Hutch, stuff like save the api adress in a register and use "call <reg>"
if the api is used more than one. Pushing parameters at the right
time, etc. That sort of "smart stuff".
Posted on 2001-09-04 20:42:29 by f0dder
stuff like save the api adress in a register and use "call <reg>" if the api is used more than one. Pushing parameters at the right time, etc. That sort of "smart stuff"


Sheesh, I have a hard enough time dealing with limited register resources, let alone reserve one for call addresses. I've got 16 regs on the mainframe, and that ain't enough! And you know, some of the tricks, like leaving a parameter on the stack, or pushing them "at the right time", as in much earlier in your code, can be a real bear to debug. Hardly seems worth the few clocks you save, for the "average" program.

:)
Posted on 2001-09-04 22:45:10 by S/390
f0dder:

yes, and don't forget the "smart stuff" like constant folding, dead code elimination, algebraic simplification, common subexpression elimination, etc... the kinda "smart stuff" that makes up for the "not-so-smart stuff" between the chair and the keyboard.

dr phil :tongue:
Posted on 2001-09-04 23:05:43 by phil
S/390: the compiler will only do this if it deems it is good register usage.
Thus it's not likely to do it inside an algorith, but rather in long
repetitive API calls. Sure, these tricks don't matter all that much,
and are hard to debug. But the neat thing is, you will not *need*
to debug this very often (at the assembly level at least). And afaik, you can always turn off these smart things if you *do*
need to assembly-level debugging. So you don't actually *lose*
anything on it... The wins might be small, but you don't lose anything :].

Now, using registers for call addresses, and leaving parameters
on the stack is hardly a reason to switch to a compiler, I see it
more as a bonus.

However, things like constant folding, algebraic simplification, and
common subexpression removal... those are some of the greatest
benefits of using a compiler. "Mumble grumble I can do it more
efficiently". Sure, compilers will hardly ever *beat* handoptimized
asm code. But I code "complex stuff" faster in C than asm, and when
I'm satisified with the result, I can always convert it to asm if it's necessary.

16 registers... I wish I had 16 registers... but better get off that
track, or I'd end up bitching about all the deficiencies of the 386+
processor and how much I hate intel for not doing major overhauls
when they went from 80286 to 80386 :]
Posted on 2001-09-04 23:30:57 by f0dder

Hi, how would you call a windows api call using MSVC++ 6.0 inline assembler with out causing a windows general protection fault?


Try:

#include<windows.h>

char []="hello";
__asm{
push 0

lea eax,qwe

push eax
push eax

push 0
call dword ptr MessageBoxA
}
Posted on 2001-09-05 14:26:43 by MatriX
try this,

MessageBox(hWin,szText,szTitle,MB_OK);

same as..

__asm{

push MB_OK
lea eax,szTitle
push eax
lea eax,szText
push eax
push hWin
call dword ptr

add esp,4*4

}
Posted on 2001-09-10 23:08:07 by c][obo
f0dder,

--------
But I code "complex stuff" faster in C than asm, and when I'm satisified with the result, I can always convert it to asm if it's necessary.
--------

This is hogwash, "complex stuff" in a high level language translates to garbage at an opcode level and it is usually easier to convert the high level routine directly to assembler than mess around with compiler optimisations that don't have the grunt to do the smart stuff anyway.

Loading a register with a value and doing multiple pushes is trivial,

mov eax, 0
push eax
push eax

etc ....

The problem is related to the difference between an HLL and an assembler, with an assembler you can design an algorithm that uses the registers in a very efficient way without repeated pushes and pops, that uses normal fall throughs to exit loops and a miriad of normal ASM tricks that no optimising compiler can do.

The best you can say about compilers is that they are at times easier to design algorithms in but unless you keep in mind the equivalent assembler constructions, you can end up with serious underperformances with the end result.

Compilers win in the "don't care" range of code because they can hack it out faster, in performance terms they don't make the starting gate with algorithms written in assembler.

Regards,

hutch@pbq.com.au
Posted on 2001-09-11 04:31:04 by hutch--