I have two questions about generation of assembly code by gcc as a result of using inline assembly function in C file. Let me first explains my situation/code and then I will ask questions.
Situation:
I have defined following macro NBL2SPM
which calls the mySimMagic2()
function.
#define NBL2SPM(compactCode,MemAdr) mySimMagic2(SIM_CMD_USER,compactCode,MemAdr)
mySimMagic2()
is defined as below.
#define mySimMagic2(cmd, MemAdr, CompactCode) ({ \
void *_arg0 = (MemAdr);\
unsigned long _arg1 = (CompactCode), _res; \
__asm__ __volatile__ ( \
"\tmovq (%%rdx), %%rbx" "\n"\
"\txchg %%bx, %%bx\n" \
: "=a" (_res) /* output */ \
: "d"(_arg0), \
"c"(_arg1) /* input */ \
: "%rbx" ); /* clobbered */ \
_res; \
})
I use the macro in C-source file in following way
NBL2SPM(&(DU1.Vin[50000]),0x00000105);
And it results into following assembly code.
movq %rax, -400104(%rbp)
leaq -400032(%rbp), %rax
addq $200000, %rax
movq %rax, -400096(%rbp)
movq $261, -400088(%rbp)
movq -400096(%rbp), %rax
movq -400088(%rbp), %rcx
movq %rax, %rdx
movq (%rdx), %rbx
xchg %bx, %bx
Question 1: Why compiler (gcc) generates assembly instructions (i.e. movq %rax, -400096(%rbp)
and movq $261, -400088(%rbp)
) to first move the register RAX and immediate value $261
to memory and then loads them back from memory into registers (i.e. movq -400096(%rbp), %rax
and movq 400088(%rbp), %rcx
)? What is the purpose/advantage of this storing of data to memory and then loading back the same data to registers?
Question 2: Suppose that I want to avoid this "unwanted" saving of register/immediate value to memory and then loading it back to register. Is there a way to force the compiler (gcc) to not generate these "unwanted" memory store/load instructions.
User contributions licensed under CC BY-SA 3.0