Hello ! I have a problem which I can't resolve. I wrote program for 89C2051 for one of my friend in Basom Basic compiler (http://www.mcselec.com). Compiled code was about 1945 bytes. Since I prefer C, I transalte Bascom basic to C. But when I compile, generated code was 2313. I try to change optimization parameters, but generated code never go below 2313. Even bigger. Where am I wrong ? Is it possible that stupid Basic make smaller code tne best C compiler ? Then I find out that this code: while (1) { printf ("Hello World\n"); } generate fully 1093 bytes (!) for Atmel 89C2051. 1093 bytes???? Three lines? Same program in Bascom generate only 154 (!) bytes. When I remove all "printf" from my C code, generated code was arond 2200 byte. But, with same action in Basic code (remove all "print") 1340 bytes long code. Exactly same program in Keil C and Bascom basic generate 860 byte smaller code ! I don't belive this. Do I have to program in (stupid) Basic if I want smaller code ? I still belive I did mistake somewhere, so I need help. I will send both C and basic code for interested people. With best regards !
You can get into situations where, for example, if there are multiple source files, each source module calls for a different "edition" of printf. If this is the case, it is actually up to the linker to resolve which printf gets included. It's fairly easy for the compiler to say, "Oh, this module calls printf and only has an unsigned int in the format string. OK, include printf#1." In another module, however, the compiler might say, "Oh. this module calls printf and uses floating-point format specifiers in the format string (but no unsigned ints are used). OK, include printf#4." Now, the linker has to make sense of all of these different printf functions. And, at the end of the day, the linker includes most of what's in the standard printf. The only applications this would work well for would be the Hello World example. I mean, if I include sprintf in my application, I'm going to use it to my best advantage. I'm not going to try to use a printf SUBSET to save memory. If memory is that precious, I'm going to write my own formatting routines. printf has been an acknowledged memory hog since C was invented. There have been very few successful attempts at reducing its size. For its size, I think it does quiet well. Jon
I just downloaded the BASCOM demo and played around for a while. It's pretty neat. After playing around for a while, I decided to run a real program and compare it to C51. Not wanting to spend the rest of my life on this, I chose the SIEVE. Here are the results: BASCOM: Code Size: 866 Bytes Execution Speed: 23.553726 Seconds Keil (using printf): Code Size: 1,248 Bytes Execution Speed: 6.331546 Seconds Keil (using putchar and puts): Code Size: 524 Bytes Execution Speed: 6.330123 Seconds I did all speed testing with the uVision2 Debugger/Simulator under identical circumstances. From this little comparison, I think you can tell that the printf is what kills the C code--but not the performance. And, the difference between a CUSTOM formatter (using puts and putchar) and printf saves 724 bytes. I'll be more than happy to post the source for these things (of course, the SIEVE source comes with the Keil tools) if anyone wants to check my work. Jon
And...with linker code packing... Keil (using putchar and puts and linker code packing): Code Size: 486 Bytes Execution Speed: 6.330105 Seconds Jon
Jon, Here's a suggestion for the manual. How about adding a note to the printf page, pointing out that puts/putchar use much less code memory for simple output.
"And...with linker code packing... Keil (using putchar and puts and linker code packing): Code Size: 486 Bytes Execution Speed: 6.330105 Seconds" Jon, That's very interesting. What optimisation level did you use? Stefan
What optimisation level did you use? The very highest one I could - 11. Jon
"What optimisation level did you use? The very highest one I could - 11." I'll bet you could shrink it a bit further using level 9. Stefan