Jump to content

Small ASM printf

Recommended Posts

So I've been working on my own version of a printf

Written entirely in ASM it currently supports:

- Fixed lengh: %03u = "010"

- Zero or space filled: %3X = " A"

- Hex

- Integer (0 - 65535)

- Strings (Due to the memory structure of MSP strings can be in flash or RAM)

- Characters

- Binary

- Octal


With all options on size is just over 200 bytes.


No support for long ints (32 bits wide)...Yet.

No support for signed integers, but I am trying to work that one out.

(Any help on implementing these would be great. :D )


This code isn't exactly fully completed yet but still very functional.


run: "make prog" to program to msp430g2231 for a serial demo.

Demo code requires mspgcc to build. (I used the uniarc version)


Link to post
Share on other sites

Update, I have added signed support. After some digging around with mspdebug.


Added a feature level define to save code space by disabling certain features. This makes it easy to remove features that are not needed in particular projects.

#define X_SUPPORT 0
//  Level   Features:       ~code size of xprint (bytes):
//  0       c,s,u,X,d,o,b   254
//  1       c,s,u,X,d       238
//  2       c,s,u,X         200
//  3       c,s,u           190
//  4       u               156


Running xprint(); destroys the values in r15 - r6. This could easily be fixed by adding some push and pops but would increase the code size. If any problems arise I can add them in.


Link to post
Share on other sites

ok. All the features of a good printf function have been added.


NEW!! Editable output function. This adds 2 bytes to BSS but no affect on function size.

(This should allow a function like sprintf(); to be created very easily.)

Define your function before any calls to xprint(); like so:




Format strings parsed to xprint follow these rules

%[Zero filled?][Min. Width][Long?][Type]


Zero Filled: either '0' or nothing.

Min. Width: 1-9 or nothing.

Long: 'l' or nothing.


  • [*:2vft2qoj] u - Unsigned
    [*:2vft2qoj] d - Signed integer
    [*:2vft2qoj] s - String [ram & flash]
    [*:2vft2qoj] c - Character
    [*:2vft2qoj] X - Hexadecimal
    [*:2vft2qoj] b - Binary
    [*:2vft2qoj] o - Octal (Not sure how useful this really is. Removing saves 8 bytes :P)


Some examples:

xprint("%05u",1234); \\produces "01234" *Zero filled, Width:5, Unsigned
xprint("%X",0x2F); \\produces "2F" *Hexadecimal
xprint("%9ld",-1234567); \\produces " -1234567" *Width:9, Long, Signed 



All this with a total footprint of 278 bytes of flash and 2 bytes of static ram. with all functions enabled

ASM xprint.s  >  xprint.o
  text	   data	    bss	    dec	    hex	filename
   278	      0	      2	    280	    118	build/xprint.o


Still only designed for mspgcc. Support for CCS and IAR should be easy if all compilers use the stack to transfer variable parameters.

Will continue to optimize the code.


Link to post
Share on other sites
  • 1 year later...



  I tried the latest v0.3 program in the post but not able to make it work on my enviroment.

  I am using a mspgcc-20120406 with chip msp430f2619.

  I tested my basic putc function using the hardware UART and is able to transmit each char successfully.


  After which, i add the xprint.s file and changed xputc to use the code in my putc.  It is able to compile and assemble successfully but upon run i got "rubblish" keep showing on my PC console.


  Any advice, please..




Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...