0

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 ?

Ramtin Mousavi
  • 332
  • 1
  • 5
  • 17
  • 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 Nov 14 '18 at 17:07
  • Some C compilers, such as armcc, do use virtual registers in inline assembly. Neither Microsoft or GCC inline assembly does. – Timothy Baldwin Nov 14 '18 at 20:36

1 Answers1

3

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • so there is another question, for example my processor is a core i5 processor which certainly has a different architecture from x86. I mean it probably has more registers than x86. and inline assembly has just a few x86 registers. what about the other registers ? – Ramtin Mousavi Nov 14 '18 at 18:07
  • @RamtinMousavi: Your i5 is an x86-64 CPU. It can run in 32-bit or 64-bit mode. MSVC inline asm isn't available when compiling for 64-bit mode. Compiler-generated code can use 64-bit registers just fine. When running in 32-bit mode, you "only" get the 8 instead of 16 integer registers and xmm/ymm/zmm 0..7 instead of xmm/ymm0..15 or zmm0..31 But you still have the x87 stack, MPX bound registers, EFLAGS, and segment, debug, and control registers. – Peter Cordes Nov 14 '18 at 18:13
  • @RamtinMousavi Of course your CPU is an x86 CPU with the x86 architecture. It does have other registers, but they are not general-purpose registers like eax, ebx, etc., so you can't use them directly for the same purpose. – fuz Nov 18 '18 at 12:38
  • @fuz thanks. i didn't know all core ix cpus are x86 too. do all intel cpus which are released after 8086 use same assembly lang? – Ramtin Mousavi Nov 18 '18 at 17:19
  • 1
    @RamtinMousavi While Intel also makes processors with other architectures, all Intel CPUs you are going to encounter normally are x86 processors. Same for AMD processors. If the architecture was different, then x86 programs would not run on these chips without being recompiled first. – fuz Nov 18 '18 at 18:02