📄 c5116.txt
字号:
发信人: reflection (似水流年), 信区: EEtechnology
标 题: C51 Primer (15) General Things to be Aware of
发信站: 南京大学小百合站 (Wed Nov 24 12:06:30 1999), 转信
15 General Things To Be Aware Of
The following rules will allow the compiler to make the best use of the proc
essor's resources. Generally, approaching C from an assembler programmer's v
iewpoint does no harm whatsoever!
15.1
Always use 8 bit variables the 8051 is strictly an 8 bit machine with no 16
bit instructions. char will always be more efficient than int's.
15.2
Always use unsigned variables where possible. The 8051 has no signed compare
s, multiplies etc., hence all sign management must be done by discrete 8051
instructions.
15.3
Try to avoid dividing anything but 8 bit numbers. There is only an 8 by 8 di
vide in the instruction set. 32 by 16 divides could be lengthy unless you ar
e using an 80C537!
15.4
Try to avoid using bit structures. Until v2.30, C51 did not support these st
ructures as defined by ANSI. Having queried this omission with Keil, the exp
lanation was that the code produced would be very large and inefficient. Now
that they have been added, this has proved to be right. An alternative solu
tion is to declare bits individually, using the "bit" storage class, and pas
s them to a user-written function.
15.5
The ANSI standard says that the product of two 8 bit numbers is also an 8 bi
t number. This means that any unsigned chars which might have to be multipli
ed must actually be declared as unsigned int's if there is any possibility t
hat they may produce even an intermediate result over 255.
However it is very wasteful to use integer quantities in an 8051 if a char c
an do the job! The solution is to temporarily convert (cast) a char to an in
t. Here the numerator potentially could be 16 bits but the result always 8 b
its. The "(unsigned int)" casts ensure that a 16 bit multiply is used by C51
.
{
unsigned char z ;
unsigned char x ;
unsigned char y ;
z = ((unsigned int) y * (unsigned int) x) >> 8 ;
}
Here the two eight bit numbers x and y are multiplied and then divided by 25
6. The intermediate 16 bit (unsigned int) result is permissible because y an
d x have been loaded by the multiplier library routine as int's.
15.6
Calculations which consist of integer operands but which always produce an 8
bit (char ) due to careful scaling result thus:
unsigned int x, y ;
unsigned char z ;
z = x*y/256 ;
will always work, as C51 will equate z to the upper byte (least significant)
of the integer result. This is not machine-dependant as ANSI dictates what
should be done. Also note that C51 will access the upper byte directly, thus
saving code.
15.7 Floating Point Numbers
One operand is always pushed onto an arithmetic stack in the internal RAM. I
n the SMALL model the 8051 stack is used, but in other models a fixed segmen
t is created at the lowest available address above the register bank area. I
n applications where on-chip RAM is at a premium, full floating point maths
really should not be used. Fixed point is a far more realistic alternative.
----------------------------------------------------------------------------
----
--
Ours is essentially a tragic age, so we refuse to take it tragically.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -