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",
35 uint16_t ax = 0x0e00|(ch&0xff);
36 asm volatile("movw $0x0007,%%bx ; "
39 :: "ebx", "ecx", "edx", "esi", "edi", "ebp");
45 int puts(const char *s)
59 * Oh, it's a waste of space, but oh-so-yummy for debugging. It's just
60 * initialization code anyway, so it doesn't take up space when we're
61 * actually running. This version of printf() does not include 64-bit
62 * support. "Live with it."
64 * Most of this code was shamelessly snarfed from the Linux kernel, then
71 return (ch >= '0') && (ch <= '9');
74 static int skip_atoi(const char **s)
79 i = i*10 + *((*s)++) - '0';
83 unsigned int atou(const char *s)
87 i = i*10 + (*s++ - '0');
91 static int strnlen(const char *s, int maxlen)
94 while ( *es && maxlen ) {
101 #define ZEROPAD 1 /* pad with zero */
102 #define SIGN 2 /* unsigned/signed long */
103 #define PLUS 4 /* show plus */
104 #define SPACE 8 /* space if plus */
105 #define LEFT 16 /* left justified */
106 #define SPECIAL 32 /* 0x */
107 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
109 #define do_div(n,base) ({ \
111 __res = ((unsigned long) n) % (unsigned) base; \
112 n = ((unsigned long) n) / (unsigned) base; \
115 static char * number(char * str, long num, int base, int size, int precision
119 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
123 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
126 if (base < 2 || base > 36)
128 c = (type & ZEROPAD) ? '0' : ' ';
135 } else if (type & PLUS) {
138 } else if (type & SPACE) {
143 if (type & SPECIAL) {
152 else while (num != 0)
153 tmp[i++] = digits[do_div(num,base)];
157 if (!(type&(ZEROPAD+LEFT)))
162 if (type & SPECIAL) {
173 while (i < precision--)
182 /* Forward decl. needed for IP address printing stuff... */
183 int sprintf(char * buf, const char *fmt, ...);
185 int vsprintf(char *buf, const char *fmt, va_list args)
193 int flags; /* flags to number() */
195 int field_width; /* width of output field */
196 int precision; /* min. # of digits for integers; max
197 number of chars for from string */
198 int qualifier; /* 'h', 'l', or 'L' for integer fields */
200 for (str=buf ; *fmt ; ++fmt) {
209 ++fmt; /* this also skips first '%' */
211 case '-': flags |= LEFT; goto repeat;
212 case '+': flags |= PLUS; goto repeat;
213 case ' ': flags |= SPACE; goto repeat;
214 case '#': flags |= SPECIAL; goto repeat;
215 case '0': flags |= ZEROPAD; goto repeat;
218 /* get field width */
221 field_width = skip_atoi(&fmt);
222 else if (*fmt == '*') {
224 /* it's the next argument */
225 field_width = va_arg(args, int);
226 if (field_width < 0) {
227 field_width = -field_width;
232 /* get the precision */
237 precision = skip_atoi(&fmt);
238 else if (*fmt == '*') {
240 /* it's the next argument */
241 precision = va_arg(args, int);
247 /* get the conversion qualifier */
249 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
260 while (--field_width > 0)
262 *str++ = (unsigned char) va_arg(args, int);
263 while (--field_width > 0)
268 s = va_arg(args, char *);
269 len = strnlen(s, precision);
272 while (len < field_width--)
274 for (i = 0; i < len; ++i)
276 while (len < field_width--)
281 if (field_width == -1) {
282 field_width = 2*sizeof(void *);
286 (unsigned long) va_arg(args, void *), 16,
287 field_width, precision, flags);
292 if (qualifier == 'l') {
293 long * ip = va_arg(args, long *);
296 int * ip = va_arg(args, int *);
305 /* integer number formats - set up the flags and "break" */
330 if (qualifier == 'l')
331 num = va_arg(args, unsigned long);
332 else if (qualifier == 'h') {
333 num = (unsigned short) va_arg(args, int);
336 } else if (flags & SIGN)
337 num = va_arg(args, int);
339 num = va_arg(args, unsigned int);
340 str = number(str, num, base, field_width, precision, flags);
346 int sprintf(char * buf, const char *fmt, ...)
352 i=vsprintf(buf,fmt,args);
357 int printf(const char *fmt, ...)
359 char printf_buf[1024];
364 printed = vsprintf(printf_buf, fmt, args);