Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you "Drop down to Assembly"?

Sometimes when I'm reading stuff, usually in C++ / Ojective-C, I see people say that for speed increase (etc), if necessary, you as the programmer can drop down to Assembly. How do you actually do that? Do you write some Assembly and compile it, and then... ? How do you link the two together? Do you have to link the objects together when linking? Could someone provide an example, maybe of the main function of a simple C++ hello app calling, say a function written in Assembly?

Thanks.

like image 309
Josh Avatar asked Feb 21 '26 06:02

Josh


2 Answers

It differs by compiler, but in Visual C++:

__asm
{
    int 3;
}

Which triggers the debug-interrupt. Everything enclosed in that __asm block is, well, assembler.

like image 162
Moo-Juice Avatar answered Feb 23 '26 20:02

Moo-Juice


As far as I know, most compilers allow for inline assembler. Some assemblers produce object files that are compatible with those produced by a related C++ compiler which it can then link with it's linker.

However, typically, inline assembler produces a speed decrease, not a speed increase. This is because the compiler has no optimization information about what's going on in the assembler block, and a modern compiler typically produces faster assembler than written by a human hand. Most assembly language functionality is available in C++, either through direct language features or intrinsics (like the Interlocked intrinsics in Visual Studio).

like image 26
Puppy Avatar answered Feb 23 '26 21:02

Puppy