News:

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

inline asm

Started by hunt@masm, March 19, 2007, 06:42:44 PM

Previous topic - Next topic

hunt@masm

Hi All,

I am new to asm, so if my question is silly please forgive me to waste your time.

I used to run C/C++ under VS and just start to try asm as I hope it can help to scratch my own back. I am wondering should I include prolog and epilog into my "__asm { }" block?
    someFunc Proc
    push    ebp
    mov    ebp, esp   ; end prolog
   :
    mov    esp, ebp   ; start epilog
    pop    ebp
    ret
    someFunc endP
As I have read few samples from MSDN and the web, and found no one wrote them, but not sure.
please help,

Hunt Chang :P


Vortex

Welcome on board.

If use naked functions, you don't need to deal with prologue \ epilogue code :

#include <stdio.h>

int __declspec(naked) __stdcall sum(int a, int b)
{
__asm{
mov eax,[esp+4]
add eax,[esp+8]
ret 8
}
}

int main()
{
printf("70 + 30 = %d",sum(70,30));
return 0;
}


Naturally, you can use here the C calling convention instead of STDCALL

Personnaly, I would suggest you to write Masm procedures to be linked with your VC++ main module. If I am not wrong, inline asm can interfere with the C code optimization.

zooba

Quote from: Vortex on March 19, 2007, 07:41:03 PM
If I am not wrong, inline asm can interfere with the C code optimization.

Perfectly correct. A method that includes inline assembly code will not be optimised at all by some compilers (including the MS variety). Presumably the idea is that you either implement the entire function in optimised assembly or let the compiler do it. I think a fair assumption is that all variables are moved back to memory for an _asm block, even if they were stored in registers up until then.

Cheers,

Zooba :U

hunt@masm

Thanks to Vortex and Zooba, both of your responses are impressively fast, thank you.

Though I have run C++ for years as a hobby, but I guess myself is still an ameteur. Allow me for one more question, since both of you have mentioned about compiler's optimization, does it very important to exe's speed?  Cause the reason I jump into asm is speed, as I need to do some real-time matrix calculation, and I wish to have both the speed and lower down the burden of sys to make my program's response more smooth.

Besides, I have read some articles before, most of the authors normally recommeneds to turn off the compiler's optimization. To use VC++ under VS, this is also the default choice.

I like inline asm cause it is handy, but the annoy thing is that I can not invoke subrotine as MASM's do. I wonder that if I have any chance to set the masm's macros myself into C's macros, so to use them with inline __asm? As I have not started to study the asm's Macro yet, any comment would be very appreciate.

Cheers,

hunt chang

zooba

For a debug configuration under VC++, yes, optimisation is turned off. Under a release configuration is it turned on. The reason for this is that the optimisations quite often cause the source code and the generated code to not match up which can make source level debugging very hard. The optimiser has also been known to occasionally break some code, though this is rare and usually because the code isn't being good in the first place.

I haven't benchmarked it, but some of the optimisations are very complex. For very repetitive code (ie. inner loop stuff) you'll probably see a reasonable difference. For API crunching, you probably won't notice.

AFAIK, when inlining assembly you have to use push/call syntax. I don't believe there's anything to gain by using assembly for this and you could potentially ruin optimisations in the procedure you're calling (such as passing parameters in registers).

If you're doing matrix calculations, learn SSE. It will have much more effect on the speed than converting C++ FPU code to ASM FPU code (though be wary of running your program on computers that don't have SSE). I am yet to see a good way of using SSE in C++ without inline assembly, so you will probably want to use ASM for this.

C preprocessor macros are applied blindly, so they will work within __asm blocks. I don't believe there is any support for MASM style macros (as nice as it would be).

A question for you: You've highlighted 'real-time' in your post. By real-time, do you mean "as fast as possible" or "completed within a known time period"? There are different techniques which can help with both. (BTW. the second definition is correct, the first is a misinterpretation :wink )

Cheers,

Zooba :U

hunt@masm

Hi Zooba,

Thanks a lot for your advice about the optimization difference between inline __asm and tthe asm subroutine. It seems I shall go via the latter way. I have done few tests to make MASM works with VS and seems work fine so far. Thanks again for your kind share.

Regarding to the real-time issue, I am tring some ideas on speech recognition of my mother-lang now, I believe asm can help me save some cycles at least on FFT (fast fourier transfer), then I may get more room to handle the other part like data matching, etc. The project is still pretty primitive, and there are still lots of moutains waiting in front of my way.

Cheers and wish you the best with your family,

Hunt