News:

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

Guidelines for inline asm in HLL?

Started by l_d_allan, February 21, 2006, 06:22:40 PM

Previous topic - Next topic

l_d_allan

<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>

Vortex

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

Vortex

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;
}

Vortex

Attached is the same source code compiled with Pelles C

[attachment deleted by admin]


hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php