[Back] [Top] [Next]

15 General Things To Be Aware Of

The following rules will allow the compiler to make the best use of the processor's resources. Generally, approaching C from an assembler programmer's viewpoint 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 compares, 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 divide in the instruction set. 32 by 16 divides could be lengthy unless you are using an 80C537!

15.4

Try to avoid using bit structures. Until v2.30, C51 did not support these structures as defined by ANSI. Having queried this omission with Keil, the explanation 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 solution is to declare bits individually, using the "bit" storage class, and pass them to a user-written function.

15.5

The ANSI standard says that the product of two 8 bit numbers is also an 8 bit number. This means that any unsigned chars which might have to be multiplied must actually be declared as unsigned int's if there is any possibility that they may produce even an intermediate result over 255.

However it is very wasteful to use integer quantities in an 8051 if a char can do the job! The solution is to temporarily convert (cast) a char to an int. Here the numerator potentially could be 16 bits but the result always 8 bits. 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 256. The intermediate 16 bit (unsigned int) result is permissible because y and 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. In the SMALL model the 8051 stack is used, but in other models a fixed segment is created at the lowest available address above the register bank area. In 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.


[Back] [Top] [Next]