This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Translate ASM code



I ran into this ASM code to do very short delays:

 

static void __inline__ brief_pause( register unsigned int n )
{
__asm__ __volatile__ (
		"1: \n"
		" dec %[n] \n"
		" jne 1b \n" : [n] "+r"(n)
		);
}

I am not familiar with ASM so I need help on interpreting on exactly how long this "brief pause" is in regards to int n. Thanks!

  • The pause should be 3*n instruction cycles long.

    N shows up in a register, is decremented (1 cycle), and then the code loops until N==0 (conditional jump instruction, 2 cycles.)

     

  • Thanks! So would this be the equivalent C code, time of 3*n cycles?

     

     

    while(n>0)

    n--;

  • Victor Youk said:
    Thanks! So would this be the equivalent C code, time of 3*n cycles? 

    while(n>0)
    n--;

    In theory, yes. But you never know what the compiler does wiht your code.

    With optimization on, the compiler might detect that your code does nothing and will eliminate it, unless n is a global volatile variable. And even then you can only be sure that n will be decremented n times. Depending on the place where this code is inlined, the compiler might unroll it (for small 'n') by placing e.g. 3 times the decrement in a row instead of doing the loop, resulting in 6 instead of 9 cycles for n=3. Also, if n is a global volatile variable, it will be kept and decremented in memory, not register, so it actually takes 4 bytes per decrement (instead of 2) and 4 isntead of 2 cycles each.

    Only when doing it in assembly languange, you'll know the outcome. If you do it in C, it may result in everything, as long as the result is the same (n ending up with 0)

**Attention** This is a public forum