문제

I'm using Apple's llvm-gcc to compile some code with inline assembly. I wrote what I want it to do, but it adds extraneous commands that keep writing variables to memory. Why is it doing this and how can I stop it?

Example:

__asm__{
  mov r11, [rax]
  and r11, 0xff
  cmp r11, '\0'
}

becomes (in the "assembly" assistant view):

mov  0(%rax), %r11     // correct
movq %r11, -104(%rbp)  // no, GCC, obviously wrong
and  $255, %r11
movq %r11, -104(%rbp)
cmp  $0, %r11

Cheers.

도움이 되었습니까?

해결책

You need to use GCC's extended asm syntax to tell it which registers you're using as input and output and which registers get clobbered. If you don't do that, it has no idea what you're doing, and the assembly it generates can easily interfere with your code.

By informing it about what your code is doing, it changes how it does register allocation and optimization and avoids breaking your code.

다른 팁

it's because gcc tries to optimize your code. you can prevent optimizations by adding -O0 to command-line.

Try adding volatile after __asm__ if you don't want that. That additional commands are probably part previous/next C instructions. Without volatile compiler is allowed to do this (as it probably executes faster this way - not your code, the whole routine).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top