Jump to content
Sign in to follow this  
ajcrm125

Break Not Handled Properly Within Switch

Recommended Posts

Could also be a problem elsewhere, but as of now I'm only experimented with a switch statement.

It seems that the break statement will not break out of a case within a switch if the break is contained inside curly braces. For example:

//#include <system.h>
#define INIT 0
#define STATE1 1
#define STATE2 2
#define STATE3 3

char state;
char nextState;

main()
{
state = 0;

while(1)
{
 switch (state)
 {
  case INIT : {
            nextState = STATE1;
            break;
           }
  case STATE1 : {
            nextState = STATE3;
            break;
           }
  case STATE2 : {
            nextState = STATE1;
            break;
           }
  case STATE3 : {
            nextState = STATE2;
            break;
           }
  default : nextState = INIT;
 }  
}         
            
}

 

If you compile this the assembly looks like this:

;;;;;;;;//#include <system.h>
;;;;;;;;char state;
;;;;;;;;char nextState;
;;;;;;;;
;;;;;;;;
;;;;;;;;main()
_main__code
;;;;;;;;{
;;;;;;;; state = 0;
clrf _state
;;;;;;;;
;;;;;;;; while(1)
label_0000
;;;;;;;; {
;;;;;;;; switch (state)
movf _state, W
movwf _code_tmp_0000 
;;;;;;;; {
;;;;;;;; case 0 : {
movf _code_tmp_0000 , W
sublw D'0'
btfss STATUS, Z
goto label_0003
label_0004
;;;;;;;; nextState = 1;
movlw D'1'
movwf _nextState
;;;;;;;; break;
;;;;;;;; }
;;;;;;;; case 1 : {
label_0003
movf _code_tmp_0000 , W
sublw D'1'
btfss STATUS, Z
goto label_0005
label_0006
;;;;;;;; nextState = 3;
movlw D'3'
movwf _nextState
;;;;;;;; break;
;;;;;;;; }
;;;;;;;; case 2 : {
label_0005
movf _code_tmp_0000 , W
sublw D'2'
btfss STATUS, Z
goto label_0007
label_0008
;;;;;;;; nextState = 1;
movlw D'1'
movwf _nextState
;;;;;;;; break;
;;;;;;;; }
;;;;;;;; case 3 : {
label_0007
movf _code_tmp_0000 , W
sublw D'3'
btfss STATUS, Z
goto label_0009
label_0010
;;;;;;;; nextState = 2;
movlw D'2'
movwf _nextState
;;;;;;;; break;
;;;;;;;; }
;;;;;;;; default : nextState = 0;
label_0009
clrf _nextState
;;;;;;;; }
;;;;;;;; }
label_0002
;;;;;;;;
;;;;;;;;}
goto label_0000
label_0001
_main__end
const_ptr_data
END

 

As you can see, the break is essentially ignored. If you leave out the curly braces, compiles to this:

;;;;;;;;//#include <system.h>
;;;;;;;;char state;
;;;;;;;;char nextState;
;;;;;;;;
;;;;;;;;
;;;;;;;;main()
_main__code
;;;;;;;;{
;;;;;;;; state = 0;
clrf _state
;;;;;;;;
;;;;;;;; while(1)
label_0000
;;;;;;;; {
;;;;;;;; switch (state)
movf _state, W
movwf _code_tmp_0000 
;;;;;;;; {
;;;;;;;; case 0 :
movf _code_tmp_0000 , W
sublw D'0'
btfss STATUS, Z
goto label_0003
;;;;;;;; nextState = 1;
label_0004
movlw D'1'
movwf _nextState
;;;;;;;; break;
goto label_0002
;;;;;;;;
;;;;;;;; case 1 :
label_0003
movf _code_tmp_0000 , W
sublw D'1'
btfss STATUS, Z
goto label_0005
;;;;;;;; nextState = 3;
label_0006
movlw D'3'
movwf _nextState
;;;;;;;; break;
goto label_0002
;;;;;;;;
;;;;;;;; case 2 :
label_0005
movf _code_tmp_0000 , W
sublw D'2'
btfss STATUS, Z
goto label_0007
;;;;;;;; nextState = 1;
label_0008
movlw D'1'
movwf _nextState
;;;;;;;; break;
goto label_0002
;;;;;;;;
;;;;;;;; case 3 :
label_0007
movf _code_tmp_0000 , W
sublw D'3'
btfss STATUS, Z
goto label_0009
;;;;;;;; nextState = 2;
label_0010
movlw D'2'
movwf _nextState
;;;;;;;; break;
goto label_0002
;;;;;;;;
;;;;;;;; default : nextState = 0;
label_0009
clrf _nextState
;;;;;;;; }
;;;;;;;; }
label_0002
;;;;;;;;
;;;;;;;;}
goto label_0000
label_0001
_main__end
const_ptr_data
END

 

and behaves as expected. I've loaded the C source into MS Visual C++ and both variations behave as expected so it looks like there's a dependancy on the break statement and curly braces for C2C Plus.

Share this post


Link to post
Share on other sites

Adam,

 

Have you tried the BoostC compiler ?

It does a much better job of everything :(

 

Regards

Dave

Share this post


Link to post
Share on other sites

ajcrm125,

 

Unfortunately I'm working with the PIC12F508 which I don't think BoostC supports right?
Yes unfortunately you are stuck with C2C-plus compiler for this device :(

 

Regards

Dave

Share this post


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.

Guest
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.

Loading...
Sign in to follow this  

×
×
  • Create New...