0
votes

Microsoft visual studio has a feature named "Inline assembly" which gives you the ability of writing your x86 codes in a c++ program inside __asm block.

Now i have a question about this feature.

Are the registers in this block real registers or just some virtualization from registers ?

Does using this registers (like eax , ebx and etc.) improve the performance ?

1
The registers are real. Yes, the performance is generally improved if you use them. Note that the C compiler has a very good optimiser that often does a lot better job at picking registers than you, so you should avoid using inline assembly unless you know exactly what you are doing.fuz
Some C compilers, such as armcc, do use virtual registers in inline assembly. Neither Microsoft or GCC inline assembly does.Timothy Baldwin

1 Answers

3
votes

They're real registers, the same ones that compiler-generated asm uses. After assembling compiler output to machine code, there's no difference between which instructions came from inline asm vs. which ones were emitted by the compiler.

Does using this registers (like eax , ebx and etc.) improve the performance ?

Compared to what? Compiler-generated code already uses registers, so no, you can't usually beat the compiler using inline asm unless you know exactly what you're doing. (e.g. you've read and understood all of Agner Fog's optimization guides, Intel's optimization manual, and so on. See more links in https://stackoverflow.com/tags/x86/info).

C++ code for testing the Collatz conjecture faster than hand-written assembly - why? is a good example of hand-written asm being worse than compiler-generated asm.

The more you (or the compiler) keep variables in registers the better, when they change frequently. You can't avoid using registers because x86 doesn't have memory-to-memory instructions outside of a few special instructions. But you can (and should) avoid using memory.


And even then, MSVC's poor inline-asm syntax makes it impossible to pass data into inline asm without bouncing it through memory, so you'd need to write a whole loop in asm to mitigate that overhead.

See What is the difference between 'asm', '__asm' and '__asm__'? for more about that, and an example of the final compiler output for a simple function using MSVC inline asm that shows the compiler-generated instructions as well.

(You can do that yourself for any code using https://godbolt.org/. See also How to remove "noise" from GCC/clang assembly output? for more about looking at compiler output.)

Most of the reasons in https://gcc.gnu.org/wiki/DontUseInlineAsm apply to MSVC asm as well as GNU C asm.