The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: l_d_allan on February 21, 2006, 06:22:40 PM

Title: Guidelines for inline asm in HLL?
Post by: l_d_allan on February 21, 2006, 06:22:40 PM
<alert comment="masm32 newbie">

Is there a link for guidelines on using inline asm within a HLL like C/C++/Delphi etc?  Especially register usage?

I suppose this varies by compiler ... my current main interest is the Microsoft VC6 and VC7.1 compilers. I've read the vc7.1-2003 documentation, but wanted to check with the veterns on this forum.

There are situations in the innermost of bottleneck code where you KNOW a lot about the variables .... such as the buffer is exactly 32 bytes long and is 8-byte aligned .... and production/release functions have to have all kinds of overhead to handle special corner cases and ill-behaved data. That seems like a good place for
__asm MyInlineAsmCode
__asm MyInlineAsmCode
         etc.
__asm MyInlineAsmCode

Does the compiler generally tend to generate prelogue and/or epilogue code if you don't follow certain conventions ... especially register usage? My understanding is that you setting your code up for error is you mess up.

</alert>
Title: Re: Guidelines for inline asm in HLL?
Post by: Vortex on February 21, 2006, 07:01:32 PM
Have a look at this thread for an example of using inline asm with Pelles C, it's similar to VC++ :

http://www.masmforum.com/simple/index.php?topic=362.0
Title: Re: Guidelines for inline asm in HLL?
Post by: Vortex on February 21, 2006, 07:59:39 PM
l_d_allan,

You can use the __declspec(naked) convention to turn off the progue-epilogue code emission , code translated to VC++ :

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

__declspec(naked) char *szUpper(char *text)
{
__asm{
mov eax,[esp+4]
dec eax
__repeat:
add eax, 1
cmp BYTE PTR [eax], 0
je __end
cmp BYTE PTR [eax],97 /* a=97 */
jb __repeat
cmp BYTE PTR [eax],122 /* z=122 */
ja __repeat
sub BYTE PTR [eax], 32
jmp __repeat
__end:
mov eax,[esp+4]
ret
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
char msg[]="inline assembly programming";
MessageBox(0,szUpper(msg),"Hello!",MB_OK);

return 0;
}
Title: Re: Guidelines for inline asm in HLL?
Post by: Vortex on February 21, 2006, 09:19:47 PM
Attached is the same source code compiled with Pelles C

[attachment deleted by admin]
Title: Re: Guidelines for inline asm in HLL?
Post by: MichaelW on February 21, 2006, 11:59:01 PM
 MSDN: C++ Language Reference, Inline Assembler (http://msdn2.microsoft.com/en-us/library/4ks26t93.aspx)

Title: Re: Guidelines for inline asm in HLL?
Post by: hutch-- on February 22, 2006, 02:02:52 AM
ld,

This much with inline assembler with VC is its register usage tends to mess up the internal optimisation that the compiler performs so if it truly speed critical code, it does make sense to write it as a seperate module and link it into the VC app rather than build it in internally. Compiler design seems to be going that way and if I have it right the 64 bit compilers do not support inline assembler.