AnsweredAssumed Answered

Replacing Printf() & sprintf() Functions

Question asked by Dave Miller on Jul 14, 2009
Latest reply on Sep 14, 2009 by Dave Miller

Hello All,


OK interesting issue that I have been told has happened before and has been solved by developers writing their own versions of printf().


I am using Codewarrior for DSC56800/E 8.1.1a and my target is the 56F8323 which has 32K of program space.


My issue is I am running out of codespace.  I have turned on Optimizations to level 3 and it is set for smaller code over speed.  I am still bumping into the code size limit and I have been going back through my code and optimizing where I can manually (which is a good thing). I have also used Processor Expert to generate a lot of my code.


In analyzing code usage I find that the excess bloat is not necessarily from PE but from some of the libs themselves.  I use sprintf a lot to format my strings before I then transmit them to the outside world.  Now I know I can modify putchr() to talk directly with a Serial port but I am actually using both ports and some parallel bits to get my data to the outside world so that is not really the best option.


In looking at the map file it appears that the printf.o is a huge codespace hog:


  address    size

  000026AA 000002C9 .text   Fparse_format (MSL C 56800E.lib printf.o       )
  00002973 00000172 .text   Flong2str (MSL C 56800E.lib printf.o       )
  00002AE5 000000BA .text   Fround_decimal (MSL C 56800E.lib printf.o       )
  00002B9F 000003D3 .text   Ffloat2str (MSL C 56800E.lib printf.o       )
  00002F72 0000036C .text   F__pformatter (MSL C 56800E.lib printf.o       )
  000032DE 0000002B .text   F__StringWrite (MSL C 56800E.lib printf.o       )
  00003309 00000039 .text   Fvsnprintf (MSL C 56800E.lib printf.o       )
  00003342 0000000D .text   Fsprintf (MSL C 56800E.lib printf.o       )

I have had another developer tell me they have handled this issue by writing their own printf() function.  So my questions to the community:


1) Can writing your own printf() funtion really save you a lot of code space?

2) If you write your own printf() funtion how do you call it?  Do you need to rename the function to something like myprintf()?  How does that keep other lib functions (such as string handlers) from calling the original printf() and therefore still adding the bloat?


So if anyone who can point me to a good book or internet link on this subject I would be most appreciative.