Electronic – How many lines of C code (average) corresponds to 1Ko programmable memory

picprogramming

I'd like to know how many lines of C code in average corresponds to 1Ko programmable memory for PIC18F microcontrollers

Or how many lines of assembly instructions ?

Best Answer

Let's take some real world examples. I wrote a simple C program, which I think represents typical C code. Some could argue one way or another that it doesn't, but I think it will make my point.

Here is the C code:

unsigned short m [20];

int main()
{
    unsigned char a, x, i;
    unsigned short b, y;
    unsigned long c, z;

    a = b = c = 0;

    for (i=0; i < 20; i++)
    {
        a++;
        b++;
        c++;

        x = a * 3;
        y = b * 3;
        z = c * 3;

        if (a > 50)
        {
            a = 0;
        }

        m[i] = a / (b / (c - z) * x) + i;
    }
}

In addition to the 8-bit PIC18F asked about by the OP, I also compiled it for three other processors: an 8-bit 8051, an 8-bit PIC16F, and a 32-bit PIC32, and for each I took a look at the disassembly code to get the number of instructions and the number of bytes for each line of C code. Due to the length, I decided not to post the disassembly code here.

For the C code above, the PIC18F used 168 instructions and 336 bytes of code (all machine instructions were two bytes long). That however doesn't include some helper routines since the PIC18F for example doesn't have a hardware divide instruction, so a subroutine call is made for that. Of course these helper routines are shared for all the code in a program. So I'll ignore them since they probably affect the code size in a real program by a few percent.

The PIC16F used 272 instructions and 476 bytes of code (all machine instructions on the PIC16F are 14-bits long, so I converted this to bytes). Like the PIC18F, the PIC16F doesn't have a hardware divide instruction, so it also uses helper routines which were not included in the count.

The 8051 used 99 instructions and 175 bytes of code, or an average of 1.77 bytes per machine instruction. Like the PIC18F, the 8051 doesn't have a hardware divide instruction, so it also uses helper routines which were not included in the count.

The PIC32 used 68 instructions and 272 bytes of code. All machine instructions on the PIC32 take 4 bytes, unlike for example the 8051 where instructions can be variable length: 1, 2 or 3 bytes.

There are 11 lines of executable code in my C program. So that works out to be 15.2 instructions per line of C for the for the PIC18F; 24.7 instructions for the PIC16F; 9 instructions for the 8051; and 6.2 instructions For the PIC32.

As far as program bytes per line of code, the numbers are 30.5 bytes for the PIC18F; 43.3 bytes for the PIC16F; 15.9 bytes for the 8051; and 24.7 bytes for the PIC32. So the 8051 wins in terms of least number of bytes per line of C code.

However, for the three PIC compilers, I was not using the optimized versions (they have three versions of each compiler: free, standard, and pro). I was using the free one. Microchip claims the pro compilers can reduce code space by 60%. In my own work experience with the PIC32 pro compiler, I was getting 40-50% reduction. So I'm going to use 50% here as a compromise.

So the numbers become:

          Instructions/line of code    Bytes/line of code

PIC18F          7.6 (est)                  15.2 (est)                
PIC16F          12.4 (est)                 21.6 (est)
8051            9                          15.9
PIC32           3.1 (est)                  12.3 (est)

Interesting that the number of bytes per line of code is so similar between three of the processors, and not that much higher for the PIC16F.

So to answer the original question, for the PIC18F you get 1024/168 * 11 = 67.0 lines of C per 1K of code; for the PIC16F you can get 1024/272 * 11 = 41.4 lines of C, for the 8051 you can get 1024/175 * 11 = 64.4 lines of C, and for the PIC32, 1024/136 * 11 = 82.8 lines of C. So you get the most number of lines of C using the PIC32. I think the average number of lines of C (which is what the OP asked for) for most programs is probably somewhere between 25 and 120 lines of code depending on the processor and the program.

As far as lines of assembly instructions, for the PIC18F you can get 7.6 * 67.0 = 509 instructions; for the PIC16F you can get 12.3 * 40.4 * 16/14 = 567.9 instructions, for the 8051 you can get 9 * 62.8 = 565.2 instructions, and for the PIC32, 3.1 * 82.8 = 256.7

          Lines of C in 1K bytes    Instructions in 1K bytes

PIC18F          65.5 (est)               497.8 (est)                
PIC18F          40.4 (est)               567.9 (est)
8051            62.8                     565.2
PIC32           80.9 (est)               256.7 (est)

Note the "Instructions in 1K bytes" actually is just over 500 for the PIC18F, and 250 for the PIC32, since they use 2 and 4 bytes instruction respectively. The number for the PIC16F is higher due to the allowance for its 14-bit words.