Jump to content

if(var != 0) more precisely (ex.func ret problem)

Recommended Posts

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)


 // this program turns on all PORTB pins, if everything works

void main()
 char result = 0;


   //asm movf _result_main,w;
   //asm sublw 0;
   if(result != 0)


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...? :o


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.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...