2 /* ----------------------------------------------------------------------- *
4 * Copyright 2001 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Bostom MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * Output to the screen
27 asm volatile("movw $0x0e0d,%%ax ; "
28 "movw $0x0007,%%bx ; "
30 ::: "eax", "ebx", "ecx", "edx",
34 asm volatile("movw $0x0007,%%bx ; "
36 :: "a" ((uint16_t)(0x0e00|(ch&0xff)))
37 : "eax", "ebx", "ecx", "edx",
43 int puts(const char *s)
57 * Oh, it's a waste of space, but oh-so-yummy for debugging. It's just
58 * initialization code anyway, so it doesn't take up space when we're
59 * actually running. This version of printf() does not include 64-bit
60 * support. "Live with it."
62 * Most of this code was shamelessly snarfed from the Linux kernel, then
69 return (ch >= '0') && (ch <= '9');
72 static int skip_atoi(const char **s)
77 i = i*10 + *((*s)++) - '0';
81 static int strnlen(const char *s, int maxlen)
84 while ( *es && maxlen ) {
91 #define ZEROPAD 1 /* pad with zero */
92 #define SIGN 2 /* unsigned/signed long */
93 #define PLUS 4 /* show plus */
94 #define SPACE 8 /* space if plus */
95 #define LEFT 16 /* left justified */
96 #define SPECIAL 32 /* 0x */
97 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
99 #define do_div(n,base) ({ \
101 __res = ((unsigned long) n) % (unsigned) base; \
102 n = ((unsigned long) n) / (unsigned) base; \
105 static char * number(char * str, long num, int base, int size, int precision
109 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
113 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
116 if (base < 2 || base > 36)
118 c = (type & ZEROPAD) ? '0' : ' ';
125 } else if (type & PLUS) {
128 } else if (type & SPACE) {
133 if (type & SPECIAL) {
142 else while (num != 0)
143 tmp[i++] = digits[do_div(num,base)];
147 if (!(type&(ZEROPAD+LEFT)))
152 if (type & SPECIAL) {
163 while (i < precision--)
172 /* Forward decl. needed for IP address printing stuff... */
173 int sprintf(char * buf, const char *fmt, ...);
175 int vsprintf(char *buf, const char *fmt, va_list args)
183 int flags; /* flags to number() */
185 int field_width; /* width of output field */
186 int precision; /* min. # of digits for integers; max
187 number of chars for from string */
188 int qualifier; /* 'h', 'l', or 'L' for integer fields */
190 for (str=buf ; *fmt ; ++fmt) {
199 ++fmt; /* this also skips first '%' */
201 case '-': flags |= LEFT; goto repeat;
202 case '+': flags |= PLUS; goto repeat;
203 case ' ': flags |= SPACE; goto repeat;
204 case '#': flags |= SPECIAL; goto repeat;
205 case '0': flags |= ZEROPAD; goto repeat;
208 /* get field width */
211 field_width = skip_atoi(&fmt);
212 else if (*fmt == '*') {
214 /* it's the next argument */
215 field_width = va_arg(args, int);
216 if (field_width < 0) {
217 field_width = -field_width;
222 /* get the precision */
227 precision = skip_atoi(&fmt);
228 else if (*fmt == '*') {
230 /* it's the next argument */
231 precision = va_arg(args, int);
237 /* get the conversion qualifier */
239 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
250 while (--field_width > 0)
252 *str++ = (unsigned char) va_arg(args, int);
253 while (--field_width > 0)
258 s = va_arg(args, char *);
262 len = strnlen(s, precision);
265 while (len < field_width--)
267 for (i = 0; i < len; ++i)
269 while (len < field_width--)
274 if (field_width == -1) {
275 field_width = 2*sizeof(void *);
279 (unsigned long) va_arg(args, void *), 16,
280 field_width, precision, flags);
285 if (qualifier == 'l') {
286 long * ip = va_arg(args, long *);
289 int * ip = va_arg(args, int *);
298 /* integer number formats - set up the flags and "break" */
323 if (qualifier == 'l')
324 num = va_arg(args, unsigned long);
325 else if (qualifier == 'h') {
326 num = (unsigned short) va_arg(args, int);
329 } else if (flags & SIGN)
330 num = va_arg(args, int);
332 num = va_arg(args, unsigned int);
333 str = number(str, num, base, field_width, precision, flags);
339 int sprintf(char * buf, const char *fmt, ...)
345 i=vsprintf(buf,fmt,args);
350 int printf(const char *fmt, ...)
352 char printf_buf[1024];
357 printed = vsprintf(printf_buf, fmt, args);