This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

may be a BUG in Keil c51 ? (32bit calculations)

Look at this program:

/*------------------------------------------------------------------------------*/
#include <reg51.h>
#include <stdio.h>


typedef unsigned long uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;

#define MEMORY xdata

#define StructMEMORY xdata

typedef struct {
  uint16 lArg0;
  uint32 lArg1;
} MyStruct;

uint8 xdata * MEMORY lPtr;

MyStruct StructMEMORY m;

void MyInit() {

 m.lArg0=8;
 m.lArg1=2;

 lPtr=(uint8 xdata *)0xddd3;
}

void bug_0(MyStruct StructMEMORY *p) {
static uint32 MEMORY lResults[4];

 lResults[0]=((uint32)lPtr) + p->lArg1 + (uint32)(p->lArg0);
 lResults[1]=p->lArg1 + (uint32)(p->lArg0) + ((uint32)lPtr);


 lResults[2]=((uint32)lPtr);
 lResults[2]+=p->lArg1 + (uint32)(p->lArg0);

 lResults[3]=(uint32)(lPtr+p->lArg1 + (uint32)(p->lArg0));

 if (lResults[0]==lResults[1] && lResults[1]==lResults[2] && lResults[2]==lResults[3])
    printf("\nNo problems.");
  else
    for(;/* BUG */;);

}

void main() {

 MyInit();
 bug_0(&m);
}
/*------------------------------------------------------------------------------*/

Try to compile it and run it. You will get the endless loop.

i used a 7.01 ver. of c51

Project sets:
Target i8051AH
[v]Use on-chip rom
off-chip xdata memory: 0x0000-0x01ff
memory model: small, code rom size: small

Michael S.

Parents Reply Children
  • I don't understand what you want to say, but I know aboud big and small endian.

    the next code:

    {
     long a1,a2,b,c,d;
    
     b=14444;c=3;d=4;
     a1=b+c+d;
    
     a2=b;
     a2+=c+d;
    
     if (a1!=a2)
        printf("\ncompiler bug.");
    }
    

    must not print "compiler bug" at any C compiler, if, sure, that compiler works with 32 bit data.

    I don't want to say, that c51 "must die" (etc.), but bug is bug.

  • By the way, look at assembler listing, generated from my source code and you will find interesting thing, the place where compiler hungs and generates wrong assembler directives: multiplication of lPtr by 2, and no actions on calculation of structure fields.....

  • "the next code:

    {
    long a1,a2,b,c,d;

    b=14444;c=3;d=4;
    a1=b+c+d;

    a2=b;
    a2+=c+d;

    if (a1!=a2)
    printf("\ncompiler bug.");
    }


    must not print "compiler bug" at any C compiler, if, sure, that compiler works with 32 bit data."

    I compiled this using 7.01 and it works fine. I've no idea what your first code example above is supposed to achieve, but why do you think that casting pointers to unsigned long is a legitimate (or reasonable) thing to do anyway?

    Stefan