Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
05/22/12 17:42
Read: 2005 times

#187466 - Where can one learn Intermediate C techniques for 8051
C is really frustrating on the 8051, and I'm not sure how to get more proficient. There are many examples which teach basics (here is the memory map, this is a register/timer/port, etc), but where can the intermediate 8051 c programmer turn to see how to structure 8051 applications to overcome the the complexities that emerge in larger code bases (32k - 64k range)? And how about testing strategies for those applications? Unit testing with something like Unity?

Here's a stupid example with 4 possible implementations of a table look-up function:

typedef unsigned char U8;
typedef unsigned char S8;
typedef unsigned int U16;

#define UNITS_PER_GROUP  8

unsigned char code ID_TBL[NUMBER_OF_GROUPS * UNITS_PER_GROUP] = {
    0x01, 0x02, 0x03, 0x04, 0x65, 0x66, 0x67, 0x68, /* Group 01 */
    0x05, 0x06, 0x07, 0x08, 0x69, 0x70, 0x71, 0x72, /* Group 02 */
    0x09, 0x10, 0x11, 0x12, 0x73, 0x74, 0x75, 0x76, /* Group 03	*/
    0x13, 0x14, 0x15, 0x16, 0x77, 0x78, 0x79, 0x80, /* Group 04	*/
    0x17, 0x18, 0x19, 0x20, 0x81, 0x82, 0x83, 0x84, /* Group 05 */
    0x21, 0x22, 0x23, 0x24, 0x85, 0x86, 0x87, 0x88, /* Group 06 */
    0x25, 0x26, 0x27, 0x28, 0x89, 0x90, 0x91, 0x92, /* Group 07 */
    0x29, 0x30, 0x31, 0x32, 0x93, 0x94, 0x95, 0x96, /* Group 08 */
    0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40	/* Group 09 */

typedef struct {
    U8 num;
    U16 price;
    U8 num_units;
    U8 unit_status;
    /* char code *name; */
} group_t;

/* code size = 19 bytes */
U8 get_unit_id1(group_t xdata *pgr, U8 unit_indx)
    U8 idata x;

    x = (pgr->num - 1) * UNITS_PER_GROUP;
    return ID_TBL[x + unit_indx];

/* code size = 20 bytes */
U8 get_unit_id2(group_t xdata *pgr, U8 unit_indx)
    U8 idata x;

    x = ((pgr->num - 1) * UNITS_PER_GROUP) + unit_indx;
    return ID_TBL[x];

/* code size = 25 bytes */
U8 get_unit_id3(group_t xdata *pgr, U8 unit_indx)
    U8 idata x;

    x = pgr->num - 1;
    x += unit_indx;
    x = ID_TBL[x];
    return x;

/* code size = 33 bytes */
U8 get_unit_id4(group_t xdata *pgr, U8 unit_indx)
    return ID_TBL[((pgr->num - 1) * UNITS_PER_GROUP) + unit_indx];


At any compiler optimization level (Keil C51 V9.01) the same logical code could cost you between 19 and 33 bytes of code depending on how you feel like writing it. Interestingly, the most "idiomatic" C way costs the most, which has been my experience trying to implement C techniques on the 8051. And all of this is just for a "simple" look-up.

The frustrating part is I'm not exactly sure what I'm supposed to learn from this exercise, since the various cases are just various attempts to use lower level constructs in order to help the compiler out. It is certainly not obvious what the best (meaning efficient use of code space in this context) approach might be in other cases. The answer seems to be that one must fiddle with absolutely everything one writes to see what the compiler will give in the specific circumstance. It's good advise, but very distracting when trying to solve the actual application problem.

However, the real point of this example was that one must start asking questions such as "Why don't you just expose the table directly?", and "Is passing around pointers to structs even necessary for this application?". These are the questions that I don't know how to answer. They are really design decision questions which will affect hundreds of little implementation decisions like the example above.

How does one effectively learn the very specific craft of 8051 C programming absent some kind of mentor or larger well-written 8051 projects to study?

List of 33 messages in thread
Where can one learn Intermediate C techniques for 8051      David Good      05/22/12 17:42      
   on the right track        Maarten Brock      05/23/12 06:11      
      one more thing      Erik Malund      05/23/12 07:15      
         Not uncommon bid bad coding standards to comply with      Per Westermark      05/23/12 08:14      
         Not afraid of globals, but...      David Good      05/23/12 10:07      
   More keil optimizer interesting tidbits      David Good      05/24/12 16:24      
      optimization      Maarten Brock      05/25/12 05:35      
      nothing gained, nothing lost      Erik Malund      05/25/12 07:53      
         I don't      Per Westermark      05/25/12 07:58      
            you can do both      Erik Malund      05/25/12 09:44      
               That is not helping the compiler      Justin Fontes      05/25/12 16:31      
                  exact same      Maarten Brock      05/26/12 03:36      
                     Technically, they are not the same      Justin Fontes      05/26/12 11:17      
                        Hmmm...      Andy Neil      05/26/12 23:46      
                        C don't do full evaluation of logical expressions      Per Westermark      05/28/12 06:33      
                           In discrete mathematics proving one is not a proof      Justin Fontes      05/28/12 14:02      
                              Lazy evaluation demanded        Per Westermark      05/28/12 14:51      
                                 I have learned something new because of this      Justin Fontes      05/28/12 17:54      
                                    me too      Andy Peters      06/01/12 11:43      
                                       Very Important      Michael Karas      06/01/12 14:30      
                              Breaks.      Michael Karas      05/28/12 17:57      
                                 Compile the code      Justin Fontes      05/28/12 18:28      
                                    To be more exact      Justin Fontes      05/28/12 19:11      
                                       Stop It!!        Michael Karas      05/28/12 22:01      
                                          That is exactly what I intended      Justin Fontes      05/29/12 10:16      
                                             Switch Break.      Michael Karas      05/29/12 22:04      
               obfusciating code to help the compiler is a VERY bad idea      Andy Neil      05/27/12 07:13      
                  the source of this      Erik Malund      05/27/12 07:17      
   Where can one learn Intermediate C techniques for 8051      Andy Neil      05/26/12 23:56      
   Getting the least out of your compiler        Andy Neil      05/27/12 07:25      
      Maybe IAR should follow their own advice?      Christoph Franck      05/30/12 02:05      
         provided the case ...      Maarten Brock      05/30/12 06:29      
            Compilers not knowing the target chip.      Christoph Franck      05/31/12 03:19      

Back to Subject List