简体   繁体   English

GNU C中的float的printf实现,半主机

[英]Implementation of printf for float in GNU C , semihosting

I need to use gnu c printf function to send floating point number to semihosting console. 我需要使用gnu c printf函数将浮点数发送到半主机控制台。 The current implementation printf(vsnprintf) is 当前的实现printf(vsnprintf)是

 signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap)
{
    char          fill;
    unsigned char width;
    signed int    num = 0;
    signed int    size = 0;

    /* Clear the string */
    if (pStr) {

        *pStr = 0;
    }

    /* Phase string */
    while (*pFormat != 0 && size < length) {

        /* Normal character */
        if (*pFormat != '%') {

            *pStr++ = *pFormat++;
            size++;
        }
        /* Escaped '%' */
        else if (*(pFormat+1) == '%') {

            *pStr++ = '%';
            pFormat += 2;
            size++;
        }
        /* Token delimiter */
        else {

            fill = ' ';
            width = 0;
            pFormat++;

            /* Parse filler */
            if (*pFormat == '0') {

                fill = '0';
                pFormat++;
            }

            /* Parse width */
            while ((*pFormat >= '0') && (*pFormat <= '9')) {

                width = (width*10) + *pFormat-'0';
                pFormat++;
            }

            /* Check if there is enough space */
            if (size + width > length) {

                width = length - size;
            }

            /* Parse type */
            switch (*pFormat) {
            case 'd': 
            case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break;
            case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break;
            case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break;
            case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break;
            case 's': num = PutString(pStr, va_arg(ap, char *)); break;
            case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break;
            default:
                return EOF;
            }

            pFormat++;
            pStr += num;
            size += num;
        }
    }

    /* NULL-terminated (final \0 is not counted) */
    if (size < length) {

        *pStr = 0;
    }
    else {

        *(--pStr) = 0;
        size--;
    }

    return size;
}

Any help to implement 'f' format specifier is greatly appreciated 非常感谢您对实现'f'格式说明符的任何帮助

It seems you are using a custom printf implementation as opposed to using one from libc your toolchain. 似乎您正在使用自定义的printf实现,而不是使用来自工具链中libc的实现。 Provided you have implemented syscalls , you should be able to simply switch to the standard printf implementation of the toolchain by simply not compiling in your stdio implementation. 前提是您已实现syscall ,则只需不编译到stdio实现中,就可以简单地切换到工具链的标准printf实现。

Another way could be to make a PutFloat function that simply multiplies the input by a power of 10 and then separately prints the above and below decimal parts of the number using existing integer prints. 另一种方法是制作一个PutFloat函数,该函数将输入乘以10的幂,然后使用现有的整数打印分别打印数字的小数点上下部分。 For example: 例如:

x = (signed int)floatIn*10000;
PutSignedInt(x/10000);
PutChar('.');
ax = abs(x);
ay = abs(y);
ax = ax - ay*10000;
PutSignedInt(ax);

If you get the idea, you should be able to fill in the details yourself. 如果您有主意,则应该可以自己填写详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM