• 9
• 16
• 15
• 12
• 9

# Assembly & C++

This topic is 3894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have some knowledge of assembly and I want to write some small procedures in c++ but I want to write them in assembly, my problem is that I don't know how to integrate the two, accessing variable declared on the c++ part inside the assembly code, etc... I'm using Microsoft Visual studio 2005. I know I have to use something like this:
asm{

}

But I'm not sure...

##### Share on other sites
You will have to be an expert in assembly to write more optimized code than the VS2005. Are you sure that your knowledge of ASM is up to these standards? Inline ASM is considered to be a last resort in low-level C++. I think you'd be better off learning some basic C++ instead of using ASM, because you'll be productive much sooner then with pure machine code with practically no speed difference.
Anyway, I'd take a look at this: http://www.touro.edu/acm/Newsletters/Fall99/advpro.htm

Jeroen

##### Share on other sites
I want to write in asm not because I want to make something super optimized, but because I want to know how to do it... I believe that the more you know the better! :-P

and thanks for the link

##### Share on other sites
Quote:
 Each mnemonic, therefore, represents the smallest possible task the computer can carry out in a single clock cycle. It seems that people are much better at putting these miniscule instructions together efficiently than compilers are

That's just outdated and wrong.

##### Share on other sites
The __asm keyword is not even supported on Vista 64 so you should read this.
It covers using inline assembly with C++ under Microsoft's latest OS.
It'll probably be all Greek to you unless you have prior asm experience.

##### Share on other sites
In VS2005 you can use the _asm keyword to put assembly code right in the middle of C++ code, like this:
int myfunction(int a){	int b = a + 7;	_asm {		mov ax,ax		nop		xor ebx,0	}	return b * 2;}

Be aware that inline assembly is not part of the C or C++ language, but rather is a Microsoft-specific extension. Some other compilers, such as GCC, also have inline assembly, but do it differently because it isn't (and shouldn't be) standard, so inline assembly is not portable across compilers (and obviously won't be portable across different machine achitectures).

##### Share on other sites
If it's just for learning, inline ASM should be even less of a priority, because it is incredibly platform- AND toolchain-dependent. For example Visual Studio inline assembly won't compile with gcc, not even on gcc's windows port. And developing for 64-bit windows with Visual Studio you can't use inline assembly at all.

That said, the VS 2005 documentation contains a decent amount of information about using inline assembly. Some highlights:

* Don't use asm{} blocks; they will compile without errors or warnings, but they will not generate any code. Use __asm{} blocks instead.
* You can use identifiers inside __asm blocks just like in normal C++. So, for example, if you declared an int called LoopCounter, __asm move ecx, LoopCounter will work just fine.
* You can call any C function from inside an asm block, but you can only call certain C++ functions. Specifically, you can only call non-overloaded global functions; calling overloaded functions and member functions will cause compiler errors.

##### Share on other sites
you don't necessarily have to use inline asm, you can write a separate asm file, compile it into an object file, and link it w/ the other object files generated from your c++ files.

this can get tricky to learn at first, but it makes complete sense later.

using nasm syntax, you could write an asm file like (i'm rusty in asm so please forgive any syntax errors):
.datamystring: db "Hello, World!",0.text; i belive this is the correct syntax for declaring externals?import _printfglobal myfunc; this is 'naked' function, no prolog/epilog codemyfunc:push mystringcall _printfret

using nasm to compile this to a microsoft compatible object file, than in your c++ code:
extern "C" {void myfunc();}int main(int argc, char *argv[]){myfunc();}

(myfunc might require some name mangling on the c++ side).

link all your object files together (and don't forget stdio.lib for the asm file), and all should work ok.