Written Program Size: A Comparison of C vs. Assembly

Discussion in 'Embedded Systems and Microcontrollers' started by ke5nnt, Dec 28, 2011.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Greetings again,

    As I get more comfortable writing programs for PIC MCUs in both Assembly and C, I have come to notice one thing that bothers me. It seems that programs written in the C language take up a whole lot more program memory space than do programs which are written in Assembly.

    To be clear and specific, my setup includes MPLAB IDE with MPASM for assembler programs, and MPLAB IDE with High-Tech PICC-Lite compiler for C programs.

    As I started to notice this huge discrepancy between the program size between the 2 languages, I did an experiment. I took a program I had written in assembler and I rewrote it using C. With a PIC12F629 which has 1K program memory size, my assembler program uses a little over 580 words. The same program written for the same MCU in C cannot even be fully written before I use all 1K words. As a matter of fact, I'm barely halfway through writing the program in C.

    That being said, here are my questions:
    1. Is the overwhelming size of the C program a result of using the "lite" version. I know that there is a notice when I compile that says something along the lines of Omniscient code-generation is not available in the "lite" mode, and that my program could be 40% smaller. So okay, there's something, but still, the Assembler version uses 580 words, or a little over half the program memory, I get ~50% of the program written in C before I'm out of space, even if that was reduced by 40%, I still would be pushing 100% memory usage with a completed program. This furthers my speculation that C uses more space than assembler.

    2. I could also speculate that perhaps I'm just not a very efficient C programmer, and maybe that's why my C program is so large. I could understand this considering I'm pretty noob to programming in general. I realize that no one can really verify or discredit this speculation without me posting some sort of code example, and I'm sorry to say that I cannot right now since I'm not on my own computer. If it's important to you though, I can post something later.

    3. Is there a way (other than buying the pro version of High-Tech PICC) to reduce the size of my C programs while still accomplishing the same tasks? C programming seems so much easier than writing in assembly and takes far less time. Assembly seems quite cumbersome to me.

    Thank you for all the time you folks put in to helping me.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    This is not unusual but a lot depends on the specific MCU and the compiler. Some MCU architectures are better designed to produce efficient compiled code. Atmel AVR was specifically designed on this basis.

    Secondly, not all compilers create compact code. Optimization usually requires multiple passes in order to compact the code and remove code that will never be executed.

    Finally, in your comparison, make sure that you do not call any standard C functions such as floating point, strings and I/O. Write your own equivalent library functions.

    Make sure the you use the smallest data type, for example, byte instead of int.

    Some C code can result in code bloat.
    For example, if you had to test a single bit in a status word using

    if ( ( status & bit_mask) == 0 )

    may get translated into four or more instructions while one would do.
    (If you want compact code, try AVR.)
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    A C program will always be longer that a well written assembler program but there is a lot you can do to make your programming more efficient.

    Use the smallest variable type that you can, I.E. where possible use "char" rather than "int". Also use unsigned variable types where-ever possible in preference to signed. Be careful, many compilers default to signed variables.

    Avoid floating point variables like the plague. Try to confine any calculations to integer arithmetic.

    Avoid using global variables unless absolutely necessary, global variables eat up RAM.

    Be careful what library functions you use. Functions such as printf() can eat up your resources at a great rate of knots.

    Many "free" versions of C compilers turn off or limit code optimisation which produces the sort of code bloat that you are seeing. I'm sorry to say that the only solution is to buy full version C compiler but there are cheaper products than HiTechC. Take a look at SourceBoostC. It's a lot cheaper and does quite a good job of code optimisation. It does have a couple of very minor limitations (poor floating point support and no bit-field support) but for the price you can live with that, I certainly can as it's the compiler that I use most of the time.
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    I guess we cross posted. Great minds think alike.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Then you upgrade to a newer version of HI-Tech C. You will get 30 days of pro mode for evaluation. Also if you have access to some other computer. You may install MPLAB and HI-Tech C. And get the same 30 days period.
    In MPLAB toolbar go to project->Build Options->project Find the linker tab. And unselect the calibrate oscillator option. That may shave off some code.
    I also suggest that you post your code. You are limited by the PIC instruction set. So even if you write code that looks compact in C. It may come up as awkward assembler code.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    In the dawn of the computer era, 1947-1970 computers were expensive ($600/hr.) and people were cheap ($5.00/hr) so assembly language programming that optimized the use of the machine resources was worth the manpower effort to produce.

    Fast forward to 2011. Few people except lawyers and hedge fund managers make $600/hr, but $5.00/hr will buy a wheelbarrow full of microchips and memory each and every week.

    So how much effort is now justified to to write a highly optimized piece of assembly language code that delays the product launch by 6 months versus a chunk of C-code that you can bang out in an afternoon. The machines don't care what language we write code in, they are happy as pigs in slop executing whatever we throw at them.

    Is assembly language coding fun, challenging, interesting and deeply satisfying to produce?

    Hex yeah it is!

    That should be enough reason to do it.
     
  7. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    This is what I cannot understand about PICs. For AVR, you get a nice compiler that does everything you need plus tons of libraries for free. With PIC you get some light version of compiler and basically for anything larger than blinking a LED you need to buy the full version. Any ideas why?
     
  8. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Agreed!

    Further, higher level languages tend to obscure the underlying hardware. Unlike PC programming, with MCUs you *are* interfacing with the hardware, usually without the benefit of an underlying OS. Asm has a tendency to force you to think harder about hardware than C, to a better result, IMHO.

    Also, many C programmers have been taught application programming, which is quite a bit different than hardware programming. I bugs me to no end when I see a programmer write something like:

    pay = rate * time;

    for an embedded processor!
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    What is wrong with that statement?
     
  10. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Sorry, I should have put a smiley after that.

    I meant, in a tongue-and-cheek way, that "application" trained programmers tend to approach the problem in a way typical of application programming.

    Generally, this is not effective or efficient for embedded processor programming.

    I used pay=rate * time simply because this is usually one of the first exercises a programming student experiences.
     
  11. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Further, the implementation of

    pay = rate * time;

    is entirely dependent upon the compiler and the data types used. Often there are optimizations that can be done at the assembly level that the compiler cannot preform, simply because it cannot know the underlying nature of the data used in the calculation.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Most commercial AVR compilers do have limited code size in free mode. About 2K. Microchip have a somewhat different approach. They have unlimited code size. But poorer code optimization in the free mode. Most of the PICs in the 14 to 28 pin range do have plenty of flash program memory. Even the obsolete but sadly heavily used 16f84A do have 3.5 KB flash memory. Microchip have also starting from the PIC18 series made the core more optimized for the use of C-compilers and also included a 8x8 hardware multipler.
    However I recommend everybody to use the chip vendor and compiler setup they feel most comfortable with. For my hobbyist use I selected Microchip. The reason was the cheap PICKIT 2 programmer/debugger and a store with a good selection of PICs nearby.
     
  13. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    That may very well be for AVR commercial compilers but there is also a version of GCC ported for AVR chips. It is an excellent totally free compiler that comes without any restrictions and is totally ANSI compliant unlike most commercial products which claim compliance but fall far short of it in practice.
     
  14. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Thanks for all the responses guys, it's good info. The next couple of posts will show my code. The program is the same for both assembly and C, and it was a project for a LED traffic light. The traffic light operates in 3 separate modes depending on the position of a single pole double throw (SPDT) switch. The modes are normal cycle operation (switch at center "off" position) that cycles through the red/yellow/green at given intervals. A night mode where the main traffic lanes are flashing yellow while the intersecting street flashes red, and a night mode where all lanes flash red. Additionally, when the switch goes back to a neutral position from one of the night modes, the light enters a transition period that cycles traffic safely back to normal operation. The object here was to never produce an unsafe traffic condition. Code comments should be helpful with this program. It was one of the first programs I wrote in Assembly, and the first I wrote in C.

    Assembly: PIC16F628A; Program size: 140 words, data memory bytes: 0
    Code ( (Unknown Language)):
    1.  
    2. ;************************************************************************
    3. ;TRAFFIC CONTROL SIGNAL                                                    *
    4. ;THIS SIGNAL IS INTENDED TO CONTROL TRAFFIC AT A 4-WAY INTERSECTION        *
    5. ;WITH THE "MAIN" ROAD HAVING A GREEN ARROW AND INDEPENDENT YELLOW        *
    6. ;CONTROL FOR STOPPING TURN TRAFFIC.  SECONDARY ROAD IS STANARD R-Y-G    *
    7. ;LIGHTS.  THIS PROGRAM ALSO HAS 2 "NIGHT MODE" OPERATIONS WHERE            *
    8. ;MAIN LANES FLASH YELLOW WHILE SECONDARY ROAD FLASHES RED, OR            *
    9. ;4-WAY FLASHING RED DEPENDING ON POSITION OF A PERIPHERAL SWITCH        *
    10. ;                                                                        *
    11. ;CREATED BY RYAN GOFF FOR THE PIC16F628A                                *
    12. ;************************************************************************
    13. ;CONFIGURATION
    14.     LIST        P=PIC16F628A
    15.     #INCLUDE    P16F628A.INC
    16.     __CONFIG B'10000100000000'
    17.     ERRORLEVEL    -302
    18. ;
    19.     ORG        01
    20.     GOTO    INIT
    21. ;
    22.     ORG        04
    23.     GOTO    INIT
    24. ;
    25.     COUNT1    EQU    22
    26.     COUNT2    EQU    23
    27. ;*************************************************************************
    28. ;SUBROUTINES
    29. T500MS    MOVLW    0x32
    30.         MOVWF    COUNT1
    31.         MOVLW    0x04
    32.         MOVWF    COUNT2
    33. T500MS1    DECFSZ    COUNT1        ;500MS DELAY FOR NIGHT MODE FLASHING
    34.         GOTO    $+2
    35.         DECFSZ    COUNT2
    36.         GOTO    T500MS1
    37.         RETURN
    38. ;
    39. T3SEC    MOVLW    0xBF
    40.         MOVWF    COUNT1
    41.         MOVLW    0x13
    42.         MOVWF    COUNT2
    43. T3SEC1    DECFSZ    COUNT1        ;3 SECOND DELAY FOR 4-WAY RED
    44.         GOTO    $+2
    45.         DECFSZ    COUNT2
    46.         GOTO    T3SEC1
    47.         RETURN
    48. ;
    49. T4SEC    MOVLW    0xFF
    50.         MOVWF    COUNT1
    51.         MOVLW    0x19
    52.         MOVWF    COUNT2
    53. T4SEC1    DECFSZ    COUNT1        ;4 SECOND DELAY FOR YELLOW
    54.         GOTO    $+2
    55.         DECFSZ    COUNT2
    56.         GOTO    T4SEC1
    57.         RETURN
    58. ;
    59. T10SEC    MOVLW    0xFF
    60.         MOVWF    COUNT1
    61.         MOVLW    0x40
    62.         MOVWF    COUNT2
    63. T10SEC1    DECFSZ    COUNT1        ;10 SECOND DELAY FOR GREEN TURN ARROW
    64.         GOTO    $+2
    65.         DECFSZ    COUNT2
    66.         GOTO    T10SEC1
    67.         RETURN
    68. ;
    69. T20SEC    MOVLW    0xFF
    70.         MOVWF    COUNT1
    71.         MOVLW    0x80
    72.         MOVWF    COUNT2
    73. T20SEC1    DECFSZ    COUNT1        ;20 SECOND DELAY FOR GREEN LIGHT
    74.         GOTO    $+2
    75.         DECFSZ    COUNT2
    76.         GOTO    T20SEC1
    77.         RETURN
    78. ;************************************************************************
    79. ;PROGRAM INITIALIZATION
    80. INIT    BSF        STATUS,5    ;BANK 1
    81.         MOVLW    B'00000000'
    82.         MOVWF    TRISB        ;ALL PORT B ARE OUTPUT
    83.         MOVLW    B'00000011'
    84.         MOVWF    TRISA        ;RA0 AND RA1 INPUT, OTHERS OUTPUT
    85.         BCF        STATUS,5    ;BANK 0
    86.         MOVLW    B'00000111'
    87.         MOVWF    CMCON        ;COMPARATORS OFF
    88.         BTFSC    PORTA,0        ;NIGHT MODE DESIRED?
    89.         GOTO    NMRY        ;FLASH RED/YELLOW
    90.         BTFSC    PORTA,1        ;OTHER NIGHT MODE DESIRED?
    91.         GOTO    NMRR        ;FLASH RED/RED
    92. ;*************************************************************************
    93. ;NORMAL SIGNAL OPERATION
    94. NORM    MOVLW    B'00001001'
    95.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    96.         CALL    T3SEC        ;FOR 3 SECONDS
    97.         MOVLW    B'01001001'
    98.         MOVWF    PORTB        ;MAIN LANE TURN ARROW GREEN, ALL OTHERS RED
    99.         CALL    T10SEC        ;FOR 10 SECONDS
    100.         MOVLW    B'00011001'
    101.         MOVWF    PORTB        ;MAIN LANE YELLOW, ALL OTHERS RED
    102.         CALL    T4SEC        ;FOR 4 SECONDS
    103.         MOVLW    B'00001001'
    104.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    105.         CALL    T3SEC        ;FOR 3 SECONDS
    106.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    107.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    108.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    109.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    110.         MOVLW    B'10000001'
    111.         MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, TURN ARROWS OFF, SECONDARY RED
    112.         CALL    T20SEC        ;FOR 20 SECONDS
    113.         MOVLW    B'00010001'
    114.         MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW + SECONDARY RED
    115.         CALL    T4SEC        ;FOR 4 SECONDS
    116.         MOVLW    B'00001001'
    117.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    118.         CALL    T3SEC        ;FOR 3 SECONDS
    119.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    120.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    121.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    122.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    123.         MOVLW    B'00001100'
    124.         MOVWF    PORTB        ;SECONDARY GREEN, MAIN LANES RED
    125.         CALL    T20SEC        ;FOR 20 SECONDS
    126.         MOVLW    B'00001010'
    127.         MOVWF    PORTB        ;SECONDARY YELLOW, MAIN LANES RED
    128.         CALL    T4SEC        ;FOR 4 SECONDS
    129.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    130.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    131.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    132.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    133.         GOTO    NORM        ;OR REMAIN IN NORMAL CYCLE OPERATION
    134. ;**************************************************************************
    135. ;R/Y NIGHT MODE OPERATION.  PORTA 0
    136. NMRY    MOVLW    B'00010000'
    137.         MOVWF    PORTB        ;MAIN LANES ALL YELLOW, ALL OTHERS OFF
    138.         CALL    T500MS        ;FOR 1/2 SECOND
    139.         MOVLW    B'00000001'
    140.         MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
    141.         CALL    T500MS        ;FOR 1/2 SECOND
    142.         BTFSS    PORTA,0        ;STILL WANT R/Y NIGHT MODE?
    143.         GOTO    TRANS        ;IF NO, TRANSITION TO NORMAL CYCLE MODE
    144.         BTFSC    PORTA,1        ;SWITCH TO R/R NIGHT MODE?
    145.         GOTO    NMRR        ;THEN GO TO R/R NIGHT MODE
    146.         GOTO    NMRY        ;OR STAY IN THIS NIGHT MODE
    147. ;**************************************************************************
    148. ;R/R NIGHT MODE OPERATION.  PORTA 1
    149. NMRR    MOVLW    B'00001000'
    150.         MOVWF    PORTB        ;MAIN LANES RED, ALL OTHERS OFF
    151.         CALL    T500MS        ;FOR 1/2 SECOND
    152.         MOVLW    B'00000001'
    153.         MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
    154.         CALL    T500MS        ;FOR 1/2 SECOND
    155.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    156.         GOTO    NMRY        ;THEN GO TO R/Y NIGHT MODE
    157.         BTFSS    PORTA,1        ;R/R NIGHT MODE STILL DESIRED?
    158.         GOTO    TRANS        ;NO, TRANSITION TO NORMAL CYCLE MODE
    159.         GOTO    NMRR        ;YES, STAY IN R/R NIGHT MODE
    160. ;**************************************************************************
    161. ;TRANSITION FROM NIGHT MODE TO NORMAL
    162. TRANS    MOVLW    B'10000001'
    163.         MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, SECONDARY RED
    164.         CALL    T10SEC        ;FOR 10 SECONDS
    165.         MOVLW    B'00010001'
    166.         MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW. SECONDARY RED
    167.         CALL    T4SEC        ;FOR 4 SECONDS
    168.         MOVLW    B'00001001'
    169.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    170.         CALL    T3SEC        ;FOR 3 SECONDS
    171.         MOVLW    B'00001100'
    172.         MOVWF    PORTB        ;SECONDARY GREEN, MAIN LANES RED
    173.         CALL    T20SEC        ;FOR 20 SECONDS
    174.         MOVLW    B'00001010'
    175.         MOVWF    PORTB        ;SECONDARY YELLOW, MAIN LANES RED
    176.         CALL    T4SEC        ;FOR 4 SECONDS
    177.         GOTO    NORM        ;GO TO NORMAL CYCLE OPERATION
    178. ;**************************************************************************
    179.         END
     
  15. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    And the C equivalent. Program size: 292 words, Data Memory: 5 Bytes. I find it interesting that in Assembly, 0 bytes of data memory get used while C uses 5 bytes.
    Code ( (Unknown Language)):
    1.  
    2. //
    3. //    6-LIGHT, 4-WAY INTERSECTION TRAFFIC CONTROL SIGNAL
    4. //    RYAN GOFF
    5. //    JAN-14-2011
    6. //    PIC16F628A
    7. //    HIGH-TECH C COMPILER, MPLAB IDE
    8. //
    9. //    THIS PROGRAM IS FOR A 6-LIGHT INTERSECTION TRAFFIC CONTROL
    10. //    SIGNAL WHERE BOTH THE MAIN TRAFFIC LANES (MAIN) AND SECONDARY
    11. //    TRAFFIC LANES (SDRY) HAVE A SINGLE SET OF RED, YELLOW, GREEN
    12. //    LIGHTS WHICH WILL CYCLE TO SAFELY CONTROL TRAFFIC WHILE
    13. //    OPERATED IN THE NORMAL CYCLE.  THE SIGNAL ALSO HAS 2 NIGHT-
    14. //    MODE CYCLES WHERE MAIN TRAFFIC LANES RECEIVE A FLASHING YELLOW
    15. //    SIGNAL WHILE SECONDARY TRAFFIC LANES RECEIVE A FLASHING RED
    16. //    SIGNAL, OR ALL TRAFFIC LANES RECEIVE A FLASHING RED SIGNAL.
    17. //    SELECTION OF EITHER OF THESE NIGHT-MODE CYCLES IS DEPENDENT ON
    18. //    THE POSITION OF A SINGLE ON-OFF-ON SWITCH WHICH WILL INPUT
    19. //    HIGH TO RA0 OR RA1.  WHEN BOTH INPUTS ARE LOW, NORMAL CYCLE
    20. //    OPERATION RESULTS.  THE SIGNAL IS PROGRAMMED TO NEVER ALLOW AN
    21. //    UNSAFE SIGNAL CONDITION, THEREFORE, TRANSITION FROM NORMAL
    22. //    OPERATION TO A NIGHT-MODE CYCLE OR VISE-VERSA IS NOT
    23. //    INSTANTANEOUS, AND WILL OCCUR ONLY AT THE PROGRAMMED SAFE
    24. //    POINT.
    25. //
    26. /****************************************************************/
    27.  
    28. // CONFIGURATION
    29.  
    30. #include <htc.h>
    31. #define _XTAL_FREQ 48000
    32. __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_OFF & LVP_OFF & CP_OFF & CPD_OFF);
    33.  
    34. //    INTERNAL 48KHz CLOCK, PWRT, NO PROTECTION
    35.  
    36. /***************************************************************/
    37.  
    38. // GLOBAL VARIABLES
    39.  
    40. unsigned char count;
    41.  
    42. /***************************************************************/
    43.  
    44. void main()
    45. {
    46.  OSCF=0;                    //LOW POWER 48KHZ INTOSC
    47.  CMCON=0x07;                //COMPARATORS OFF
    48.  TRISA=0x03;                //RA0 AND RA1 INPUT, OTHERS OUTPUT
    49.  TRISB=0;                    //ALL PORT B ARE OUTPUTS
    50.  
    51.  while(1)
    52.  {
    53.     while (RA0==1)            //WHILE INPUT AT RA0, NMRR
    54.     {
    55.      PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
    56.      __delay_ms(500);
    57.      PORTB=0x01;            //RB0 ON, MAIN RED, ALL OTHERS OFF
    58.      __delay_ms(500);
    59.     }
    60.     while (RA1==1)            //WHILE INPUT AT RA1, NMRY
    61.     {
    62.      PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
    63.      __delay_ms(500);
    64.      PORTB=0x02;            //RB1 ON, MAIN YELLOW, ALL OTHERS OFF
    65.      __delay_ms(500);
    66.     }
    67.     if ((RA0!=1)&&(RA1!=1))    //RA0 AND RA1 NO INPUT
    68.     {
    69.      PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
    70.         for (count=0; count<3; count++)
    71.         {
    72.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
    73.         }
    74.      PORTB=0x0C;            //RB2 ON, RB3 ON, MAIN GREEN, SDRY RED
    75.         for (count=0; count<20; count++)
    76.         {
    77.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 20 TIMES, 20-SECOND DELAY
    78.         }
    79.      PORTB=0x0A;            //RB1 ON, RB3 ON, MAIN YELLOW, SDRY RED
    80.         for (count=0; count<4; count++)
    81.         {
    82.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
    83.         }
    84.      PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
    85.         for (count=0; count<3; count++)
    86.         {
    87.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
    88.         }
    89.      PORTB=0x21;            //RB0 ON, RB5 ON, MAIN RED, SDRY GREEN
    90.         for (count=0; count<15; count++)
    91.         {
    92.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 15 TIMES, 15-SECOND DELAY
    93.         }
    94.      PORTB=0x11;            //RB0 ON, RB4 ON, MAIN RED, SDRY YELLOW
    95.         for (count=0; count<4; count++)
    96.         {
    97.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
    98.         }
    99.     }
    100.  }
    101. }
    I also find the disassembled C program quite interesting, lots of "GOTO" instructions. I intended to post the disassembly, but I cannot seem to copy it from MPLAB.
     
  16. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Figured it out. The disassembled C program:
    Code ( (Unknown Language)):
    1.  
    2. Line  Address  Opcode               Disassembly              
    3.  
    4.      1   000     2EDE  GOTO 0x6de
    5. [B]Large gap here to next address line[/B]
    6.   1759   6DE     01F2  CLRF 0x72                              
    7.   1760   6DF     0183  CLRF 0x3                              
    8.   1761   6E0     2EE1  GOTO 0x6e1                            
    9.   1762   6E1     1683  BSF 0x3, 0x5                          
    10.   1763   6E2     1303  BCF 0x3, 0x6                          
    11.   1764   6E3     118E  BCF 0xe, 0x3                          
    12.   1765   6E4     3007  MOVLW 0x7                              
    13.   1766   6E5     1283  BCF 0x3, 0x5                          
    14.   1767   6E6     1303  BCF 0x3, 0x6                          
    15.   1768   6E7     009F  MOVWF 0x1f                            
    16.   1769   6E8     3003  MOVLW 0x3                              
    17.   1770   6E9     1683  BSF 0x3, 0x5                          
    18.   1771   6EA     1303  BCF 0x3, 0x6                          
    19.   1772   6EB     0085  MOVWF 0x5                              
    20.   1773   6EC     0186  CLRF 0x6                              
    21.   1774   6ED     2F09  GOTO 0x709                            
    22.   1775   6EE     2F09  GOTO 0x709                            
    23.   1776   6EF     3008  MOVLW 0x8                              
    24.   1777   6F0     1283  BCF 0x3, 0x5                          
    25.   1778   6F1     0086  MOVWF 0x6                              
    26.   1779   6F2     3008  MOVLW 0x8                              
    27.   1780   6F3     00F1  MOVWF 0x71                            
    28.   1781   6F4     30C9  MOVLW 0xc9                            
    29.   1782   6F5     00F0  MOVWF 0x70                            
    30.   1783   6F6     0BF0  DECFSZ 0x70, F                        
    31.   1784   6F7     2EF6  GOTO 0x6f6                            
    32.   1785   6F8     0BF1  DECFSZ 0x71, F                        
    33.   1786   6F9     2EF6  GOTO 0x6f6                            
    34.   1787   6FA     2EFB  GOTO 0x6fb                            
    35.   1788   6FB     3001  MOVLW 0x1                              
    36.   1789   6FC     1283  BCF 0x3, 0x5                          
    37.   1790   6FD     1303  BCF 0x3, 0x6                          
    38.   1791   6FE     0086  MOVWF 0x6                              
    39.   1792   6FF     3008  MOVLW 0x8                              
    40.   1793   700     00F1  MOVWF 0x71                            
    41.   1794   701     30C9  MOVLW 0xc9                            
    42.   1795   702     00F0  MOVWF 0x70                            
    43.   1796   703     0BF0  DECFSZ 0x70, F                        
    44.   1797   704     2F03  GOTO 0x703                            
    45.   1798   705     0BF1  DECFSZ 0x71, F                        
    46.   1799   706     2F03  GOTO 0x703                            
    47.   1800   707     2F08  GOTO 0x708                            
    48.   1801   708     2F09  GOTO 0x709                            
    49.   1802   709     1283  BCF 0x3, 0x5                          
    50.   1803   70A     1303  BCF 0x3, 0x6                          
    51.   1804   70B     1805  BTFSC 0x5, 0                          
    52.   1805   70C     2F0E  GOTO 0x70e                            
    53.   1806   70D     2F0F  GOTO 0x70f                            
    54.   1807   70E     2EEF  GOTO 0x6ef                            
    55.   1808   70F     2F2A  GOTO 0x72a                            
    56.   1809   710     2F2A  GOTO 0x72a                            
    57.   1810   711     3008  MOVLW 0x8                              
    58.   1811   712     0086  MOVWF 0x6                              
    59.   1812   713     3008  MOVLW 0x8                              
    60.   1813   714     00F1  MOVWF 0x71                            
    61.   1814   715     30C9  MOVLW 0xc9                            
    62.   1815   716     00F0  MOVWF 0x70                            
    63.   1816   717     0BF0  DECFSZ 0x70, F                        
    64.   1817   718     2F17  GOTO 0x717                            
    65.   1818   719     0BF1  DECFSZ 0x71, F                        
    66.   1819   71A     2F17  GOTO 0x717                            
    67.   1820   71B     2F1C  GOTO 0x71c                            
    68.   1821   71C     3002  MOVLW 0x2                              
    69.   1822   71D     1283  BCF 0x3, 0x5                          
    70.   1823   71E     1303  BCF 0x3, 0x6                          
    71.   1824   71F     0086  MOVWF 0x6                              
    72.   1825   720     3008  MOVLW 0x8                              
    73.   1826   721     00F1  MOVWF 0x71                            
    74.   1827   722     30C9  MOVLW 0xc9                            
    75.   1828   723     00F0  MOVWF 0x70                            
    76.   1829   724     0BF0  DECFSZ 0x70, F                        
    77.   1830   725     2F24  GOTO 0x724                            
    78.   1831   726     0BF1  DECFSZ 0x71, F                        
    79.   1832   727     2F24  GOTO 0x724                            
    80.   1833   728     2F29  GOTO 0x729                            
    81.   1834   729     2F2A  GOTO 0x72a                            
    82.   1835   72A     1283  BCF 0x3, 0x5                          
    83.   1836   72B     1303  BCF 0x3, 0x6                          
    84.   1837   72C     1885  BTFSC 0x5, 0x1                        
    85.   1838   72D     2F2F  GOTO 0x72f                            
    86.   1839   72E     2F30  GOTO 0x730                            
    87.   1840   72F     2F11  GOTO 0x711                            
    88.   1841   730     1805  BTFSC 0x5, 0                          
    89.   1842   731     2F33  GOTO 0x733                            
    90.   1843   732     2F34  GOTO 0x734                            
    91.   1844   733     2F09  GOTO 0x709                            
    92.   1845   734     1885  BTFSC 0x5, 0x1                        
    93.   1846   735     2F37  GOTO 0x737                            
    94.   1847   736     2F38  GOTO 0x738                            
    95.   1848   737     2F09  GOTO 0x709                            
    96.   1849   738     3009  MOVLW 0x9                              
    97.   1850   739     0086  MOVWF 0x6                              
    98.   1851   73A     01F2  CLRF 0x72                              
    99.   1852   73B     3003  MOVLW 0x3                              
    100.   1853   73C     0272  SUBWF 0x72, W                          
    101.   1854   73D     1C03  BTFSS 0x3, 0                          
    102.   1855   73E     2F40  GOTO 0x740                            
    103.   1856   73F     2F41  GOTO 0x741                            
    104.   1857   740     2F43  GOTO 0x743                            
    105.   1858   741     2F57  GOTO 0x757                            
    106.   1859   742     2F57  GOTO 0x757                            
    107.   1860   743     3010  MOVLW 0x10                            
    108.   1861   744     00F1  MOVWF 0x71                            
    109.   1862   745     3094  MOVLW 0x94                            
    110.   1863   746     00F0  MOVWF 0x70                            
    111.   1864   747     0BF0  DECFSZ 0x70, F                        
    112.   1865   748     2F47  GOTO 0x747                            
    113.   1866   749     0BF1  DECFSZ 0x71, F                        
    114.   1867   74A     2F47  GOTO 0x747                            
    115.   1868   74B     0064  CLRWDT                                
    116.   1869   74C     3001  MOVLW 0x1                              
    117.   1870   74D     00F0  MOVWF 0x70                            
    118.   1871   74E     0870  MOVF 0x70, W                          
    119.   1872   74F     07F2  ADDWF 0x72, F                          
    120.   1873   750     3003  MOVLW 0x3                              
    121.   1874   751     0272  SUBWF 0x72, W                          
    122.   1875   752     1C03  BTFSS 0x3, 0                          
    123.   1876   753     2F55  GOTO 0x755                            
    124.   1877   754     2F56  GOTO 0x756                            
    125.   1878   755     2F43  GOTO 0x743                            
    126.   1879   756     2F57  GOTO 0x757                            
    127.   1880   757     300C  MOVLW 0xc                              
    128.   1881   758     1283  BCF 0x3, 0x5                          
    129.   1882   759     1303  BCF 0x3, 0x6                          
    130.   1883   75A     0086  MOVWF 0x6                              
    131.   1884   75B     01F2  CLRF 0x72                              
    132.   1885   75C     3014  MOVLW 0x14                            
    133.   1886   75D     0272  SUBWF 0x72, W                          
    134.   1887   75E     1C03  BTFSS 0x3, 0                          
    135.   1888   75F     2F61  GOTO 0x761                            
    136.   1889   760     2F62  GOTO 0x762                            
    137.   1890   761     2F64  GOTO 0x764                            
    138.   1891   762     2F78  GOTO 0x778                            
    139.   1892   763     2F78  GOTO 0x778                            
    140.   1893   764     3010  MOVLW 0x10                            
    141.   1894   765     00F1  MOVWF 0x71                            
    142.   1895   766     3094  MOVLW 0x94                            
    143.   1896   767     00F0  MOVWF 0x70                            
    144.   1897   768     0BF0  DECFSZ 0x70, F                        
    145.   1898   769     2F68  GOTO 0x768                            
    146.   1899   76A     0BF1  DECFSZ 0x71, F                        
    147.   1900   76B     2F68  GOTO 0x768                            
    148.  
    Sorry, post was 18,000+ characters, had to break it up into 2 posts:
     
  17. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    No, you used two bytes of data memory in the asm code (count1 and count2). The assembler doesn't know that, though.
     
    ke5nnt likes this.
  18. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Code ( (Unknown Language)):
    1.  
    2. 1901   76C     0064  CLRWDT                                
    3.   1902   76D     3001  MOVLW 0x1                              
    4.   1903   76E     00F0  MOVWF 0x70                            
    5.   1904   76F     0870  MOVF 0x70, W                          
    6.   1905   770     07F2  ADDWF 0x72, F                          
    7.   1906   771     3014  MOVLW 0x14                            
    8.   1907   772     0272  SUBWF 0x72, W                          
    9.   1908   773     1C03  BTFSS 0x3, 0                          
    10.   1909   774     2F76  GOTO 0x776                            
    11.   1910   775     2F77  GOTO 0x777                            
    12.   1911   776     2F64  GOTO 0x764                            
    13.   1912   777     2F78  GOTO 0x778                            
    14.   1913   778     300A  MOVLW 0xa                              
    15.   1914   779     1283  BCF 0x3, 0x5                          
    16.   1915   77A     1303  BCF 0x3, 0x6                          
    17.   1916   77B     0086  MOVWF 0x6                              
    18.   1917   77C     01F2  CLRF 0x72                              
    19.   1918   77D     3004  MOVLW 0x4                              
    20.   1919   77E     0272  SUBWF 0x72, W                          
    21.   1920   77F     1C03  BTFSS 0x3, 0                          
    22.   1921   780     2F82  GOTO 0x782                            
    23.   1922   781     2F83  GOTO 0x783                            
    24.   1923   782     2F85  GOTO 0x785                            
    25.   1924   783     2F99  GOTO 0x799                            
    26.   1925   784     2F99  GOTO 0x799                            
    27.   1926   785     3010  MOVLW 0x10                            
    28.   1927   786     00F1  MOVWF 0x71                            
    29.   1928   787     3094  MOVLW 0x94                            
    30.   1929   788     00F0  MOVWF 0x70                            
    31.   1930   789     0BF0  DECFSZ 0x70, F                        
    32.   1931   78A     2F89  GOTO 0x789                            
    33.   1932   78B     0BF1  DECFSZ 0x71, F                        
    34.   1933   78C     2F89  GOTO 0x789                            
    35.   1934   78D     0064  CLRWDT                                
    36.   1935   78E     3001  MOVLW 0x1                              
    37.   1936   78F     00F0  MOVWF 0x70                            
    38.   1937   790     0870  MOVF 0x70, W                          
    39.   1938   791     07F2  ADDWF 0x72, F                          
    40.   1939   792     3004  MOVLW 0x4                              
    41.   1940   793     0272  SUBWF 0x72, W                          
    42.   1941   794     1C03  BTFSS 0x3, 0                          
    43.   1942   795     2F97  GOTO 0x797                            
    44.   1943   796     2F98  GOTO 0x798                            
    45.   1944   797     2F85  GOTO 0x785                            
    46.   1945   798     2F99  GOTO 0x799                            
    47.   1946   799     3009  MOVLW 0x9                              
    48.   1947   79A     1283  BCF 0x3, 0x5                          
    49.   1948   79B     1303  BCF 0x3, 0x6                          
    50.   1949   79C     0086  MOVWF 0x6                              
    51.   1950   79D     01F2  CLRF 0x72                              
    52.   1951   79E     3003  MOVLW 0x3                              
    53.   1952   79F     0272  SUBWF 0x72, W                          
    54.   1953   7A0     1C03  BTFSS 0x3, 0                          
    55.   1954   7A1     2FA3  GOTO 0x7a3                            
    56.   1955   7A2     2FA4  GOTO 0x7a4                            
    57.   1956   7A3     2FA6  GOTO 0x7a6                            
    58.   1957   7A4     2FBA  GOTO 0x7ba                            
    59.   1958   7A5     2FBA  GOTO 0x7ba                            
    60.   1959   7A6     3010  MOVLW 0x10                            
    61.   1960   7A7     00F1  MOVWF 0x71                            
    62.   1961   7A8     3094  MOVLW 0x94                            
    63.   1962   7A9     00F0  MOVWF 0x70                            
    64.   1963   7AA     0BF0  DECFSZ 0x70, F                        
    65.   1964   7AB     2FAA  GOTO 0x7aa                            
    66.   1965   7AC     0BF1  DECFSZ 0x71, F                        
    67.   1966   7AD     2FAA  GOTO 0x7aa                            
    68.   1967   7AE     0064  CLRWDT                                
    69.   1968   7AF     3001  MOVLW 0x1                              
    70.   1969   7B0     00F0  MOVWF 0x70                            
    71.   1970   7B1     0870  MOVF 0x70, W                          
    72.   1971   7B2     07F2  ADDWF 0x72, F                          
    73.   1972   7B3     3003  MOVLW 0x3                              
    74.   1973   7B4     0272  SUBWF 0x72, W                          
    75.   1974   7B5     1C03  BTFSS 0x3, 0                          
    76.   1975   7B6     2FB8  GOTO 0x7b8                            
    77.   1976   7B7     2FB9  GOTO 0x7b9                            
    78.   1977   7B8     2FA6  GOTO 0x7a6                            
    79.   1978   7B9     2FBA  GOTO 0x7ba                            
    80.   1979   7BA     3021  MOVLW 0x21                            
    81.   1980   7BB     1283  BCF 0x3, 0x5                          
    82.   1981   7BC     1303  BCF 0x3, 0x6                          
    83.   1982   7BD     0086  MOVWF 0x6                              
    84.   1983   7BE     01F2  CLRF 0x72                              
    85.   1984   7BF     300F  MOVLW 0xf                              
    86.   1985   7C0     0272  SUBWF 0x72, W                          
    87.   1986   7C1     1C03  BTFSS 0x3, 0                          
    88.   1987   7C2     2FC4  GOTO 0x7c4                            
    89.   1988   7C3     2FC5  GOTO 0x7c5                            
    90.   1989   7C4     2FC7  GOTO 0x7c7                            
    91.   1990   7C5     2FDB  GOTO 0x7db                            
    92.   1991   7C6     2FDB  GOTO 0x7db                            
    93.   1992   7C7     3010  MOVLW 0x10                            
    94.   1993   7C8     00F1  MOVWF 0x71                            
    95.   1994   7C9     3094  MOVLW 0x94                            
    96.   1995   7CA     00F0  MOVWF 0x70                            
    97.   1996   7CB     0BF0  DECFSZ 0x70, F                        
    98.   1997   7CC     2FCB  GOTO 0x7cb                            
    99.   1998   7CD     0BF1  DECFSZ 0x71, F                        
    100.   1999   7CE     2FCB  GOTO 0x7cb                            
    101.   2000   7CF     0064  CLRWDT                                
    102.   2001   7D0     3001  MOVLW 0x1                              
    103.   2002   7D1     00F0  MOVWF 0x70                            
    104.   2003   7D2     0870  MOVF 0x70, W                          
    105.   2004   7D3     07F2  ADDWF 0x72, F                          
    106.   2005   7D4     300F  MOVLW 0xf                              
    107.   2006   7D5     0272  SUBWF 0x72, W                          
    108.   2007   7D6     1C03  BTFSS 0x3, 0                          
    109.   2008   7D7     2FD9  GOTO 0x7d9                            
    110.   2009   7D8     2FDA  GOTO 0x7da                            
    111.   2010   7D9     2FC7  GOTO 0x7c7                            
    112.   2011   7DA     2FDB  GOTO 0x7db                            
    113.   2012   7DB     3011  MOVLW 0x11                            
    114.   2013   7DC     1283  BCF 0x3, 0x5                          
    115.   2014   7DD     1303  BCF 0x3, 0x6                          
    116.   2015   7DE     0086  MOVWF 0x6                              
    117.   2016   7DF     01F2  CLRF 0x72                              
    118.   2017   7E0     3004  MOVLW 0x4                              
    119.   2018   7E1     0272  SUBWF 0x72, W                          
    120.   2019   7E2     1C03  BTFSS 0x3, 0                          
    121.   2020   7E3     2FE5  GOTO 0x7e5                            
    122.   2021   7E4     2FE6  GOTO 0x7e6                            
    123.   2022   7E5     2FE8  GOTO 0x7e8                            
    124.   2023   7E6     2F09  GOTO 0x709                            
    125.   2024   7E7     2F09  GOTO 0x709                            
    126.   2025   7E8     3010  MOVLW 0x10                            
    127.   2026   7E9     00F1  MOVWF 0x71                            
    128.   2027   7EA     3094  MOVLW 0x94                            
    129.   2028   7EB     00F0  MOVWF 0x70                            
    130.   2029   7EC     0BF0  DECFSZ 0x70, F                        
    131.   2030   7ED     2FEC  GOTO 0x7ec                            
    132.   2031   7EE     0BF1  DECFSZ 0x71, F                        
    133.   2032   7EF     2FEC  GOTO 0x7ec                            
    134.   2033   7F0     0064  CLRWDT                                
    135.   2034   7F1     3001  MOVLW 0x1                              
    136.   2035   7F2     00F0  MOVWF 0x70                            
    137.   2036   7F3     0870  MOVF 0x70, W                          
    138.   2037   7F4     07F2  ADDWF 0x72, F                          
    139.   2038   7F5     3004  MOVLW 0x4                              
    140.   2039   7F6     0272  SUBWF 0x72, W                          
    141.   2040   7F7     1C03  BTFSS 0x3, 0                          
    142.   2041   7F8     2FFA  GOTO 0x7fa                            
    143.   2042   7F9     2FFB  GOTO 0x7fb                            
    144.   2043   7FA     2FE8  GOTO 0x7e8                            
    145.   2044   7FB     2F09  GOTO 0x709                            
    146.   2045   7FC     2F09  GOTO 0x709                            
    147.   2046   7FD     2F09  GOTO 0x709                            
    148.   2047   7FE     2F09  GOTO 0x709                            
    149.   2048   7FF     2800  GOTO 0
    Something I find very interesting, aside from the number of GOTO instructions found in this disassembly, is the consistent clearing of the watchdog timer, even though it is disabled and not used. I imagine bits of code like that being automatically inserted into the program is eating up program space. I'm really not that good at reading disassembled code, but I certainly see things that make me wonder what the compiler is doing, and I recognize some code-bloat.
     
    Last edited: Dec 29, 2011
  19. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    And you have illustrated well one of my major objections to using C for embedded projects.

    When you write C, you are not writing code for the CPU, you are writing instructions to the compiler, and the compiler uses those instructions to generate the asm code. The method of code generation, and correctness of the code, is entirely dependent upon the compiler and the libraries used, and you have to have confidence in a bunch of guys you don't know who wrote the compiler and the libraries. From my experience, *very few* programmers are competent. Why should compiler developers be any different?

    In some thread a couple of months ago, we had a similar discussion. One post indicated that the author had code that worked fine with one compiler, but had bug with another.

    That scares the cr** out of me! When my stuff fails in the field, for whatever reason, there are not going to be a bunch of compiler developers that are going to stand in and take the heat for me!
     
    Eric007 likes this.
  20. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    A very excellent and valid point Joey. I assume you prefer to use assembly for all of your work?
     
Loading...