How do I move the address of a label to a registry?

advertisements

In gcc inline assembly for x86, How can I move the address of a label in a register. What it should be instead of $label here, because the assembler is giving error undefined reference to label.

Note that the label is outside the asm block, that is, within the normal C code.

      __asm__ __volatile__ ("movl $label, %eax;");
label:
      .....


You can do:

register unsigned long eax __asm__("eax");  // See [1]
eax = &&label;  // See [2]

label:
  // some code

However, beware: GCC is free to reorder code under optimization, and so the C label may not be exactly where you would it expect it to be.

If you need precise placement of label (e.g. because some assembly code will jump to it), you'll be better off coding the whole thing in assembly.

[1] http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html#Local-Reg-Vars

[2] http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html