PDA

View Full Version : _asm or __asm?



...
12-19-2003, 04:38 AM
in MSVC, it allows you to define assembly blocks with either _asm or __asm (one underscore as opposed to two). i was wondering if there is any difference in these at all, and if so, what? the docs only provide information for the double underscore version.

DISGUISED
12-19-2003, 09:34 AM
I have only had the need to use asm a few times so I am no expert, but I read an article somewhere a while back that basically defined the difference as ....

_asm - simply invokes the inline assembler
__asm - is treated like an intrinsic function call


EDIT: Brain was moving faster than fingers

DavidP
12-19-2003, 06:44 PM
inline assembly is compiler dependent, and most compilers have a few different ways of doing it, in order to support other compilers' ways of doing inline assembly.

there is no ANSI standard on inline ASM code.

the most common inline asm statements are:

asm
_asm
__asm

and i think GCC uses a function call that takes a string, but I have not been able to get it to work so far, so anyone who is able to get it to work, tell me:

asm ( char * );

DISGUISED
12-19-2003, 07:07 PM
Very true, and the article I was referring to was written about MSVC, which is the compiler in question.

JaWiB
12-19-2003, 07:30 PM
and i think GCC uses a function call that takes a string, but I have not been able to get it to work so far, so anyone who is able to get it to work, tell me:

asm ( char * );


I was able to get inline asm to work in Dev-C++ (which uses gcc I believe), but it's really strange, and to use intel syntax you have to add -masm=intel to compiler commands...Which doesn't seem to work if I try to call gcc directly. But anyways here's something I got to compile with Dev-C++:


#include <iostream>
using namespace std;

int main(void)
{
static int x __asm__("x") = 5;
asm(
"mov eax, x;"
"shr eax,1;"
"mov x, eax;"
);

std::cout<<x;
}


Useless, yes, but my assembly skills suck and I couldn't think of anything better...

VirtualAce
12-20-2003, 12:56 AM
There is no difference. There is no function call for asm. It simply inserts your asm code directly into the code stream. This is not always the best way.

Based upon research by Fordy and myself, it is best to either use a separate assembly file for your assembly functions or view your entire code in assembly so that you can see exactly what the compiler is doing.

In many cases inline assembly is much slower due to compiler issues - you must get along with the compiler in your asm code. Best bet is use a pure assembly object module and link it in - then include external references to the assembly functions.

But having an intrinsic call would simply negate any benefits that assembly might have. Upon a call the compiler would have to build a stack frame using ENTER <bytes>,<lexical_level> and then clean up the stack using LEAVE. This would take more cycles than needed.

neandrake
12-20-2003, 01:02 AM
Originally posted by DISGUISED
EDIT: Brain was moving faster than fingers

Don't let it happen again