Jump to content

Incorrect code generated

Recommended Posts

Bug description:

The compiler generates incorrect code for the following statement -

while (c -= 1)


Steps to reproduce:

My source code is this -

#define BSIZE 120
unsigned char RxBuffer[BSIZE]@0x100;
unsigned char TxBuffer[BSIZE]@0x200;
unsigned char RamSwapArea[16U]@0x300;

void main()
	asm lfsr 1, _RxBuffer
	asm lfsr 2, _TxBuffer
	unsigned char c = BSIZE;
	while (c -= 1)
		asm movff _postinc1, _postinc2

The bug is the statement "while (c -= 1)" generates an INFSNZ instruction, even though variable c is being decremented.

Here is the disassembly listing - 

13:            #define BSIZE 120
14:            unsigned char RxBuffer[BSIZE]@0x100;
15:            unsigned char TxBuffer[BSIZE]@0x200;
16:            unsigned char RamSwapArea[16U]@0x300;
18:            void main()
19:            {
20:            	asm lfsr 1, _RxBuffer
0008  EE11     LFSR 1, 0x100
000A  F000     NOP
21:            	asm lfsr 2, _TxBuffer
000C  EE22     LFSR 2, 0x200
000E  F000     NOP
23:            	unsigned char c = BSIZE;
0010  0E78     MOVLW 0x78
0012  6E01     MOVWF c, ACCESS
24:            	while (c -= 1U)
0014  4A01     INFSNZ c, F, ACCESS
001C  D7FB     BRA 0x14
25:            	{
26:            		asm movff _postinc1, _postinc2
001A  FFDE     NOP
27:            	}
28:            }
0016  0012     RETURN 0

Expected behaviour:

The instruction at address 0014 should be 'DCFSNZ c, F, ACCESS

Is the problem 100% reproduceable:

100% every time


IDE version:   MPLAB-X 4.15 (64-bit)

Compiler:      BoostC++ Optimizing C++ Compiler Version 7.43 (for PIC18 architecture)

Compiler version:     V7.43

Target device: PIC18F8527

OS:                   Windows 10 Pro 64-bit



Have included the source files -


Share this post

Link to post
Share on other sites



I've confirmed the Bug, both for C and C++.

But as I can't solve it, I would suggest 2 alternatives that generate correct code:



while(c = c -1)




Best regards



Edited by JorgeF

Share this post

Link to post
Share on other sites

I can confirm this issue for the BoostC compiler family. The Chameleon compiler however handles this code well.




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.

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