Вопрос

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