How could I compare C and Assempler on a 8051. What are the advantages and disadvantages of each Programming language. I'm happy if anyone knows something about that or even know some good Homepages where I can find some useful information thanks
The assembler vs C question pops up all the time. The real questions to ask are: 1. Are you under any kind of time constraint? If "yes", C is the right choice. 2. Is portability an issue? If "yes", C is the right choice. 3. Is cost of development important? If "yes", C is the right choice. Note that assembly is not necessarly the right choice if any of your answers were "no". I want to be clear that I'm not dissing assembly. I like assembly. I'm good at it. It's just not the best choice for developing applications today--15 years ago (maybe) but not today. So, here are my brief summaries of assembly and C: Assembler A good assembly programmer can frequently beat the C compiler in terms of code size and execution speed. A great assembly programmer can do this most all the time. Most engineers, however, are NOT good assembly programmers--even fewer are great assembly programmers. In most cases, the compiler does a better job of efficient (minimal) data usage. For large applications, assembly takes more time to write and debug than C. If you need a routine or function you must create it yourself. Assembly does not promote software reuse. Assembly is not portable. C With a good C compiler, a fair C programmer can generate working applications quickly. New compiler optimizations shrink the code size of even poorly written C programs. Once you know C, it is fairly easy to write programs quickly. A standard library comes with the C compiler. So, you don't need to waste time creating memory routines or string functions. C is portable. Although chip-specific features are not, you can encapsulate them in functions or modules. More engineers know C so there is a better liklihood of finding C developers. Conclusion My preference is to develop entirely in C and get everything working. C allows me to do that in a short amount of time. (It usually makes the clients a lot happier when they see progress quickly.) After I have everything workin (in C) I look for program hot spots (parts of the code that don't run as fast as I want). I first analyse the algorithm that I used to see if that is the problem (it is in many cases). Then, if there are parts of the program that still run too slowly, I write them in assembly. Jon
"1. Are you under any kind of time constraint?" Here, Jon is talking about a time constraint on you the programmer; ie, have you got to get this code written in a restricted timescale? This is different to asking if the execution of your code is time-critical; ie, does your code need to run fast? "A good assembly programmer can frequently beat the C compiler in terms of code size and execution speed. A great assembly programmer can do this most all the time." I've added my emphasis there to Jon's exellent statement. You will often hear people say things like, "Assembly is faster than 'C'" - but that is only true if the Assembly is well-written in a good, efficient manner. Sure, in Assembly you can agonise over which instruction to use, which addressing mode to use; you can count the bytes & the clock cycles - and if you really know your stuff you can end up with a really fast, tight, efficient bit of code - but it might take you all day to write just one function! Jon's approach is good advice: get it working first in 'C', then optimise your 'C', then go to Assembly for any bits which still really need that extra bit of performance
I'll add to the previous replies that IMHO another important reason to know assembly-level programming is so that you can understand what the processor is doing when it's running your C code and doesn't do what you expect ... The trouble is, you don't reach that level of understanding until you've written a moderate amount of assembly that works. Writing very short assembly code is quite different from writing 64+ KBytes of working assembly. I wrote embedded code in assembly for some 15+ years -- now, I start everything in C. But I do still write assembly when it's required, especially when doing things that C does not inherently support (e.g. playing with the stack). And I examine a lot of C compiler output (in assembly) to judge the quality (and sometimes correctness) of the compiler's output. In short, I think that you will be a better C programmer if you have a working knowledge of assembly-level programming. Perhaps a good way to start is do your work in C, and fiddle around after hours in assembly. Regards,