number of lines of code!

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi All!

I was just wondering...if for a certain project, it takes ~6500 lines of code in C to implement it. Then how many lines of code do you think it would require to implement it in PIC18 assembly?

Of course you can't tell it precisely...but roughly just to have an idea!

Regards,

Eric007
 

hexreader

Joined Apr 16, 2011
567
A line of C code could be as simple as "PORTA=0;" or as complex as you like.

My wild guess would be about 5 assembly instructions per line for simply written code on a good compiler, to 20 instructions per line for average code - maybe.

There is no reason why a line of code might not create 1000 assembly instructions.

This seems like a "how long is a piece of string" question to me. Depends on the application, the coder, the compiler, whether an OS is involved and more.
 

ErnieM

Joined Apr 24, 2011
8,346
More. Lots more. Not that I'm really going to count: my latest project spans over 16 .c files and 6 more .h files, and that's not counting the library functions that just get referenced (and not written).

You can wander thru the disassembly listing which shows both the originating C code and the assembly statements it generated.

My disassembly is 20,785 lines long to generate 20,128 assembly words. (Note there is no correspondence between these numbers and they are only close by coincidence.) It can be interesting to wander thru this file to see how much code a seemingly simple statement generates.
 

tallgirl

Joined Jun 17, 2012
7
It's very heavily dependent on the programmer, their skill, programming style, the processor being used, etc.

The answer isn't just "there is no answer", but often "the answer depends on how well they understand the language, and how well they are trying to fit their high level code to the low level processor."

It can also heavily depend on the compiler that's being used. Different compilers have different abilities to optimize code after a simplistic conversion from C to assembly / machine language has been performed. Some compilers are able to optimize for either space or performance -- "space" optimization will produce fewer instructions, but take longer, "performance" optimizations (such as loop unrolling) will produce more instructions, but take less time.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Thanks for all your comments!

@tallgirl: welcome to the forum! And I was assuming that the programmer has has very good programming skill, style,... in both C and assembly!

Again thanks all y'all
 

MrChips

Joined Oct 2, 2009
27,722
If you were to use a single statement such as

sprintf("Hello World");

or

x = y + 2.34;

this would invoke about 2000 lines of asm code from a library function.
 

MrChips

Joined Oct 2, 2009
27,722
oops. Sorry, I misread the question.

If a project took 6000 lines of C code and an experienced programmer had to rewrite the entire project in asm he/she would go insane.

My wild estimate would be x10 to x100 in asm which is a HUGE project.

(I am writing my own windows OS for STM M4. I will let you know later how may lines of C/asm I'm up to now.)
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
oops. Sorry, I misread the question.

If a project took 6000 lines of C code and an experienced programmer had to rewrite the entire project in asm he/she would go insane.
Hahahah...lol

My wild estimate would be x10 to x100 in asm which is a HUGE project.
Yes it is Sir...

(I am writing my own windows OS for STM M4. I will let you know later how may lines of C/asm I'm up to now.)
Oh wow...gudluck!
Can't wait to hear...

Thanks!
 

tallgirl

Joined Jun 17, 2012
7
Thanks for all your comments!

@tallgirl: welcome to the forum! And I was assuming that the programmer has has very good programming skill, style,... in both C and assembly!

Again thanks all y'all
The question doesn't have an answer.

I've written C code where the code in question was very close to 1 to 1, perhaps as low as 1 LOC of C to 2 or 3 assembly instructions. Much of that sort of code is for device drivers where very specific actions need to happen very precisely -- setting a bit in a control register, transferring a word to or from a device register. I've also written C where 1 average line produced 20 or 30 machine instructions.

Although much of my more complex code was at a higher level than inside an operating system, today's microcontrollers have so much performance that features which were once limited to "real" computers are now possible on the higher performance versions of embedded chips.

And to the person who commented that a 6,000 line project in C would drive someone "crazy" if they were to write in in assembly, 6,000 lines of C is nothing. My first large scale assembler project was about 50KLOC. That's small compared to what I've done since, where KLOC counts get into the hundreds and thousands (millions of lines of code).
 

tallgirl

Joined Jun 17, 2012
7
oops. Sorry, I misread the question.

If a project took 6000 lines of C code and an experienced programmer had to rewrite the entire project in asm he/she would go insane.

My wild estimate would be x10 to x100 in asm which is a HUGE project
Very unlikely, unless each C statement was a giant mess.

For much of my ATmega-based work, based on each instruction being 2 bytes, my code seems to be something on the order of between 7 to 10 to 1 (1 line of C equals 7 to 10 machine instructions).

That's code that hasn't been optimized for space yet and I could probably reduce that if I wanted to spend the time. For my largest, best-written, project the expansion is about 6 to 1. I may have to go back and revisit that code because I want to add features and I'm out of program memory.

When I was still hand-coding assembly, I was able to do a lot more in a lot less program memory space because I could hand-pick the exact instructions and/or addressing modes I needed, without hoping the compiler made the correct decision. Which it seldom does.
 

MrChips

Joined Oct 2, 2009
27,722
Well congratulations to you tallgirl... 1MLOC!

My STM32F407 has 1MB flash memory which I assume would translate to 250KLOC.
If I get anywhere near to filling all of that I would be nuts!
 

tallgirl

Joined Jun 17, 2012
7
Well congratulations to you tallgirl... 1MLOC!

My STM32F407 has 1MB flash memory which I assume would translate to 250KLOC.
If I get anywhere near to filling all of that I would be nuts!
It's all about time. I produce about 25KLOC per year myself, in a good year. I used to have a staff of over 10, which is sufficient in itself to drive one crazy, but can make for very huge projects in very small amounts of time.

It's not the KLOC count that makes a project hard, it's usually the skill in designing the code, long before fingers are put to keyboard. For a well-designed project, laying down 6KLOC is an exercise in typing. If very well designed, 6KLOC could be anywhere from 3 to 6 weeks of fingers-on-keyboards, or it could be 6 months to a year.

Yesterday I put down 700 lines of C for a test validation dongle. There are also two Easter Eggs embedded in the part -- a "breathing" LED (the stock LED indication is on, off or 1Hz blinking -- my cow-orker likes something with a more triangular waveform) for one of my colleagues and a variable frequency PWM-based sine wave generator for myself (because I like me some sine waves and a needed the functionality for something else I'm making).
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
The question doesn't have an answer.

And to the person who commented that a 6,000 line project in C would drive someone "crazy" if they were to write in in assembly, 6,000 lines of C is nothing. My first large scale assembler project was about 50KLOC. That's small compared to what I've done since, where KLOC counts get into the hundreds and thousands (millions of lines of code).
Oh WOW!!! I'm very impressed!

Guess we have a BIG girl in the house! That's great!

I can feel the confidence in you...:D
It makes me think of a BIG boy who seems to have dissappeared in the forum...
 

MrChips

Joined Oct 2, 2009
27,722
I've been working on a STM32F407 for about 6 months now, on and off.
So I'll estimate a total time of 100 hours spent coding.

What I have so far is a digital scope, FFT, spectrum analyzer, mouse, keyboard, windows, buttons, menus.

Total code space so far is 25KB which I estimate to be 10KB user code + 15KB libraries.

My C code estimate is 2500 lines of code -> 10KB code
I assume this is a 32-bit MCU giving 2500 instructions.
That would give 1:1 C to asm which doesn't sound right.

In terms of code productivity, I'm batting about 25 lines/hour, i.e about 100 lines/day if we call it a 4-hour day.

For this project I estimate I am about half-way completed. Hence my total LOC will be about 5000.
 

BillO

Joined Nov 24, 2008
990
Back in my fire-craker days, I used to lay down 2000 lines of APL code on stone tablets using a toothpick while walking 15 miles up-hill, to and from school each day on an Escher stairs!:p
 
Top