Written Program Size: A Comparison of C vs. Assembly

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
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.
 

MrChips

Joined Oct 2, 2009
21,880
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.)
 

AlexR

Joined Jan 16, 2008
732
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.
 

t06afre

Joined May 11, 2009
5,934
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.
 

Papabravo

Joined Feb 24, 2006
14,435
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.
 

kubeek

Joined Sep 20, 2005
5,733
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?
 

joeyd999

Joined Jun 6, 2011
4,430
Is assembly language coding fun, challenging, interesting and deeply satisfying to produce?

Hex yeah it is!

That should be enough reason to do it.
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!
 

joeyd999

Joined Jun 6, 2011
4,430
What is wrong with that statement?
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.
 

joeyd999

Joined Jun 6, 2011
4,430
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.
 

t06afre

Joined May 11, 2009
5,934
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?
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.
 

AlexR

Joined Jan 16, 2008
732
Most commercial AVR compilers do have limited code size in free mode. .......................
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.
 

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
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.
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
Rich (BB code):
;************************************************************************
;TRAFFIC CONTROL SIGNAL                                                    *
;THIS SIGNAL IS INTENDED TO CONTROL TRAFFIC AT A 4-WAY INTERSECTION        *
;WITH THE "MAIN" ROAD HAVING A GREEN ARROW AND INDEPENDENT YELLOW        *
;CONTROL FOR STOPPING TURN TRAFFIC.  SECONDARY ROAD IS STANARD R-Y-G    *
;LIGHTS.  THIS PROGRAM ALSO HAS 2 "NIGHT MODE" OPERATIONS WHERE            *
;MAIN LANES FLASH YELLOW WHILE SECONDARY ROAD FLASHES RED, OR            *
;4-WAY FLASHING RED DEPENDING ON POSITION OF A PERIPHERAL SWITCH        *
;                                                                        *
;CREATED BY RYAN GOFF FOR THE PIC16F628A                                *
;************************************************************************
;CONFIGURATION
    LIST        P=PIC16F628A
    #INCLUDE    P16F628A.INC
    __CONFIG B'10000100000000'
    ERRORLEVEL    -302
;
    ORG        01
    GOTO    INIT
;
    ORG        04
    GOTO    INIT
;
    COUNT1    EQU    22
    COUNT2    EQU    23
;*************************************************************************
;SUBROUTINES
T500MS    MOVLW    0x32
        MOVWF    COUNT1
        MOVLW    0x04
        MOVWF    COUNT2
T500MS1    DECFSZ    COUNT1        ;500MS DELAY FOR NIGHT MODE FLASHING
        GOTO    $+2
        DECFSZ    COUNT2
        GOTO    T500MS1
        RETURN
;
T3SEC    MOVLW    0xBF
        MOVWF    COUNT1
        MOVLW    0x13
        MOVWF    COUNT2
T3SEC1    DECFSZ    COUNT1        ;3 SECOND DELAY FOR 4-WAY RED
        GOTO    $+2
        DECFSZ    COUNT2
        GOTO    T3SEC1
        RETURN
;
T4SEC    MOVLW    0xFF
        MOVWF    COUNT1
        MOVLW    0x19
        MOVWF    COUNT2
T4SEC1    DECFSZ    COUNT1        ;4 SECOND DELAY FOR YELLOW
        GOTO    $+2
        DECFSZ    COUNT2
        GOTO    T4SEC1
        RETURN
;
T10SEC    MOVLW    0xFF
        MOVWF    COUNT1
        MOVLW    0x40
        MOVWF    COUNT2
T10SEC1    DECFSZ    COUNT1        ;10 SECOND DELAY FOR GREEN TURN ARROW
        GOTO    $+2
        DECFSZ    COUNT2
        GOTO    T10SEC1
        RETURN
;
T20SEC    MOVLW    0xFF
        MOVWF    COUNT1
        MOVLW    0x80
        MOVWF    COUNT2
T20SEC1    DECFSZ    COUNT1        ;20 SECOND DELAY FOR GREEN LIGHT
        GOTO    $+2
        DECFSZ    COUNT2
        GOTO    T20SEC1
        RETURN
;************************************************************************
;PROGRAM INITIALIZATION
INIT    BSF        STATUS,5    ;BANK 1
        MOVLW    B'00000000'
        MOVWF    TRISB        ;ALL PORT B ARE OUTPUT
        MOVLW    B'00000011'
        MOVWF    TRISA        ;RA0 AND RA1 INPUT, OTHERS OUTPUT
        BCF        STATUS,5    ;BANK 0
        MOVLW    B'00000111'
        MOVWF    CMCON        ;COMPARATORS OFF
        BTFSC    PORTA,0        ;NIGHT MODE DESIRED?
        GOTO    NMRY        ;FLASH RED/YELLOW
        BTFSC    PORTA,1        ;OTHER NIGHT MODE DESIRED?
        GOTO    NMRR        ;FLASH RED/RED
;*************************************************************************
;NORMAL SIGNAL OPERATION
NORM    MOVLW    B'00001001'
        MOVWF    PORTB        ;ALL TRAFFIC LANES RED
        CALL    T3SEC        ;FOR 3 SECONDS
        MOVLW    B'01001001'
        MOVWF    PORTB        ;MAIN LANE TURN ARROW GREEN, ALL OTHERS RED
        CALL    T10SEC        ;FOR 10 SECONDS
        MOVLW    B'00011001'
        MOVWF    PORTB        ;MAIN LANE YELLOW, ALL OTHERS RED
        CALL    T4SEC        ;FOR 4 SECONDS
        MOVLW    B'00001001'
        MOVWF    PORTB        ;ALL TRAFFIC LANES RED
        CALL    T3SEC        ;FOR 3 SECONDS
        BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
        GOTO    NMRY        ;GO TO R/Y NIGHT MODE
        BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
        GOTO    NMRR        ;GO TO R/R NIGHT MODE
        MOVLW    B'10000001'
        MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, TURN ARROWS OFF, SECONDARY RED
        CALL    T20SEC        ;FOR 20 SECONDS
        MOVLW    B'00010001'
        MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW + SECONDARY RED
        CALL    T4SEC        ;FOR 4 SECONDS
        MOVLW    B'00001001'
        MOVWF    PORTB        ;ALL TRAFFIC LANES RED
        CALL    T3SEC        ;FOR 3 SECONDS
        BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
        GOTO    NMRY        ;GO TO R/Y NIGHT MODE
        BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
        GOTO    NMRR        ;GO TO R/R NIGHT MODE
        MOVLW    B'00001100'
        MOVWF    PORTB        ;SECONDARY GREEN, MAIN LANES RED
        CALL    T20SEC        ;FOR 20 SECONDS
        MOVLW    B'00001010'
        MOVWF    PORTB        ;SECONDARY YELLOW, MAIN LANES RED
        CALL    T4SEC        ;FOR 4 SECONDS
        BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
        GOTO    NMRY        ;GO TO R/Y NIGHT MODE
        BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
        GOTO    NMRR        ;GO TO R/R NIGHT MODE
        GOTO    NORM        ;OR REMAIN IN NORMAL CYCLE OPERATION
;**************************************************************************
;R/Y NIGHT MODE OPERATION.  PORTA 0
NMRY    MOVLW    B'00010000'
        MOVWF    PORTB        ;MAIN LANES ALL YELLOW, ALL OTHERS OFF
        CALL    T500MS        ;FOR 1/2 SECOND
        MOVLW    B'00000001'
        MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
        CALL    T500MS        ;FOR 1/2 SECOND
        BTFSS    PORTA,0        ;STILL WANT R/Y NIGHT MODE?
        GOTO    TRANS        ;IF NO, TRANSITION TO NORMAL CYCLE MODE
        BTFSC    PORTA,1        ;SWITCH TO R/R NIGHT MODE?
        GOTO    NMRR        ;THEN GO TO R/R NIGHT MODE
        GOTO    NMRY        ;OR STAY IN THIS NIGHT MODE
;**************************************************************************
;R/R NIGHT MODE OPERATION.  PORTA 1
NMRR    MOVLW    B'00001000'
        MOVWF    PORTB        ;MAIN LANES RED, ALL OTHERS OFF
        CALL    T500MS        ;FOR 1/2 SECOND
        MOVLW    B'00000001'
        MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
        CALL    T500MS        ;FOR 1/2 SECOND
        BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
        GOTO    NMRY        ;THEN GO TO R/Y NIGHT MODE
        BTFSS    PORTA,1        ;R/R NIGHT MODE STILL DESIRED?
        GOTO    TRANS        ;NO, TRANSITION TO NORMAL CYCLE MODE
        GOTO    NMRR        ;YES, STAY IN R/R NIGHT MODE
;**************************************************************************
;TRANSITION FROM NIGHT MODE TO NORMAL
TRANS    MOVLW    B'10000001'
        MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, SECONDARY RED
        CALL    T10SEC        ;FOR 10 SECONDS
        MOVLW    B'00010001'
        MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW. SECONDARY RED
        CALL    T4SEC        ;FOR 4 SECONDS
        MOVLW    B'00001001'
        MOVWF    PORTB        ;ALL TRAFFIC LANES RED
        CALL    T3SEC        ;FOR 3 SECONDS
        MOVLW    B'00001100'
        MOVWF    PORTB        ;SECONDARY GREEN, MAIN LANES RED
        CALL    T20SEC        ;FOR 20 SECONDS
        MOVLW    B'00001010'
        MOVWF    PORTB        ;SECONDARY YELLOW, MAIN LANES RED
        CALL    T4SEC        ;FOR 4 SECONDS
        GOTO    NORM        ;GO TO NORMAL CYCLE OPERATION
;**************************************************************************
        END
 

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
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.
Rich (BB code):
//
//    6-LIGHT, 4-WAY INTERSECTION TRAFFIC CONTROL SIGNAL
//    RYAN GOFF
//    JAN-14-2011
//    PIC16F628A
//    HIGH-TECH C COMPILER, MPLAB IDE
//
//    THIS PROGRAM IS FOR A 6-LIGHT INTERSECTION TRAFFIC CONTROL
//    SIGNAL WHERE BOTH THE MAIN TRAFFIC LANES (MAIN) AND SECONDARY
//    TRAFFIC LANES (SDRY) HAVE A SINGLE SET OF RED, YELLOW, GREEN
//    LIGHTS WHICH WILL CYCLE TO SAFELY CONTROL TRAFFIC WHILE
//    OPERATED IN THE NORMAL CYCLE.  THE SIGNAL ALSO HAS 2 NIGHT-
//    MODE CYCLES WHERE MAIN TRAFFIC LANES RECEIVE A FLASHING YELLOW
//    SIGNAL WHILE SECONDARY TRAFFIC LANES RECEIVE A FLASHING RED
//    SIGNAL, OR ALL TRAFFIC LANES RECEIVE A FLASHING RED SIGNAL.
//    SELECTION OF EITHER OF THESE NIGHT-MODE CYCLES IS DEPENDENT ON
//    THE POSITION OF A SINGLE ON-OFF-ON SWITCH WHICH WILL INPUT
//    HIGH TO RA0 OR RA1.  WHEN BOTH INPUTS ARE LOW, NORMAL CYCLE
//    OPERATION RESULTS.  THE SIGNAL IS PROGRAMMED TO NEVER ALLOW AN
//    UNSAFE SIGNAL CONDITION, THEREFORE, TRANSITION FROM NORMAL
//    OPERATION TO A NIGHT-MODE CYCLE OR VISE-VERSA IS NOT
//    INSTANTANEOUS, AND WILL OCCUR ONLY AT THE PROGRAMMED SAFE
//    POINT.
//
/****************************************************************/

// CONFIGURATION

#include <htc.h>
#define _XTAL_FREQ 48000
__CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_OFF & LVP_OFF & CP_OFF & CPD_OFF);

//    INTERNAL 48KHz CLOCK, PWRT, NO PROTECTION

/***************************************************************/

// GLOBAL VARIABLES

unsigned char count;

/***************************************************************/

void main()
{
 OSCF=0;                    //LOW POWER 48KHZ INTOSC
 CMCON=0x07;                //COMPARATORS OFF
 TRISA=0x03;                //RA0 AND RA1 INPUT, OTHERS OUTPUT
 TRISB=0;                    //ALL PORT B ARE OUTPUTS

 while(1)
 {
    while (RA0==1)            //WHILE INPUT AT RA0, NMRR
    {
     PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
     __delay_ms(500);
     PORTB=0x01;            //RB0 ON, MAIN RED, ALL OTHERS OFF
     __delay_ms(500);
    }
    while (RA1==1)            //WHILE INPUT AT RA1, NMRY
    {
     PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
     __delay_ms(500);
     PORTB=0x02;            //RB1 ON, MAIN YELLOW, ALL OTHERS OFF
     __delay_ms(500);
    }
    if ((RA0!=1)&&(RA1!=1))    //RA0 AND RA1 NO INPUT
    {
     PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
        for (count=0; count<3; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
        }
     PORTB=0x0C;            //RB2 ON, RB3 ON, MAIN GREEN, SDRY RED
        for (count=0; count<20; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 20 TIMES, 20-SECOND DELAY
        }
     PORTB=0x0A;            //RB1 ON, RB3 ON, MAIN YELLOW, SDRY RED
        for (count=0; count<4; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
        }
     PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
        for (count=0; count<3; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
        }
     PORTB=0x21;            //RB0 ON, RB5 ON, MAIN RED, SDRY GREEN
        for (count=0; count<15; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 15 TIMES, 15-SECOND DELAY
        }
     PORTB=0x11;            //RB0 ON, RB4 ON, MAIN RED, SDRY YELLOW
        for (count=0; count<4; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
        }
    }
 }
}
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.
 

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
Figured it out. The disassembled C program:
Rich (BB code):
Line  Address  Opcode               Disassembly              

     1   000     2EDE  GOTO 0x6de 
Large gap here to next address line
  1759   6DE     01F2  CLRF 0x72                              
  1760   6DF     0183  CLRF 0x3                               
  1761   6E0     2EE1  GOTO 0x6e1                             
  1762   6E1     1683  BSF 0x3, 0x5                           
  1763   6E2     1303  BCF 0x3, 0x6                           
  1764   6E3     118E  BCF 0xe, 0x3                           
  1765   6E4     3007  MOVLW 0x7                              
  1766   6E5     1283  BCF 0x3, 0x5                           
  1767   6E6     1303  BCF 0x3, 0x6                           
  1768   6E7     009F  MOVWF 0x1f                             
  1769   6E8     3003  MOVLW 0x3                              
  1770   6E9     1683  BSF 0x3, 0x5                           
  1771   6EA     1303  BCF 0x3, 0x6                           
  1772   6EB     0085  MOVWF 0x5                              
  1773   6EC     0186  CLRF 0x6                               
  1774   6ED     2F09  GOTO 0x709                             
  1775   6EE     2F09  GOTO 0x709                             
  1776   6EF     3008  MOVLW 0x8                              
  1777   6F0     1283  BCF 0x3, 0x5                           
  1778   6F1     0086  MOVWF 0x6                              
  1779   6F2     3008  MOVLW 0x8                              
  1780   6F3     00F1  MOVWF 0x71                             
  1781   6F4     30C9  MOVLW 0xc9                             
  1782   6F5     00F0  MOVWF 0x70                             
  1783   6F6     0BF0  DECFSZ 0x70, F                         
  1784   6F7     2EF6  GOTO 0x6f6                             
  1785   6F8     0BF1  DECFSZ 0x71, F                         
  1786   6F9     2EF6  GOTO 0x6f6                             
  1787   6FA     2EFB  GOTO 0x6fb                             
  1788   6FB     3001  MOVLW 0x1                              
  1789   6FC     1283  BCF 0x3, 0x5                           
  1790   6FD     1303  BCF 0x3, 0x6                           
  1791   6FE     0086  MOVWF 0x6                              
  1792   6FF     3008  MOVLW 0x8                              
  1793   700     00F1  MOVWF 0x71                             
  1794   701     30C9  MOVLW 0xc9                             
  1795   702     00F0  MOVWF 0x70                             
  1796   703     0BF0  DECFSZ 0x70, F                         
  1797   704     2F03  GOTO 0x703                             
  1798   705     0BF1  DECFSZ 0x71, F                         
  1799   706     2F03  GOTO 0x703                             
  1800   707     2F08  GOTO 0x708                             
  1801   708     2F09  GOTO 0x709                             
  1802   709     1283  BCF 0x3, 0x5                           
  1803   70A     1303  BCF 0x3, 0x6                           
  1804   70B     1805  BTFSC 0x5, 0                           
  1805   70C     2F0E  GOTO 0x70e                             
  1806   70D     2F0F  GOTO 0x70f                             
  1807   70E     2EEF  GOTO 0x6ef                             
  1808   70F     2F2A  GOTO 0x72a                             
  1809   710     2F2A  GOTO 0x72a                             
  1810   711     3008  MOVLW 0x8                              
  1811   712     0086  MOVWF 0x6                              
  1812   713     3008  MOVLW 0x8                              
  1813   714     00F1  MOVWF 0x71                             
  1814   715     30C9  MOVLW 0xc9                             
  1815   716     00F0  MOVWF 0x70                             
  1816   717     0BF0  DECFSZ 0x70, F                         
  1817   718     2F17  GOTO 0x717                             
  1818   719     0BF1  DECFSZ 0x71, F                         
  1819   71A     2F17  GOTO 0x717                             
  1820   71B     2F1C  GOTO 0x71c                             
  1821   71C     3002  MOVLW 0x2                              
  1822   71D     1283  BCF 0x3, 0x5                           
  1823   71E     1303  BCF 0x3, 0x6                           
  1824   71F     0086  MOVWF 0x6                              
  1825   720     3008  MOVLW 0x8                              
  1826   721     00F1  MOVWF 0x71                             
  1827   722     30C9  MOVLW 0xc9                             
  1828   723     00F0  MOVWF 0x70                             
  1829   724     0BF0  DECFSZ 0x70, F                         
  1830   725     2F24  GOTO 0x724                             
  1831   726     0BF1  DECFSZ 0x71, F                         
  1832   727     2F24  GOTO 0x724                             
  1833   728     2F29  GOTO 0x729                             
  1834   729     2F2A  GOTO 0x72a                             
  1835   72A     1283  BCF 0x3, 0x5                           
  1836   72B     1303  BCF 0x3, 0x6                           
  1837   72C     1885  BTFSC 0x5, 0x1                         
  1838   72D     2F2F  GOTO 0x72f                             
  1839   72E     2F30  GOTO 0x730                             
  1840   72F     2F11  GOTO 0x711                             
  1841   730     1805  BTFSC 0x5, 0                           
  1842   731     2F33  GOTO 0x733                             
  1843   732     2F34  GOTO 0x734                             
  1844   733     2F09  GOTO 0x709                             
  1845   734     1885  BTFSC 0x5, 0x1                         
  1846   735     2F37  GOTO 0x737                             
  1847   736     2F38  GOTO 0x738                             
  1848   737     2F09  GOTO 0x709                             
  1849   738     3009  MOVLW 0x9                              
  1850   739     0086  MOVWF 0x6                              
  1851   73A     01F2  CLRF 0x72                              
  1852   73B     3003  MOVLW 0x3                              
  1853   73C     0272  SUBWF 0x72, W                          
  1854   73D     1C03  BTFSS 0x3, 0                           
  1855   73E     2F40  GOTO 0x740                             
  1856   73F     2F41  GOTO 0x741                             
  1857   740     2F43  GOTO 0x743                             
  1858   741     2F57  GOTO 0x757                             
  1859   742     2F57  GOTO 0x757                             
  1860   743     3010  MOVLW 0x10                             
  1861   744     00F1  MOVWF 0x71                             
  1862   745     3094  MOVLW 0x94                             
  1863   746     00F0  MOVWF 0x70                             
  1864   747     0BF0  DECFSZ 0x70, F                         
  1865   748     2F47  GOTO 0x747                             
  1866   749     0BF1  DECFSZ 0x71, F                         
  1867   74A     2F47  GOTO 0x747                             
  1868   74B     0064  CLRWDT                                 
  1869   74C     3001  MOVLW 0x1                              
  1870   74D     00F0  MOVWF 0x70                             
  1871   74E     0870  MOVF 0x70, W                           
  1872   74F     07F2  ADDWF 0x72, F                          
  1873   750     3003  MOVLW 0x3                              
  1874   751     0272  SUBWF 0x72, W                          
  1875   752     1C03  BTFSS 0x3, 0                           
  1876   753     2F55  GOTO 0x755                             
  1877   754     2F56  GOTO 0x756                             
  1878   755     2F43  GOTO 0x743                             
  1879   756     2F57  GOTO 0x757                             
  1880   757     300C  MOVLW 0xc                              
  1881   758     1283  BCF 0x3, 0x5                           
  1882   759     1303  BCF 0x3, 0x6                           
  1883   75A     0086  MOVWF 0x6                              
  1884   75B     01F2  CLRF 0x72                              
  1885   75C     3014  MOVLW 0x14                             
  1886   75D     0272  SUBWF 0x72, W                          
  1887   75E     1C03  BTFSS 0x3, 0                           
  1888   75F     2F61  GOTO 0x761                             
  1889   760     2F62  GOTO 0x762                             
  1890   761     2F64  GOTO 0x764                             
  1891   762     2F78  GOTO 0x778                             
  1892   763     2F78  GOTO 0x778                             
  1893   764     3010  MOVLW 0x10                             
  1894   765     00F1  MOVWF 0x71                             
  1895   766     3094  MOVLW 0x94                             
  1896   767     00F0  MOVWF 0x70                             
  1897   768     0BF0  DECFSZ 0x70, F                         
  1898   769     2F68  GOTO 0x768                             
  1899   76A     0BF1  DECFSZ 0x71, F                         
  1900   76B     2F68  GOTO 0x768
Sorry, post was 18,000+ characters, had to break it up into 2 posts:
 

joeyd999

Joined Jun 6, 2011
4,430
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.
No, you used two bytes of data memory in the asm code (count1 and count2). The assembler doesn't know that, though.
 

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
Rich (BB code):
1901   76C     0064  CLRWDT                                 
  1902   76D     3001  MOVLW 0x1                              
  1903   76E     00F0  MOVWF 0x70                             
  1904   76F     0870  MOVF 0x70, W                           
  1905   770     07F2  ADDWF 0x72, F                          
  1906   771     3014  MOVLW 0x14                             
  1907   772     0272  SUBWF 0x72, W                          
  1908   773     1C03  BTFSS 0x3, 0                           
  1909   774     2F76  GOTO 0x776                             
  1910   775     2F77  GOTO 0x777                             
  1911   776     2F64  GOTO 0x764                             
  1912   777     2F78  GOTO 0x778                             
  1913   778     300A  MOVLW 0xa                              
  1914   779     1283  BCF 0x3, 0x5                           
  1915   77A     1303  BCF 0x3, 0x6                           
  1916   77B     0086  MOVWF 0x6                              
  1917   77C     01F2  CLRF 0x72                              
  1918   77D     3004  MOVLW 0x4                              
  1919   77E     0272  SUBWF 0x72, W                          
  1920   77F     1C03  BTFSS 0x3, 0                           
  1921   780     2F82  GOTO 0x782                             
  1922   781     2F83  GOTO 0x783                             
  1923   782     2F85  GOTO 0x785                             
  1924   783     2F99  GOTO 0x799                             
  1925   784     2F99  GOTO 0x799                             
  1926   785     3010  MOVLW 0x10                             
  1927   786     00F1  MOVWF 0x71                             
  1928   787     3094  MOVLW 0x94                             
  1929   788     00F0  MOVWF 0x70                             
  1930   789     0BF0  DECFSZ 0x70, F                         
  1931   78A     2F89  GOTO 0x789                             
  1932   78B     0BF1  DECFSZ 0x71, F                         
  1933   78C     2F89  GOTO 0x789                             
  1934   78D     0064  CLRWDT                                 
  1935   78E     3001  MOVLW 0x1                              
  1936   78F     00F0  MOVWF 0x70                             
  1937   790     0870  MOVF 0x70, W                           
  1938   791     07F2  ADDWF 0x72, F                          
  1939   792     3004  MOVLW 0x4                              
  1940   793     0272  SUBWF 0x72, W                          
  1941   794     1C03  BTFSS 0x3, 0                           
  1942   795     2F97  GOTO 0x797                             
  1943   796     2F98  GOTO 0x798                             
  1944   797     2F85  GOTO 0x785                             
  1945   798     2F99  GOTO 0x799                             
  1946   799     3009  MOVLW 0x9                              
  1947   79A     1283  BCF 0x3, 0x5                           
  1948   79B     1303  BCF 0x3, 0x6                           
  1949   79C     0086  MOVWF 0x6                              
  1950   79D     01F2  CLRF 0x72                              
  1951   79E     3003  MOVLW 0x3                              
  1952   79F     0272  SUBWF 0x72, W                          
  1953   7A0     1C03  BTFSS 0x3, 0                           
  1954   7A1     2FA3  GOTO 0x7a3                             
  1955   7A2     2FA4  GOTO 0x7a4                             
  1956   7A3     2FA6  GOTO 0x7a6                             
  1957   7A4     2FBA  GOTO 0x7ba                             
  1958   7A5     2FBA  GOTO 0x7ba                             
  1959   7A6     3010  MOVLW 0x10                             
  1960   7A7     00F1  MOVWF 0x71                             
  1961   7A8     3094  MOVLW 0x94                             
  1962   7A9     00F0  MOVWF 0x70                             
  1963   7AA     0BF0  DECFSZ 0x70, F                         
  1964   7AB     2FAA  GOTO 0x7aa                             
  1965   7AC     0BF1  DECFSZ 0x71, F                         
  1966   7AD     2FAA  GOTO 0x7aa                             
  1967   7AE     0064  CLRWDT                                 
  1968   7AF     3001  MOVLW 0x1                              
  1969   7B0     00F0  MOVWF 0x70                             
  1970   7B1     0870  MOVF 0x70, W                           
  1971   7B2     07F2  ADDWF 0x72, F                          
  1972   7B3     3003  MOVLW 0x3                              
  1973   7B4     0272  SUBWF 0x72, W                          
  1974   7B5     1C03  BTFSS 0x3, 0                           
  1975   7B6     2FB8  GOTO 0x7b8                             
  1976   7B7     2FB9  GOTO 0x7b9                             
  1977   7B8     2FA6  GOTO 0x7a6                             
  1978   7B9     2FBA  GOTO 0x7ba                             
  1979   7BA     3021  MOVLW 0x21                             
  1980   7BB     1283  BCF 0x3, 0x5                           
  1981   7BC     1303  BCF 0x3, 0x6                           
  1982   7BD     0086  MOVWF 0x6                              
  1983   7BE     01F2  CLRF 0x72                              
  1984   7BF     300F  MOVLW 0xf                              
  1985   7C0     0272  SUBWF 0x72, W                          
  1986   7C1     1C03  BTFSS 0x3, 0                           
  1987   7C2     2FC4  GOTO 0x7c4                             
  1988   7C3     2FC5  GOTO 0x7c5                             
  1989   7C4     2FC7  GOTO 0x7c7                             
  1990   7C5     2FDB  GOTO 0x7db                             
  1991   7C6     2FDB  GOTO 0x7db                             
  1992   7C7     3010  MOVLW 0x10                             
  1993   7C8     00F1  MOVWF 0x71                             
  1994   7C9     3094  MOVLW 0x94                             
  1995   7CA     00F0  MOVWF 0x70                             
  1996   7CB     0BF0  DECFSZ 0x70, F                         
  1997   7CC     2FCB  GOTO 0x7cb                             
  1998   7CD     0BF1  DECFSZ 0x71, F                         
  1999   7CE     2FCB  GOTO 0x7cb                             
  2000   7CF     0064  CLRWDT                                 
  2001   7D0     3001  MOVLW 0x1                              
  2002   7D1     00F0  MOVWF 0x70                             
  2003   7D2     0870  MOVF 0x70, W                           
  2004   7D3     07F2  ADDWF 0x72, F                          
  2005   7D4     300F  MOVLW 0xf                              
  2006   7D5     0272  SUBWF 0x72, W                          
  2007   7D6     1C03  BTFSS 0x3, 0                           
  2008   7D7     2FD9  GOTO 0x7d9                             
  2009   7D8     2FDA  GOTO 0x7da                             
  2010   7D9     2FC7  GOTO 0x7c7                             
  2011   7DA     2FDB  GOTO 0x7db                             
  2012   7DB     3011  MOVLW 0x11                             
  2013   7DC     1283  BCF 0x3, 0x5                           
  2014   7DD     1303  BCF 0x3, 0x6                           
  2015   7DE     0086  MOVWF 0x6                              
  2016   7DF     01F2  CLRF 0x72                              
  2017   7E0     3004  MOVLW 0x4                              
  2018   7E1     0272  SUBWF 0x72, W                          
  2019   7E2     1C03  BTFSS 0x3, 0                           
  2020   7E3     2FE5  GOTO 0x7e5                             
  2021   7E4     2FE6  GOTO 0x7e6                             
  2022   7E5     2FE8  GOTO 0x7e8                             
  2023   7E6     2F09  GOTO 0x709                             
  2024   7E7     2F09  GOTO 0x709                             
  2025   7E8     3010  MOVLW 0x10                             
  2026   7E9     00F1  MOVWF 0x71                             
  2027   7EA     3094  MOVLW 0x94                             
  2028   7EB     00F0  MOVWF 0x70                             
  2029   7EC     0BF0  DECFSZ 0x70, F                         
  2030   7ED     2FEC  GOTO 0x7ec                             
  2031   7EE     0BF1  DECFSZ 0x71, F                         
  2032   7EF     2FEC  GOTO 0x7ec                             
  2033   7F0     0064  CLRWDT                                 
  2034   7F1     3001  MOVLW 0x1                              
  2035   7F2     00F0  MOVWF 0x70                             
  2036   7F3     0870  MOVF 0x70, W                           
  2037   7F4     07F2  ADDWF 0x72, F                          
  2038   7F5     3004  MOVLW 0x4                              
  2039   7F6     0272  SUBWF 0x72, W                          
  2040   7F7     1C03  BTFSS 0x3, 0                           
  2041   7F8     2FFA  GOTO 0x7fa                             
  2042   7F9     2FFB  GOTO 0x7fb                             
  2043   7FA     2FE8  GOTO 0x7e8                             
  2044   7FB     2F09  GOTO 0x709                             
  2045   7FC     2F09  GOTO 0x709                             
  2046   7FD     2F09  GOTO 0x709                             
  2047   7FE     2F09  GOTO 0x709                             
  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:

joeyd999

Joined Jun 6, 2011
4,430
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 my wonder what the compiler is doing, and I recognize some code-bloat.
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!
 
Top