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 unsigned int atou(const char *s)
85 i = i*10 + (*s++ - '0');
89 static int strnlen(const char *s, int maxlen)
92 while ( *es && maxlen ) {
99 #define ZEROPAD 1 /* pad with zero */
100 #define SIGN 2 /* unsigned/signed long */
101 #define PLUS 4 /* show plus */
102 #define SPACE 8 /* space if plus */
103 #define LEFT 16 /* left justified */
104 #define SPECIAL 32 /* 0x */
105 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
107 #define do_div(n,base) ({ \
109 __res = ((unsigned long) n) % (unsigned) base; \
110 n = ((unsigned long) n) / (unsigned) base; \
113 static char * number(char * str, long num, int base, int size, int precision
117 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
121 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
124 if (base < 2 || base > 36)
126 c = (type & ZEROPAD) ? '0' : ' ';
133 } else if (type & PLUS) {
136 } else if (type & SPACE) {
141 if (type & SPECIAL) {
150 else while (num != 0)
151 tmp[i++] = digits[do_div(num,base)];
155 if (!(type&(ZEROPAD+LEFT)))
160 if (type & SPECIAL) {
171 while (i < precision--)
180 /* Forward decl. needed for IP address printing stuff... */
181 int sprintf(char * buf, const char *fmt, ...);
183 int vsprintf(char *buf, const char *fmt, va_list args)
191 int flags; /* flags to number() */
193 int field_width; /* width of output field */
194 int precision; /* min. # of digits for integers; max
195 number of chars for from string */
196 int qualifier; /* 'h', 'l', or 'L' for integer fields */
198 for (str=buf ; *fmt ; ++fmt) {
207 ++fmt; /* this also skips first '%' */
209 case '-': flags |= LEFT; goto repeat;
210 case '+': flags |= PLUS; goto repeat;
211 case ' ': flags |= SPACE; goto repeat;
212 case '#': flags |= SPECIAL; goto repeat;
213 case '0': flags |= ZEROPAD; goto repeat;
216 /* get field width */
219 field_width = skip_atoi(&fmt);
220 else if (*fmt == '*') {
222 /* it's the next argument */
223 field_width = va_arg(args, int);
224 if (field_width < 0) {
225 field_width = -field_width;
230 /* get the precision */
235 precision = skip_atoi(&fmt);
236 else if (*fmt == '*') {
238 /* it's the next argument */
239 precision = va_arg(args, int);
245 /* get the conversion qualifier */
247 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
258 while (--field_width > 0)
260 *str++ = (unsigned char) va_arg(args, int);
261 while (--field_width > 0)
266 s = va_arg(args, char *);
267 len = strnlen(s, precision);
270 while (len < field_width--)
272 for (i = 0; i < len; ++i)
274 while (len < field_width--)
279 if (field_width == -1) {
280 field_width = 2*sizeof(void *);
284 (unsigned long) va_arg(args, void *), 16,
285 field_width, precision, flags);
290 if (qualifier == 'l') {
291 long * ip = va_arg(args, long *);
294 int * ip = va_arg(args, int *);
303 /* integer number formats - set up the flags and "break" */
328 if (qualifier == 'l')
329 num = va_arg(args, unsigned long);
330 else if (qualifier == 'h') {
331 num = (unsigned short) va_arg(args, int);
334 } else if (flags & SIGN)
335 num = va_arg(args, int);
337 num = va_arg(args, unsigned int);
338 str = number(str, num, base, field_width, precision, flags);
344 int sprintf(char * buf, const char *fmt, ...)
350 i=vsprintf(buf,fmt,args);
355 int printf(const char *fmt, ...)
357 char printf_buf[1024];
362 printed = vsprintf(printf_buf, fmt, args);