Saved ~75 bytes in printf and by elimination of fstrlen. There's only one

call and inlining doesn't increase the size.


git-svn-id: https://svn.code.sf.net/p/freedos/svn/kernel/trunk@803 6ac86273-5f31-0410-b378-82cca8765d1b
This commit is contained in:
Bart Oldeman 2004-03-20 10:41:30 +00:00
parent 28be8284a8
commit 7bf976be3f
2 changed files with 100 additions and 112 deletions

View File

@ -291,7 +291,7 @@ strcpy_loop:
jmp short pascal_return jmp short pascal_return
;****************************************************************** ;******************************************************************
%ifndef _INIT
global FSTRLEN global FSTRLEN
FSTRLEN: FSTRLEN:
call pascal_setup call pascal_setup
@ -301,6 +301,7 @@ FSTRLEN:
mov bl,4 mov bl,4
jmp short dostrlen jmp short dostrlen
%endif
;********************************************** ;**********************************************
global STRLEN global STRLEN

View File

@ -34,7 +34,6 @@
#endif #endif
#ifdef _INIT #ifdef _INIT
#define fstrlen init_fstrlen
#define handle_char init_handle_char #define handle_char init_handle_char
#define put_console init_put_console #define put_console init_put_console
#define ltob init_ltob #define ltob init_ltob
@ -91,6 +90,17 @@ void put_console(int c)
} }
} }
#else #else
#ifdef __WATCOMC__
void int10_e(char c);
#pragma aux int10_e = \
"push bp" \
"mov ah, 0xe" \
"mov bx, 0x0070" \
"int 0x10" \
"pop bp" \
parm [al] modify [ah bx];
#endif
void put_console(int c) void put_console(int c)
{ {
if (c == '\n') if (c == '\n')
@ -104,6 +114,8 @@ void put_console(int c)
_AH = 0x0e; _AH = 0x0e;
_BX = 0x0070; _BX = 0x0070;
__int__(0x10); __int__(0x10);
#elif defined(__WATCOMC__)
int10_e(c);
#elif defined(I86) #elif defined(I86)
__asm __asm
{ {
@ -130,27 +142,10 @@ typedef char *va_list;
static BYTE *charp = 0; static BYTE *charp = 0;
STATIC VOID handle_char(COUNT); STATIC VOID handle_char(COUNT);
STATIC BYTE * ltob(LONG, BYTE *, COUNT); STATIC void ltob(LONG, BYTE *, COUNT);
STATIC COUNT do_printf(CONST BYTE *, REG va_list); STATIC void do_printf(const char *, REG va_list);
int CDECL printf(CONST BYTE * fmt, ...); int CDECL printf(CONST BYTE * fmt, ...);
/* The following is user supplied and must match the following prototype */
VOID cso(COUNT);
#if defined(FORSYS)
COUNT fstrlen(BYTE FAR * s) /* don't want globals.h, sorry */
{
int i = 0;
while (*s++)
i++;
return i;
}
#else
COUNT /*ASMCFUNC*/ pascal fstrlen(BYTE FAR * s); /* don't want globals.h, sorry */
#endif
/* special handler to switch between sprintf and printf */ /* special handler to switch between sprintf and printf */
STATIC VOID handle_char(COUNT c) STATIC VOID handle_char(COUNT c)
{ {
@ -161,7 +156,7 @@ STATIC VOID handle_char(COUNT c)
} }
/* ltob -- convert an long integer to a string in any base (2-16) */ /* ltob -- convert an long integer to a string in any base (2-16) */
BYTE *ltob(LONG n, BYTE * s, COUNT base) STATIC void ltob(LONG n, BYTE * s, COUNT base)
{ {
ULONG u; ULONG u;
BYTE *p, *q; BYTE *p, *q;
@ -179,7 +174,7 @@ BYTE *ltob(LONG n, BYTE * s, COUNT base)
} }
} }
p = q = s; p = s;
do do
{ /* generate digits in reverse order */ { /* generate digits in reverse order */
*p++ = "0123456789abcdef"[(UWORD) (u % base)]; *p++ = "0123456789abcdef"[(UWORD) (u % base)];
@ -187,17 +182,18 @@ BYTE *ltob(LONG n, BYTE * s, COUNT base)
while ((u /= base) > 0); while ((u /= base) > 0);
*p = '\0'; /* terminate the string */ *p = '\0'; /* terminate the string */
while (q < --p) for (q = s; q < --p; q++)
{ /* reverse the digits */ { /* reverse the digits */
c = *q; c = *q;
*q++ = *p; *q = *p;
*p = c; *p = c;
} }
return s;
} }
#define LEFT 0 #define LEFT 0
#define RIGHT 1 #define RIGHT 1
#define ZEROSFILL 2
#define LONGARG 4
/* printf -- short version of printf to conserve space */ /* printf -- short version of printf to conserve space */
int CDECL printf(CONST BYTE * fmt, ...) int CDECL printf(CONST BYTE * fmt, ...)
@ -205,91 +201,72 @@ int CDECL printf(CONST BYTE * fmt, ...)
va_list arg; va_list arg;
va_start(arg, fmt); va_start(arg, fmt);
charp = 0; charp = 0;
return do_printf(fmt, arg); do_printf(fmt, arg);
return 0;
} }
int CDECL sprintf(BYTE * buff, CONST BYTE * fmt, ...) int CDECL sprintf(BYTE * buff, CONST BYTE * fmt, ...)
{ {
WORD ret;
va_list arg; va_list arg;
va_start(arg, fmt); va_start(arg, fmt);
charp = buff; charp = buff;
ret = do_printf(fmt, arg); do_printf(fmt, arg);
handle_char(NULL); handle_char('\0');
return ret; return 0;
} }
/* STATIC void do_printf(CONST BYTE * fmt, va_list arg)
ULONG FAR retcs(int i)
{
char *p = (char*)&i;
p -= 4;
return *(ULONG *)p;
}
*/
COUNT do_printf(CONST BYTE * fmt, va_list arg)
{ {
int base; int base;
BYTE s[11], FAR * p; BYTE s[11], FAR * p;
int c, flag, size, fill; int size;
int longarg; unsigned char flags;
long currentArg;
/* for (;*fmt != '\0'; fmt++)
long cs = retcs(1);
put_console("0123456789ABCDEF"[(cs >> 28) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 24) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 20) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 16) & 0x0f]);
put_console(':');
put_console("0123456789ABCDEF"[(cs >> 12) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 8) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 4) & 0x0f]);
put_console("0123456789ABCDEF"[(cs >> 0) & 0x0f]);
*/
while ((c = *fmt++) != '\0')
{ {
if (c != '%') if (*fmt != '%')
{ {
handle_char(c); handle_char(*fmt);
continue; continue;
} }
longarg = FALSE; fmt++;
size = 0; flags = RIGHT;
flag = RIGHT;
fill = ' ';
if (*fmt == '-') if (*fmt == '-')
{ {
flag = LEFT; flags = LEFT;
fmt++; fmt++;
} }
if (*fmt == '0') if (*fmt == '0')
{ {
fill = '0'; flags |= ZEROSFILL;
fmt++; fmt++;
} }
while (*fmt >= '0' && *fmt <= '9') size = 0;
while (1)
{ {
size = size * 10 + (*fmt++ - '0'); unsigned c = (unsigned char)(*fmt - '0');
if (c > 9)
break;
fmt++;
size = size * 10 + c;
} }
if (*fmt == 'l') if (*fmt == 'l')
{ {
longarg = TRUE; flags |= LONGARG;
fmt++; fmt++;
} }
c = *fmt++; switch (*fmt)
switch (c)
{ {
case '\0': case '\0':
return 0; va_end(arg);
return;
case 'c': case 'c':
handle_char(va_arg(arg, int)); handle_char(va_arg(arg, int));
@ -297,24 +274,24 @@ COUNT do_printf(CONST BYTE * fmt, va_list arg)
case 'p': case 'p':
{ {
UWORD w0 = va_arg(arg, UWORD); UWORD w0 = va_arg(arg, unsigned);
char *tmp = charp; char *tmp = charp;
sprintf(s, "%04x:%04x", va_arg(arg, UWORD), w0); sprintf(s, "%04x:%04x", va_arg(arg, unsigned), w0);
p = s; p = s;
charp = tmp; charp = tmp;
goto do_outputstring; break;
} }
case 's': case 's':
p = va_arg(arg, char *); p = va_arg(arg, char *);
goto do_outputstring; break;
case 'F': case 'F':
fmt++; fmt++;
/* we assume %Fs here */ /* we assume %Fs here */
case 'S': case 'S':
p = va_arg(arg, char FAR *); p = va_arg(arg, char FAR *);
goto do_outputstring; break;
case 'i': case 'i':
case 'd': case 'd':
@ -334,41 +311,48 @@ COUNT do_printf(CONST BYTE * fmt, va_list arg)
base = 16; base = 16;
lprt: lprt:
if (longarg) {
long currentArg;
if (flags & LONGARG)
currentArg = va_arg(arg, long); currentArg = va_arg(arg, long);
else else
currentArg = base < 0 ? (long)va_arg(arg, int) :
(long)va_arg(arg, unsigned int);
ltob(currentArg, s, base);
p = s;
do_outputstring:
size -= fstrlen(p);
if (flag == RIGHT)
{ {
currentArg = va_arg(arg, int);
if (base >= 0)
currentArg = (long)(unsigned)currentArg;
}
ltob(currentArg, s, base);
p = s;
}
break;
default:
handle_char('?');
handle_char(*fmt);
continue;
}
{
size_t i = 0;
while(p[i]) i++;
size -= i;
}
if (flags & RIGHT)
{
int ch = ' ';
if (flags & ZEROSFILL) ch = '0';
for (; size > 0; size--) for (; size > 0; size--)
handle_char(fill); handle_char(ch);
} }
for (; *p != '\0'; p++) for (; *p != '\0'; p++)
handle_char(*p); handle_char(*p);
for (; size > 0; size--) for (; size > 0; size--)
handle_char(fill); handle_char(' ');
continue;
default:
handle_char('?');
handle_char(c);
break;
}
} }
va_end(arg); va_end(arg);
return 0;
} }
#endif #endif
@ -439,10 +423,12 @@ void put_string(const char *s)
and run. if strings are wrong, the program will wait for the ANYKEY and run. if strings are wrong, the program will wait for the ANYKEY
*/ */
#include <conio.h> #include <stdio.h>
#include <string.h>
void cso(char c) void cso(char c)
{ {
putch(c); putchar(c);
} }
struct { struct {
@ -483,7 +469,7 @@ struct {
{ {
"1 ", "%-4x", 1, 0}, "1 ", "%-4x", 1, 0},
{ {
"1000", "%-04x", 1, 0}, "1 ", "%-04x", 1, 0},
{ {
"1", "%ld", 1, 0}, "1", "%ld", 1, 0},
{ {
@ -503,7 +489,7 @@ struct {
{ {
"1 ", "%-4lx", 1, 0}, "1 ", "%-4lx", 1, 0},
{ {
"1000", "%-04lx", 1, 0}, "1 ", "%-04lx", 1, 0},
{ {
"-2147483648", "%ld", 0, 0x8000}, "-2147483648", "%ld", 0, 0x8000},
{ {
@ -515,9 +501,9 @@ struct {
{ {
"32767", "%ld", 0x7fff, 0}, "32767", "%ld", 0x7fff, 0},
{ {
"ptr 1234:5678", "ptr %p", 0x5678, 0x1234}, 0}; "ptr 1234:5678", "ptr %p", 0x5678, 0x1234}, {0}};
test(char *should, char *format, unsigned lowint, unsigned highint) void test(char *should, char *format, unsigned lowint, unsigned highint)
{ {
char b[100]; char b[100];
@ -527,12 +513,12 @@ test(char *should, char *format, unsigned lowint, unsigned highint)
if (strcmp(b, should)) if (strcmp(b, should))
{ {
printf("\nhit the ANYKEY\n"); printf("\nhit ENTER\n");
getch(); getchar();
} }
} }
main() int main(void)
{ {
int i; int i;
printf("hello world\n"); printf("hello world\n");
@ -542,6 +528,7 @@ main()
test(testarray[i].should, testarray[i].format, testarray[i].lowint, test(testarray[i].should, testarray[i].format, testarray[i].lowint,
testarray[i].highint); testarray[i].highint);
} }
return 0;
} }
#endif #endif