Menu

#1503 PIC14: wrong variable write order

open
nobody
PIC14
5
2023-01-03
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

  • Takahiro WATANABE

    assembly output

     
  • Philipp Klaus Krause

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

    Philipp

     
  • Philipp Klaus Krause

    • labels: --> pic14 target
     
  • Maarten Brock

    Maarten Brock - 2011-09-18
    • summary: wrong variable write order --> PIC14: wrong variable write order
     
  • Philipp Klaus Krause

    • Category: --> PIC14
     
  • Tom Li

    Tom Li - 2023-01-03

    Can confirm the bug still exists as of 2023.

    _main:
    ; 2 exit points
    ;       .line   18; "bug1503.c" a(1);
            MOVLW   0x01
            PAGESEL _a
    _a:
    ;       .line   12; "bug1503.c" static void a(byte n) {
            MOVWF   _v  
    ;       .line   13; "bug1503.c" b();
            PAGESEL _b
    _b:
    ;       .line   9; "bug1503.c"  v = 2;
            MOVLW   0x02
            MOVWF   _v  
            PAGESEL $
            PAGESEL $
    _00115_DS_:
            GOTO    _00115_DS_
    ;       .line   22; "bug1503.c" }
            RETURN
    
     

Log in to post a comment.