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.
Fixed in revision #9316 using patch.
Philipp