This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NOP Vs _nop_()?

In one of my project I am talking with MX909 Driver IC with C51 ucontroller. To initialize MX909 if I write module in Assembly language it's working fine. Once I replaced with C same module it's not working.

The only difference I can make for both assembly and C languages is NOP and _nop_() instruction. Is both instructions will take same number of machine cycles?

Thanks,
Suresh Kumar Kavula

Parents
  • My guess is that you suffer from some fallacy that e.g. a for loop is the same as a djnz loop and nothing could be farther from the truth.

    ----- FUNCTION main (BEGIN) -----
        8: void main(void)
        9: {
       10:    unsigned char loop;
       11:
       12:    for(loop=128;loop;loop--)
    ;---- Variable 'loop' assigned to Register 'R7' ----
    00000F 7F80              MOV     R7,#080H
    000011         ?C0001?TEST:
       13:    {
       14:    }
    000011 DFFE              DJNZ    R7,?C0001?TEST
    000013         ?C0004?TEST:
       15:
       16:    while(1);
    000013 80FE              SJMP    ?C0004?TEST
    ----- FUNCTION main (END) -------
    

Reply
  • My guess is that you suffer from some fallacy that e.g. a for loop is the same as a djnz loop and nothing could be farther from the truth.

    ----- FUNCTION main (BEGIN) -----
        8: void main(void)
        9: {
       10:    unsigned char loop;
       11:
       12:    for(loop=128;loop;loop--)
    ;---- Variable 'loop' assigned to Register 'R7' ----
    00000F 7F80              MOV     R7,#080H
    000011         ?C0001?TEST:
       13:    {
       14:    }
    000011 DFFE              DJNZ    R7,?C0001?TEST
    000013         ?C0004?TEST:
       15:
       16:    while(1);
    000013 80FE              SJMP    ?C0004?TEST
    ----- FUNCTION main (END) -------
    

Children
  • My guess is that you suffer from some fallacy that e.g. a for loop is the same as a djnz loop and nothing could be farther from the truth.

    Jack,

    The authoritative masquerade very clearly exposed!

  • The compiler may implement a for loop by using a djnz.

    And here's proof that a for loop is not the same as a djnz loop:

    line level    source
    
       1          int main(void)
       2          {
       3   1              unsigned char loop;
       4   1
       5   1              for(loop = 0; loop != 128; loop++)
       6   1              {
       7   2              }
       8   1              while(1);
       9   1              return(0);
      10   1      }
    C51 COMPILER V8.02   MAIN                                                                  08/15/2007 13:01:31 PAGE 2
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 1
                                               ; SOURCE LINE # 2
                                               ; SOURCE LINE # 5
    ;---- Variable 'loop' assigned to Register 'R7' ----
    0000 E4                CLR     A
    0001 FF                MOV     R7,A
    0002         ?C0001:
                                               ; SOURCE LINE # 6
                                               ; SOURCE LINE # 7
    0002 0F                INC     R7
    0003 BF80FC            CJNE    R7,#080H,?C0001
    0006         ?C0004:
                                               ; SOURCE LINE # 8
    0006 80FE              SJMP    ?C0004
                 ; FUNCTION main (END)
    

    And another one:


    line level source 1 int main(void) 2 { 3 1 unsigned char loop; 4 1 5 1 for(loop = 128; loop-- != 0; ) 6 1 { 7 2 } 8 1 while(1); 9 1 return(0); 10 1 }
    C51 COMPILER V8.02 MAIN 08/15/2007 13:03:32 PAGE 2 ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION main (BEGIN) ; SOURCE LINE # 1 ; SOURCE LINE # 2 ; SOURCE LINE # 5
    ;---- Variable 'loop' assigned to Register 'R7' ----
    0000 7F80 MOV R7,#080H
    0002 ?C0001:
    0002 AE07 MOV R6,AR7
    0004 1F DEC R7
    0005 EE MOV A,R6
    0006 70FA JNZ ?C0001 ; SOURCE LINE # 6 ; SOURCE LINE # 7
    0008 ?C0003: ; SOURCE LINE # 8
    0008 80FE SJMP ?C0003 ; FUNCTION main (END)

    If you know the compiler and the right syntax, it is possible to get a djnz when using a for loop. There is no guarantee.

  • "The authoritative masquerade very clearly exposed!"

    Not really.

    All it has shown is that, in one particular case, the for loop just happened to turn out as a simple DJNZ loop.

    In general, you can't assume that.

    eg, in the example shown, the oprimiser has happened to assign the loop counter to R7 - unrelated changes in the source code could change that, which may or may not affect the loop timing!

    One more time: There is absolutely no guarantee whatsoever etc, etc,...

  • The authoritative masquerade very clearly exposed!

    Does "Jack" now need an audience to give some applause whenever he (doesn't) find an error in Erik's arguments ?

    int main(void)
    {
        unsigned char loop;
    
        for(loop = 128; loop != 0; loop--)
        {   }
        while(1);
        return(0);
    }
    

    Here's an easy question:

    Assume nothing about the compiler, except that it is actually working correctly. The for loop above will result in (more than one answer may be correct):

    a) a DJNZ loop
    b) another type of loop
    c) no loop at all

  • If you know the compiler and the right syntax, it is possible to get a djnz when using a for loop.

    And THAT is the point!

    Knowledge of what is possible and the means to achieve it along with the ability to explain things without sounding like an obnoxious teacher is the sign of a true authoritative poster.

  • In general, you can't assume that.

    True. But it's a long walk over very thin ice from that to Erik's "nothing could be further from the truth". It's this kind of excessive generalization and his reaction when they're pointed out to him, that so irritates people about Erik.

  • Assume nothing about the compiler, except that it is actually working correctly. The for loop above will result in (more than one answer may be correct):

    a) a DJNZ loop
    b) another type of loop
    c) no loop at all

    What is the prize for people who get the right answer ?

    Here's hoping it's not a trick question!

  • Is this supposed to be funny ?

    Not funny - More sad ... That a potentially useful forum can have so many members that post useful responses muddied by very few who post in a manner that just triggers such negative reactions.

  • Lots of complaints and arguments.

    I read the comment

    My guess is that you suffer from some fallacy that e.g. a for loop is the same as a djnz loop and nothing could be farther from the truth.
    

    as

    Don't think that a for loop is equivalent with a djnz loop.
    

    Any people who read it as:

    The compiler will never make use of a djnz in a for loop.
    

    should maybe be a bit more careful about complaining, since Erik did not claim any such foolish thing.

  • If you know the compiler and the right syntax, it is possible to get a djnz when using a for loop.

    and then you can HOPE (a very useful approach to design) that it will still be a djnz loop in the next release of the compiler.

    Erik

    re my post that caused so much reaction:
    assuming a for loop is the same as a djnz loop and nothing could be farther from the truth.

    where doues 'is' mean 'can not be' ?

  • ... you can HOPE (a very useful approach to design)

    An example of dry wit or the approach you follow?

  • An example of dry wit or the approach you follow?

    If you have to ask you have not read many of my posts

    Erik

  • maybe be a bit more careful about complaining, since Erik did not claim any such foolish thing.

    Maybe, if he'd been a little more correct with his punctuation, people might have been able to understand it more easily.

    Hope he doesn't write his code in the same manner.

  • What is the prize for people who get the right answer ?

    The warm, fuzzy feeling of knowing that they won't fall into the trap of expecting a delay loop in C to give an exact delay.

    Here's hoping it's not a trick question!

    Not at all. On the contrary, there's even a hint pointing in the right direction.

  • If you have to ask you have not read many of my posts

    Ermmmm ... It was a rhetorical question.

    I've read quite a few of your posts; and the responses they trigger!

    Very entertaining ;)