Jump to content
jartim

Same source code generates different and incorrect code in some places

Recommended Posts

jartim    0

Bug description:

The same source code line generates different assembly code in different places. Sometimes the generated code is incorrect, sometimes it is correct.

Steps to reproduce:

Use the same source code in 2 different places in the source.

Detailed description to reproduce this problem I use this statement in two different places in my source code -

WatchdogTarget = CONST_WDT_MINTIME + (Trembler & 0x07);

CONST_WDT_MINTIME is a constant -

#define CONST_WDT_MINTIME 1U

The first time it is used, the following code is generated (which looks correct) -

01D7  3007     MOVLW 0x7
01D8  0547     ANDWF 0x47, W
01D9  00CA     MOVWF 0x4A
01DA  0A4A     INCF 0x4A, W
01DB  00C4     MOVWF WatchdogTarget

The second time is it used, the following code is generated (which is not correct as it modifies the value in "Trembler" at 0x47) -

020A  3007     MOVLW 0x7
020B  05C7     ANDWF 0x47, F
020C  0A47     INCF 0x47, W
020D  00C4     MOVWF WatchdogTarget

Expected behaviour:

This is the code I would expect to see -

020A  3007     MOVLW 0x7
020B  05C7     ANDWF 0x47, W
020C  0A47     ADDLW 0x01
020D  00C4     MOVWF WatchdogTarget

The compiler should not modify the value of "Trembler" at 0x47 (and as the value of CONST_WDT_MINTIME may be other values at compile-time)

If CONST_WDT_MINTIME was (say) 32, I would expect to see this instead -

020A  3007     MOVLW 0x7
020B  05C7     ANDWF 0x47, W
020C  0A47     ADDLW 0x20
020D  00C4     MOVWF WatchdogTarget

Is the problem 100% reproduceable: Every time

IDE version:  MPLAB-X 4.0

Compiler:       BoostC++

Compiler version:  7.42

Target device: PIC12F635

OS:                    Windows 7 64-bit

 

Share this post


Link to post
Share on other sites
jartim    0

Reynard,

No, but the ANDWF 0x47, F at address 0x020B does!

020A  3007     MOVLW 0x7
020B  05C7     ANDWF 0x47, F
020C  0A47     INCF 0x47, W
020D  00C4     MOVWF WatchdogTarget
 
Edited by jartim
Added code as example

Share this post


Link to post
Share on other sites
JorgeF    0

Hi

 

Are you sure that in the context of the whole program, the value of "trembler @ 0x47" still needs to be preserved for the remaining code after 0x020B ?

 

Best regards

Jorge

 

Share this post


Link to post
Share on other sites
jartim    0
2 hours ago, JorgeF said:

Hi

 

Are you sure that in the context of the whole program, the value of "trembler @ 0x47" still needs to be preserved for the remaining code after 0x020B ?

 

Best regards

Jorge

 

Hey Jorge, that could be the answer.  The value in Trembler isn't used after the 2nd occurrence and I guess the compiler is free to overwrite its value if it sees it's no longer required.

Share this post


Link to post
Share on other sites
JorgeF    0

Hi

 

21 minutes ago, jartim said:

Hey Jorge, that could be the answer.  The value in Trembler isn't used after the 2nd occurrence and I guess the compiler is free to overwrite its value if it sees it's no longer required.

 

I think you are guessing right!

And in doing so the compiler saves one instruction, as you can see in your openning post  the second form (not preserved) is one instruction shorter than the first (preserved).

 

 

Best regards

Jorge

 

 

 

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum 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...

×