Dynamic labels in x86 assembler

And again I ran into a problem where I cannot find a direct solution ...

I'm doing some inline assembly, and I want to execute the code multiple times using the .rept directive, which tells the assembler to act as if the lines following the .rept, all the way back to before .endr, are repeated a specified number of times.

The obvious problem, of course, is that label 18 has already been specified. wondering if there is a way to create a dynamic label for each iteration?

  __asm__ __volatile__ (".rept 10 \n\t");  
  __asm__ __volatile__(  "test eax, eax \n\t" );  
  __asm__ __volatile__(  "jne  18f\n\t" );  
  ...  
  __asm__ __volatile__(  "18: nop  18f\n\t" );   
  __asm__ __volatile__(  ".endr\n\t" );  

      

+1


a source to share


2 answers


If you don't mind doing some manual work, this should be done with an inline character .

(dot) that evaluates to the current target address. See the documentation . You should be able to do something like:

asm volatile("jne .+1");

      



Where 1

you need to change depending on how far you want to jump, of course. This is a side part as you will need the byte offset. I haven't tested this, but I think it should work.

This documentation page also shows how to use string substitution to generate labels, you can also use this technique.

+1


a source


why not skip .rept and .endr and just a loop? set ecx to 10 (or 0A if you want 10 loops, not 16) put a label (say loopbegin :) where your rept statement is, and loop loopbegin

) where the .endr statement is. This way, your label 18 won't be ambiguous.



+1


a source







All Articles