Hi,

itoa() in msp430-libc does not output zero value.

I have attached the corrected source, also version for ltoa/utoa/ultoa with changed Makefile and stdlib.h.

Hope this can be put into cvs by somebody.

Hardy
/*
 * Copyright (c) 2001 Dmitry Dicky di...@eis.ru
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: stdlib.h,v 1.10 2004/07/02 19:09:30 ctakahashi Exp $
 */

#ifndef _STDLIB_H_
#define _STDLIB_H_

#include <stddef.h>
#include <sys/types.h>

#ifndef __ATTR_CONST__
#define __ATTR_CONST__ __attribute__((__const__))
#endif
 
#ifndef __ATTR_MALLOC__
#define __ATTR_MALLOC__ __attribute__((__malloc__))
#endif
 
#ifndef __ATTR_NORETURN__
#define __ATTR_NORETURN__ __attribute__((__noreturn__))
#endif
 
#ifndef __ATTR_PURE__
#define __ATTR_PURE__ __attribute__((__pure__))
#endif

#ifndef exit
extern void exit(int) __asm__("__stop_progExec__") __ATTR_CONST__;
#endif


typedef struct {  
        int quot;
        int rem;
} div_t;

extern  div_t div(int num, int denom);


typedef struct {  
        long quot;
        long rem;
} ldiv_t;

extern ldiv_t ldiv(long num, long denom);




extern __inline__ int abs(int __x) __ATTR_CONST__;
extern __inline__ int
abs(int __x)
{
return (__x < 0) ? -__x : __x;
}
         
extern __inline__ long labs(long __x) __ATTR_CONST__;
extern __inline__ long
labs(long __x)
{
return (__x < 0) ? -__x : __x;
}



long strtol(const char *, char **, int);
unsigned long strtoul(const char *, char **, int);

int atoi(const char *p);
long atol(const char *p);
char *itoa(int num, char *str, int radix);
char *utoa(unsigned num, char *str, int radix);
char *ltoa(long num, char *str, int radix);
char *ultoa(unsigned long num, char *str, int radix);

void *malloc(size_t size);
void free (void *p);
void * bsearch(const void *, const void *, size_t, size_t, register int 
(*compar)(const void *, const void *));
                                        


#ifndef RAND_MAX
#define RAND_MAX        0x7fffffffL
#endif

int rand(void);
void srand(unsigned __seed);
int rand_r(unsigned *__seed);

#endif


#
# MSP430 libc
#
# $Id: Makefile,v 1.41 2004/07/08 19:01:14 coppice Exp $
#
VERSION = 20030812

# installation prefix (set this if you don't install by hand)

prefix = /usr/local/msp430

# name of target architecture (used for conform naming)

target = msp430

prefix_target = ${prefix}/${target}
bindir = ${prefix_target}/bin
includedir = ${prefix_target}/include
libdir = ${prefix_target}/lib

srcdir = .

CC = ${target}-gcc
AS = ${target}-gcc -x assembler-with-cpp
AR = ${target}-ar
RM = rm
MD = mkdir -p $@
INSTALL = install -c -m644 $^/

ASFLAGS = -Wa,-gstabs -D_GNU_ASSEMBLER_
CFLAGS = -Wall -O2 -g -v

# make sure we can find our header files (not the installed ones)

ALL_ASFLAGS += -I$(srcdir)/../include -I$(srcdir) ${ASFLAGS}
ALL_CFLAGS += -I$(srcdir)/../include -I$(srcdir) ${CFLAGS}

opt_speed_cflags = -O2

# further declaration... 
ifdef gnu
 source_dirs = gnu
else
 source_dirs = bsd
endif

source_dirs = 

VPATH = $(addprefix $(srcdir)/, $(source_dirs))

all: build-crt build-libc build-libm

install: install-crt install-libc install-headers install-libm

clean: clean-crt clean-libc clean-libm


crt_all_objs = \
        crt430x110.o  crt430x112.o \
        crt430x1101.o crt430x1111.o crt430x1121.o \
        crt430x1122.o crt430x1132.o \
        crt430x122.o  crt430x123.o \
        crt430x1222.o crt430x1232.o \
        crt430x133.o  crt430x135.o \
        crt430x1331.o crt430x1351.o \
        crt430x147.o  crt430x148.o  crt430x149.o \
        crt430x1471.o crt430x1481.o crt430x1491.o \
        crt430x155.o  crt430x156.o  crt430x157.o \
        crt430x167.o  crt430x168.o  crt430x169.o  crt430x1610.o crt430x1611.o 
crt430x1612.o \
        crt430x311.o  crt430x312.o  crt430x313.o  crt430x314.o  crt430x315.o \
        crt430x323.o  crt430x325.o  crt430x336.o  crt430x337.o \
        crt430x412.o  crt430x413.o  crt430x415.o  crt430x417.o \
        crt430xE423.o crt430xE425.o crt430xE427.o \
        crt430xW423.o crt430xW425.o crt430xW427.o \
        crt430xG437.o crt430xG438.o crt430xG439.o \
        crt430x435.o  crt430x436.o  crt430x437.o \
        crt430x447.o  crt430x448.o  crt430x449.o

build-crt: ${crt_all_objs}

# match by name

${crt_all_objs}: crt%.o: gcrt0.S
        ${AS} ${CPPFLAGS} -mmcu=msp$* ${ALL_ASFLAGS} -c $(ABSPATH)$< -o $@

clean-crt:
        ${RM} -f ${crt_all_objs}

install-crt: ${crt_all_objs} ${libdir}
        ${INSTALL}

#--------------- a bit of libc

libc_libs = msp1/libc.a msp2/libc.a

libc_c_sources = abs.c atol.c bsearch.c errno.c labs.c \
_init_section__.c malloc.c atoi.c strtol.c strtoul.c \
sprintf.c snprintf.c vsprintf.c vsnprintf.c \
printf.c vprintf.c vuprintf.c uprintf.c puts.c \
rand.c itoa.c ltoa.c utoa.c ultoa.c

libc_asm_sources = abort.S div.S exit.S ldiv.S setjmp.S

libc_str_sources = \
isascii.c memccpy.c strchr.c strncat.c swab.c \
isblank.c memchr.c strcmp.c strncmp.c toascii.c \
bcmp.c iscntrl.c memcmp.c strcpy.c strncpy.c tolower.c \
bcopy.c isdigit.c memcpy.c strcspn.c strpbrk.c toupper.c \
bzero.c islower.c memmove.c strdup.c strrchr.c \
ffs.c isprint.c memset.c strlcat.c strsep.c \
index.c isspace.c rindex.c strlcpy.c strspn.c \
isalnum.c isupper.c strcasecmp.c strlen.c strstr.c \
isalpha.c isxdigit.c strcat.c strncasecmp.c strtok.c \
ispunct.c



libc_msp430_ct_objs_1 = ${libc_c_sources:%.c=%.o}
libc_msp430_ct_objs_2 = ${libc_c_sources:%.c=%.o}

libc_msp430_asmt_objs_1 = ${libc_asm_sources:%.S=%.o}
libc_msp430_asmt_objs_2 = ${libc_asm_sources:%.S=%.o}



libc_msp430_c_objs_1 = $(addprefix msp1/, $(libc_msp430_ct_objs_1))
libc_msp430_c_objs_2 = $(addprefix msp2/, $(libc_msp430_ct_objs_2))

libc_msp430_S_objs_1 = $(addprefix msp1/, $(libc_msp430_asmt_objs_1))
libc_msp430_S_objs_2 = $(addprefix msp2/, $(libc_msp430_asmt_objs_2))


libc_msp430_objs_1 = ${libc_msp430_c_objs_1}
libc_msp430_objs_2 = ${libc_msp430_c_objs_2}


build-libc: ${libc_libs}

${libc_msp430_objs_1}: msp1/%.o: stdlib/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp1 -c $(ABSPATH)$< -o $@

${libc_msp430_objs_2}: msp2/%.o: stdlib/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp2 -c $(ABSPATH)$< -o $@

${libc_msp430_S_objs_1}: msp1/%.o: stdlib/%.S
        ${AS} -D_GNU_ASSEMBLER_ -mmcu=msp1 -c $(ABSPATH)$< -o $@

${libc_msp430_S_objs_2}: msp2/%.o: stdlib/%.S
        ${AS} -D_GNU_ASSEMBLER_ -mmcu=msp2 -c $(ABSPATH)$< -o $@

libc_msp430_cstr_objs_1 = ${libc_str_sources:%.c=%.o}
libc_msp430_cstr_objs_2 = ${libc_str_sources:%.c=%.o}

libc_msp430_str_objs_1 = $(addprefix msp1/, $(libc_msp430_cstr_objs_1))
libc_msp430_str_objs_2 = $(addprefix msp2/, $(libc_msp430_cstr_objs_2))


${libc_msp430_str_objs_1}: msp1/%.o: string/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp1 -c $(ABSPATH)$< -o $@

${libc_msp430_str_objs_2}: msp2/%.o: string/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp2 -c $(ABSPATH)$< -o $@


all_objs1 = ${libc_msp430_objs_1} ${libc_msp430_str_objs_1} 
${libc_msp430_S_objs_1}
all_objs2 = ${libc_msp430_objs_2} ${libc_msp430_str_objs_2} 
${libc_msp430_S_objs_2}

msp1/libc.a: ${all_objs1}
        ${AR} rc $@ $?

msp2/libc.a: ${all_objs2}
        ${AR} rc $@ $?

.PHONY: clean-libc
clean-libc:
        ${RM} -f msp1/libc.a msp2/libc.a msp1/*.o msp2/*.o

install-libc: ins-msp1 ins-msp2 ins-lib

ins-msp1: msp1/libc.a ${libdir}/msp1
        ${INSTALL}

ins-msp2: msp2/libc.a ${libdir}/msp2
        ${INSTALL}

ins-lib: msp1/libc.a ${libdir}
        ${INSTALL}

#--------------- headers ---------------

install-headers: ins-hr ins-hs ins-msp430

ins-hr: $(wildcard $(srcdir)/../include/*.h) ${includedir}
        ${INSTALL}

ins-hs: $(wildcard $(srcdir)/../include/sys/*.h) ${includedir}/sys
        ${INSTALL}

ins-msp430: $(wildcard $(srcdir)/../include/msp430/*.h) ${includedir}/msp430
        ${INSTALL}

fix-limits: $(wildcard $(srcdir)/../include/limits.h) 
${includedir}/../../lib/gcc-lib/msp430/3.0/include
        ${INSTALL}      

#--------------- directories ---------------
libc_dirs = msp1 msp2
install_lib_dirs = $(libdir) $(libdir)/msp1 $(libdir)/msp2 

$(libc_dirs) $(install_lib_dirs) $(includedir) $(includedir)/sys 
$(includedir)/msp430: 
        $(MD)


#------------------------------------------------------------------------
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#-------------- Math & FP libs ---------------

math: build-libm


basic_fp = divsf.c fixsfsi.c floatdisf.c gtsf.c mulsf.c \
addsf.c eqsf.c fixunssfdi.c floatsisf.c lesf.c negsf.c \
cmpsf.c fixsfdi.c fixunssfsi.c gesf.c ltsf.c subsf.c nesf.c

fppfunc = ef_acos.c ef_hypot.c ef_remainder.c kf_sin.c sf_erf.c sf_isinf.c 
sf_rint.c \
ef_acosh.c ef_j0.c ef_scalb.c kf_tan.c sf_expm1.c sf_isnan.c sf_scalbn.c \
ef_asin.c ef_j1.c ef_sinh.c sf_asinh.c sf_fabs.c sf_ldexp.c sf_signif.c \
ef_atan2.c ef_jn.c ef_sqrt.c sf_atan.c sf_finite.c sf_log1p.c sf_sin.c \
ef_atanh.c ef_log.c erf_gamma.c sf_cbrt.c sf_floor.c sf_logb.c sf_tan.c \
ef_cosh.c ef_log10.c erf_lgamma.c sf_ceil.c sf_frexp.c sf_modf.c sf_tanh.c \
ef_exp.c ef_pow.c kf_cos.c sf_copysign.c sf_ilogb.c sf_nan.c \
ef_fmod.c ef_rem_pio2.c kf_rem_pio2.c sf_cos.c sf_infinity.c sf_nextafter.c


libm_libs = msp1/libm.a msp2/libm.a msp1/libfp.a msp2/libfp.a

libm_msp430_fp_objs_1 = ${basic_fp:%.c=%.o}
libm_msp430_fp_objs_2 = ${basic_fp:%.c=%.o}

libm_msp430_fc_objs_1 = ${fppfunc:%.c=%.o}
libm_msp430_fc_objs_2 = ${fppfunc:%.c=%.o}


libm_msp430_c_objs_1 = $(addprefix msp1/, $(libm_msp430_fp_objs_1))
libm_msp430_c_objs_2 = $(addprefix msp2/, $(libm_msp430_fp_objs_2))

libm_msp430_cf_objs_1 = $(addprefix msp1/, $(libm_msp430_fc_objs_1))
libm_msp430_cf_objs_2 = $(addprefix msp2/, $(libm_msp430_fc_objs_2))


build-libm: ${libm_libs}

${libm_msp430_c_objs_1}: msp1/%.o: libm/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp1 -c $(ABSPATH)$< -o $@

${libm_msp430_c_objs_2}: msp2/%.o: libm/%.c
        ${CC} -c ${CPPFLAGS} ${ALL_CFLAGS} -mmcu=msp2 -c $(ABSPATH)$< -o $@


${libm_msp430_cf_objs_1}: msp1/%.o: libm/%.c
        ${CC} ${CPPFLAGS} -mmcu=msp1 ${ALL_CFLAGS} -c $(ABSPATH)$< -o $@

${libm_msp430_cf_objs_2}: msp2/%.o: libm/%.c
        ${CC} ${CPPFLAGS} -mmcu=msp2 ${ALL_CFLAGS} -c $(ABSPATH)$< -o $@


allm_objs1 = ${libm_msp430_cf_objs_1} 
allm_objs2 = ${libm_msp430_cf_objs_2}

msp1/libm.a: ${allm_objs1}
        ${AR} rc $@ $?

msp2/libm.a: ${allm_objs2}
        ${AR} rc $@ $?

msp1/libfp.a:${libm_msp430_c_objs_1}
        ${AR} rc $@ $?

msp2/libfp.a:${libm_msp430_c_objs_2}
        ${AR} rc $@ $?

install-libm: insm-msp1 insm-msp2 insm-lib

insm-msp1: msp1/libm.a msp1/libfp.a ${libdir}/msp1
        ${INSTALL}

insm-msp2: msp2/libm.a msp2/libfp.a ${libdir}/msp2
        ${INSTALL}

insm-lib: msp1/libm.a ${libdir}
        ${INSTALL}

.PHONY: clean-libm
clean-libm:
        ${RM} -f msp1/libm.a msp2/libm.a msp1/libfp.a msp2/libfp.a msp1/*.o 
msp2/*.o

/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/07/02 19:09:31 $                                             *
*******************************************************/
#include <stdlib.h>

char *ultoa(unsigned long num, char *str, int radix) {
        char temp[33];  //an int can only be 16 bits long
                                        //at radix 2 (binary) the string
                                        //is at most 16 + 1 null long.
        int temp_loc = 0;
        int digit;
        int str_loc = 0;

        //construct a backward string of the number.
        do {
                digit = (unsigned int)num % radix;
                if (digit < 10) 
                        temp[temp_loc++] = digit + '0';
                else
                        temp[temp_loc++] = digit - 10 + 'A';
                ((unsigned int)num) /= radix;
        } while ((unsigned int)num > 0);

        temp_loc--;


        //now reverse the string.
        while ( temp_loc >=0 ) {// while there are still chars
                str[str_loc++] = temp[temp_loc--];      
        }
        str[str_loc] = 0; // add null termination.

        return str;
}

/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/07/02 19:09:31 $                                             *
*******************************************************/
#include <stdlib.h>

char *utoa(unsigned num, char *str, int radix) {
        char temp[17];  //an int can only be 16 bits long
                                        //at radix 2 (binary) the string
                                        //is at most 16 + 1 null long.
        int temp_loc = 0;
        int digit;
        int str_loc = 0;

        //construct a backward string of the number.
        do {
                digit = (unsigned int)num % radix;
                if (digit < 10) 
                        temp[temp_loc++] = digit + '0';
                else
                        temp[temp_loc++] = digit - 10 + 'A';
                ((unsigned int)num) /= radix;
        } while ((unsigned int)num > 0);

        temp_loc--;


        //now reverse the string.
        while ( temp_loc >=0 ) {// while there are still chars
                str[str_loc++] = temp[temp_loc--];      
        }
        str[str_loc] = 0; // add null termination.

        return str;
}

/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/07/02 19:09:31 $                                             *
*******************************************************/
#include <stdlib.h>

char *itoa(int num, char *str, int radix) {
        char sign = 0;
        char temp[17];  //an int can only be 16 bits long
                                        //at radix 2 (binary) the string
                                        //is at most 16 + 1 null long.
        int temp_loc = 0;
        int digit;
        int str_loc = 0;

        //save sign for radix 10 conversion
        if (radix == 10 && num < 0) {
                sign = 1;
                num = -num;
        }
        
        //construct a backward string of the number.
        do {
                digit = (unsigned int)num % radix;
                if (digit < 10) 
                        temp[temp_loc++] = digit + '0';
                else
                        temp[temp_loc++] = digit - 10 + 'A';
                ((unsigned int)num) /= radix;
        } while ((unsigned int)num > 0);

        //now add the sign for radix 10
        if (radix == 10 && sign) {
                temp[temp_loc] = '-';
        } else {
                temp_loc--;
        }


        //now reverse the string.
        while ( temp_loc >=0 ) {// while there are still chars
                str[str_loc++] = temp[temp_loc--];      
        }
        str[str_loc] = 0; // add null termination.

        return str;
}

/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/07/02 19:09:31 $                                             *
*******************************************************/
#include <stdlib.h>

char *ltoa(long num, char *str, int radix) {
        char sign = 0;
        char temp[33];  //an int can only be 32 bits long
                                        //at radix 2 (binary) the string
                                        //is at most 16 + 1 null long.
        int temp_loc = 0;
        int digit;
        int str_loc = 0;

        //save sign for radix 10 conversion
        if (radix == 10 && num < 0) {
                sign = 1;
                num = -num;
        }
        
        //construct a backward string of the number.
        do {
                digit = (unsigned int)num % radix;
                if (digit < 10) 
                        temp[temp_loc++] = digit + '0';
                else
                        temp[temp_loc++] = digit - 10 + 'A';
                ((unsigned int)num) /= radix;
        } while ((unsigned int)num > 0);

        //now add the sign for radix 10
        if (radix == 10 && sign) {
                temp[temp_loc] = '-';
        } else {
                temp_loc--;
        }


        //now reverse the string.
        while ( temp_loc >=0 ) {// while there are still chars
                str[str_loc++] = temp[temp_loc--];      
        }
        str[str_loc] = 0; // add null termination.

        return str;
}

Reply via email to