Menu

#2417 pic14: Adding 16-bit variable and literal produces incorrect result and corrupts variable

closed-fixed
None
PIC14
5
2015-09-12
2015-09-12
No

Adding a 16-bit literal where low byte is 0x00 with a 16-bit variable causes incorrect code to be output.

$ cat test.c

#include <stdint.h>

uint16_t u16result;
uint16_t u16left;

void main(void)
{
    u16result = u16left + 0x800;
}

$ sdcc -v

SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.5.4 #9310 (Linux)

To compile:

$ sdcc --use-non-free -mpic14 -p16f887 test.c

Resulting assembly for the line with the addition:

;       .line   8; "test.c"     u16result = u16left + 0x800;
    BANKSEL _u16left
    MOVF    _u16left,W
    BANKSEL _u16result
    MOVWF   _u16result
    MOVLW   0x08
    BANKSEL _u16left
    ADDWF   (_u16left + 1),W
    MOVWF   (_u16left + 1)

The last instruction overwrites the high byte of u16left. It should be writing to high byte of u16result instead.

Fix: Patch attached.

1 Attachments

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    Fixed in revision #9316 using patch.

    Philipp

     

Log in to post a comment.

MongoDB Logo MongoDB