Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About jusic

  • Rank
  1. My previous post was quite long, wrong title, and didn't contain code which could be easily tested by others, so I try to be more precise this time. Bug Description: status,z -flag is not updated when using if(var != 0) and maximal optimization (used as default optimization ?) Steps to reproduce: Copy the code below and build it with maximal optimization (Settings -> Options -> Optimization) //bug.c // this program turns on all PORTB pins, if everything works void main() { char result = 0; TRISB=0; PORTB=0; while(1) { result=1; //asm movf _result_main,w; //asm sublw 0; if(result != 0) PORTB=0xff; } } and the resulting asm code will be: ;;;;;;;; result=1; movlw D'1' movwf _result_main ;;;;;;;; //asm movf _result_main,w; ;;;;;;;; ;;;;;;;; //asm sublw 0; ;;;;;;;; ;;;;;;;; if(result != 0) btfsc STATUS, Z goto label_0002 Where's the logic or arithmetic operation...? Expected behaviour: The if(result != 0) line is compiled to one btfsc STATUS, Z -instruction, but there should be those commented asm-lines too in some form - which loads the variable and does a logic or an arithmetic operation to it. (status, z -flag is updated only by a logic or an arithmetic operation --PIC16F877 datasheet) Problem is 100% reproducable, at least here... Do you others have the same problem or do I have just misdownloaded my copy of PicAnt?-) PicAntIDE version: v5.0.6 Compiler: C2C++ Compiler version: v5.0.6e OS: Win2k Professional Comments: As a workaround those commented asm-lines can be used, or use if(result > 0) or if(result == 1) which seem to work rightly. Or by turning optimization Off or to Moderate the if(result != 0) works rightly.
  2. Whops, that last code snip came from the "magic line uncommented version", sorry Here's the asm code without the 'magic line': ;;;;snip ;;;;;;;; //rpyptr++;////////////////////// magic line ;;;;;;;; ;;;;;;;; ;;;;;;;; return 1; movlw D'1' goto destrRS232WaitReply return ;;;;snip
  3. The problem occurs in this part of code I wrote: result = RS232WaitReply(200, "OK^"); if(result != 0) RS232SendCmd("yes\r\n^"); else if(result == 0) RS232SendCmd("no\r\n^"); Let's see what there is in asm: ;;;snip call _RS232WaitReply bcf STATUS, RP0 bcf STATUS, RP1 movwf _result_main ;;;;;;;; if(result != 0) btfsc STATUS, Z ;;; Z flag is not updated! goto label_0004 ;;;snip Here is that wait reply function: char RS232WaitReply( short timeout, const char *reply) { char rpyptr = 0; char temp = 0; for (Time=0; Time<timeout;) // loop until timeout { if(RdPtr != RxPtr) { temp = RS232BufGetChar(); RS232SendChar(temp); if( reply[rpyptr] == temp) // if chars match { rpyptr++; if(reply[rpyptr] == 0x5E) // reached replystr end? { //rpyptr++;////////////////////// magic line return 1; } } else // if chars don't match { rpyptr=0; // take pointer back if( reply[rpyptr] == temp) // it is now the beginning? rpyptr++; } } } return 0; } In assembler: ;;;;snip ;;;;;;;; rpyptr++;////////////////////// magic line bcf STATUS, RP0 bcf STATUS, RP1 incf _rpyptr_RS232WaitReply, F ;;;;;;;; ;;;;;;;; ;;;;;;;; return 1; movlw D'1' goto destrRS232WaitReply return ;;;;snip Reason: The STATUS,Z (zero) flag won't be updated after function return (by the movlw and/or movwf instruction) and that's why it won't work. The datasheet says about the STATUS,Z bit like this "The result of an arithmetic or logic operation is (not) zero", so it seems that movlw or movwf instructions have no affect on it. My compiler is C2C++ v5.0.6e and PIC I use is PIC16F877. Reproducable: Yeps. Workaround/Fix: ensure that the zero flag will be updated somehow right that it would work before/at the function return. By uncommenting that "magic line" it works rightly. Regards --Jussi PS. I spotted another small bug related to the IDE and the Line/Column counter. The line number starts from 0 but in compilers it starts from 1. Secondly, when I put tabs (I use 8 in tab width), the Column counter doesn't count them as spaces but the number how many tab "characters" there are.
  • Create New...