#1503 PIC14: wrong variable write order

open
nobody
PIC14
5
2013-07-16
2009-01-31
No

Hello,
I found following small test program causes strange behavior.

-------------source code start-------------
#include <sdcc-lib.h>
#include <pic16f84a.h>

typedef unsigned char byte;

static volatile byte v = 0;

static void b() {
v = 2;
}

static void a(byte n) {
b();
v = n;
}

int main() {
a(1);
/* at this point, variable 'v' should be 1,
but 'v' becomes 2. */
for(;;);
}
-------------source code end-------------

I used following command to compile this:

$ /home/takahiro/workspace/pic/sdcc/bin/sdcc -V -mpic14 -p16f84a test.c

The compiler version is:
$ /home/takahiro/workspace/pic/sdcc/bin/sdcc -v
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.8.8 #5350 (Jan 31 2009) (UNIX)

The compiler wrote following assembly (complete assembly list attached) :

MOVLW 0x01
MOVWF _v
MOVLW 0x02
MOVWF _v
_00114_DS_
GOTO _00114_DS_
RETURN

So at the point just before the infinite loop, variable 'v' becomes 2 (should be 1).

Discussion

  • assembly output

     
    Attachments
  • Can't reproduce this on Z80, changing category to pic14.

    Philipp

     
    • labels: --> pic14 target
     
  • Maarten Brock
    Maarten Brock
    2011-09-18

    • summary: wrong variable write order --> PIC14: wrong variable write order
     
    • Category: --> PIC14