C51之局部变量和全局变量小记

若在C51中定义一个全局变量,编译器将在RAM中为该变量指定一个专用地址,在C程序中给变量赋的值将存入这个专用地址中,程序操作该变量是,首先从专用地址中取出存放的值,然后再进行计算。全局变量被定义在内存中的专门地址上,存储位置固定。对于频繁存取的重要变量但可以采用全局变量以减少代码的长度;由于全局变量总是占用内存,如果过多,或者把程序处理和计算中的一些中间变量也定义为全局变量,将大大消耗内存空间,处理速度会变慢,同时数据安全性也会降低。

C51中定义一个的局部变量可以和全局变量同名,但在这种情况下,局部变量的优先级较高,而同名的全局变量在该功能模块内暂时被屏蔽。

若在C51中定义一个局部变量,编译器会将该变量的地址分配到寄存器组R0~R7中。由于他是局部变量,所以编译器将使用立即数赋值语句为代表该变量的寄存器Rn赋值,最后的计算结果也将存在寄存器组中,位置有编译器任意指定。局部变量由于用寄存器直接操作,存取速度和计算机速度都很快;由于寄存器数量有限,若局部变量过多,将会使代码由于频繁分配寄存器而变得冗长。


Global variables you create in your C programs are stored in the memory area specified or in the default memory area implied by the memory model. The assembly label for the variable is the variable name. For example, for the following global variables:

unsigned int bob;
unsigned char jim;

the compiler generates the following assembler code:

?DT?MAIN             SEGMENT DATA
         PUBLIC jim
         PUBLIC bob

         RSEG  ?DT?MAIN
            bob:   DS   2
            jim:   DS   1
; unsigned int bob;
; unsigned char jim;

To access these variables in assembler, you must create an extern declaration that matches the original declaration. For example:

EXTERN DATA(jim)

If you use in-line assembler, you may simply use C extern variable declarations to generate the assembler EXTERN declarations.

You may access global variables in assembler using their label names. For example:

MOV A,jim

Note

  • Type information is not transmitted to your assembler routines. Assembly code must explicitly know the type of the global variable and the order in which it is stored.
  • The EXTERN definitions for a C external variable are generated only if the variable is referenced by the C code in the module. If an external variable is only referenced by in-line assembly code, you must declare them in in-line assembly code within that module.

reference:http://www.keil.com/support/man/docs/c51/c51_ap_globalvars.htm

http://www.kar.elf.stuba.sk/predmety/mmp/c51/c02.htm

http://www.kar.elf.stuba.sk/predmety/mmp/c51/c51prim.htm

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C51中,局部变量是指在函数内部定义的变量,只在函数内部有效,函数返回后会被销毁。C51支持多种类型的局部变量,包括auto、idata、xdata、pdata等。 其中,auto类型的局部变量是最常见的一种,也是默认类型,可以不加任何关键字直接声明。auto类型的局部变量存储在栈中,由编译器自动分配和销毁,存储空间和生存周期都与函数调用相关。在函数内部,可以通过变量名来访问auto类型的局部变量,例如: ``` void test() { auto int a; // 声明一个auto类型的局部变量a a = 10; // 对局部变量a进行赋值 } ``` 除了auto类型的局部变量以外,idata类型的局部变量也很常见。idata类型的局部变量存储在内部RAM中,可以提高程序的执行速度和节省存储空间。在C51中,可以通过idata关键字来声明idata类型的局部变量,例如: ``` void test() { idata char a; // 声明一个idata类型的局部变量a a = 0x10; // 对局部变量a进行赋值 } ``` 需要注意的是,由于idata类型的局部变量存储在内部RAM中,存储空间有限,因此在使用idata类型的局部变量时需要合理地管理内部RAM的空间,以保证程序的正常执行。 除了auto和idata类型的局部变量以外,C51还支持xdata和pdata类型的局部变量。xdata类型的局部变量存储在外部RAM中,pdata类型的局部变量则是指向程序存储器中的数据。在使用xdata和pdata类型的局部变量时,需要进行相关的初始化和配置。 总之,C51中的局部变量是非常常见的一种变量类型,可以提高程序的灵活性和可读性。合理地使用各种类型的局部变量,可以优化程序的性能和节省存储空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值