- 打卡等级:以坛为家II
- 打卡总天数:524
- 最近打卡:2025-05-04 02:13:29
论坛元老
- 积分
- 5133
|
发表于 2024-5-10 20:42:46
|
显示全部楼层
移植 C51 代码的技巧
移植 C51 代码时必须考虑 C51 和 C251 之间的一些细微差别。 以下是您可能遇到的典型问题的列表:
C251 生成的代码独立于当前选择的寄存器组,并且不再需要 C51 指令 NOAREGS 和 REGISTERBANK。
内存类型说明符必须设置在变量类型后面,如下例所示:
int xdata value; /* correct in both C51 and C251 C51 和 C251 均正确 */
xdata int value; /* illegal: old C51 form; not supported by C251 非法:旧的C51表格; C251 不支持 */
pdata 内存类型通常在 251 MCU 的 PAGE 模式下出现问题,在新应用中应忽略。
printf 库函数和变量参数列表默认提升为 int,即使对于 char 和 unsigned char 变量也是如此。 以下 printf 语句在 C251 中生成不正确的结果。
unsigned char c1;
/* works in C51, but gives wrong output in C251: 在 C51 中工作,但在 C251 中给出错误的输出: */
printf ("%bx %bi", c1, 1);
/* C251 defaults to int and does not need the b prefix: C251默认为int,不需要b前缀:*/
printf ("%x %i", c1, 1);
/* the following two forms work in both C51 and C251: 以下两种形式在 C51 和 C251 中均适用: */
printf ("%bx %bi", (unsigned char) c1, (char) 1); /* optimal code */
printf ("%x %i", (unsigned int) c1, (int) 1); /* passes int */
const 常量
在 ANSI C 中,const 类型限定符用于定义和访问常量且不可更改的对象。 用 const 声明的变量不能在程序中赋值。
C251 编译器符合 const 对象的 ANSI 定义。
单独使用 const 类型限定符声明的变量存储在与其定义关联的内存区域(data、idata、xdata 等)中。
要在 ROM 中定位的变量必须使用代码内存类型进行声明。 例如:
code char test[] = "This is a text string";
用 const 声明的变量存储在 xCONST 内存类中(内存类型 data、idata、xdata 除外)。 有关详细信息,请参阅段命名约定。
对于使用 const 声明的变量,编译器会生成存储在 ROM 中的映像。 不会为此类变量生成运行时初始化记录。
常量对象通常在定义时(在源文件中)进行初始化。 以下变量定义显示了创建常量对象的不同方法:
/* table is stored in the default memory class */
const int table[2][2] =
{ 0, 2, 4, 8 };
/* pi is stored in the HCONST memory class */
const float far pi = 3.1415927;
/* The string is stored in the HCONST memory class */
printf("This is a string\n");
当使用指向 const 对象的指针时,可以在指针定义中排除 const 类型限定符。 例如:
const unsigned char mask [] =
{ 0x01, 0x02, 0x04, 0x08 };
const unsigned char *cp = mask;
unsigned char *p = mask; /* same as cp */
.
.
.
*p = 'a'; // This has no effec.
// It causes no error or warning
*cp = 'a'; // This causes an error
如上所示,可以将 const 对象(mask)的地址分配给非常量指针(p),然后使用该指针更改 const 对象。 在这种情况下,编译器确实会生成写入 const 对象的代码。 此代码的效果未定义,可能会或可能不会按预期工作。
不可能使用 const 指针来更改它指向的 const 对象。 尝试这样做将导致编译器错误。
const 的一个有趣的用途是定义一个不可更改的指针。 例如:
char text [] = "This is a string.";
char *const textp = text;
.
.
.
*textp = 'A'; // This is OK (it changes text[0])
textp++; // This causes an error (textp is const)
textp[2] = 'B'; // This is OK (it changes text[2])
|
|