/* CONVERT TO AEMS C99 - BEN YATES 5/29/02 */ /* formatted i/o functions ** ** (c) 1986, 1987 by Clint Pulley, based on Unix C ** ** Last Edit 87/04/21 2140 ** ** Uses global (static) working storage */ extern putchar(); entry printf,atoi,itoa,Aprnf; #define NULL 0 /* #include dsk1.conio */ /* ** printf(ctlstr,arg1,arg2,...); Formatted print ** ** performs as described in Kernighan & Ritchie ** ** c,d,o,s,u and x are supported */ printf(arg) char *arg; { int argb; #asm CLR *10 argb=0; *14 MOV *14+,3 *11+,8 CI 3,>05CA INCT 10 8,>05CE INCT14 JNE PRN#1 INCT *10 argb=2; *14 JMP PRN#2 PRN#1 CI 3,>022A AI 14,n 8,>022E JNE PRN#2 MOV *14,*10 argb=n; *11,*14 PRN#2 DECT *10 *14 #endasm Aprnf(prfput,&arg+argb); } /* character dispatcher for printf */ prfput(c) int c; { return(putchar(c)); } /* ** Aprnf(putsub,argptr) ** Called by printf() & co. */ int arg, left, pad, len, maxchr, sw, width; char *ctl, *sptr, str[18]; /**/ Aprnf(putsub,nxtarg) int *putsub, *nxtarg; { ctl = *nxtarg--; while(*ctl) { if(*ctl!='%') {putsub(*ctl++); continue;} else ++ctl; if(*ctl=='%') {putsub(*ctl++); continue;} if(*ctl=='-') {left = 1; ++ctl;} else left = 0; if(*ctl=='0') pad = '0'; else pad = ' '; if(isdigit(*ctl)) { width = atoi(ctl++); while(isdigit(*ctl)) ++ctl; } else width = 0; if(*ctl=='.') { maxchr = atoi(++ctl); while(isdigit(*ctl)) ++ctl; } else maxchr = 0; arg = *nxtarg--; sptr = str; switch(*ctl++) { case 'c' : *str = arg; str[1] = NULL; break; case 's' : sptr = arg; break; case 'd' : itoa(arg,str); break; case 'o' : itoneb(arg,str,8); break; case 'u' : itoneb(arg,str,10); break; case 'x' : itoneb(arg,str,16); break; default : return ; } len = strlen(sptr); if(maxchr && maxchrlen) width = width - len; else width = 0; if(!left) while(width--) putsub(pad); while(len--) putsub(*sptr++); if(left) while(width--) putsub(' '); } return; } /* ** return true if c is a decimal digit */ isdigit(c) int c; { return(c<='9'&c>='0'); } /* ** return length of s */ char *t; /**/ strlen(s) char *s; { t=s-1; while(*++t); return(t-s); } /* ** n=atoi(s) - convert string to integer */ int sign,n; /**/ atoi(s) char *s; { while(*s==' ')++s; sign=1; if(*s=='-') { sign=-1; ++s; } if(*s=='+') ++s; n=0; while(isdigit(*s)) n=10 * n + *(s++) - '0'; return(sign*n); } /* ** convert signed n to characters in s */ itoa(n,s) int n; char *s; { if(n<0) { *s++='-'; n=-n; } itoneb(n,s,10); } /* ** convert unsigned n to chars in s using base b ** ** itoneb(n,s,b) int n,b; char *s; */ #asm ITONEB DATA C$WORK,$+2 MOV *10,7 BASE @2(10)14->10 MOV @4(10),2 N @6 14->10 LI 6,BUF# WORK BUFFER CLR 5 DIGIT COUNTER ITONB1 CLR 1 DIV 7,1 N/BASE, REM IN 2 AI 2,48 MAKE CHAR CI 2,58 JL ITONB2 IF REM<2 AI 2,7 ITONB2 SWPB 2 MOVB 2,*6+ TO BUF INC 5 COUNT MOV 1,2 QUOTIENT JNE ITONB1 IF MORE DIGITS MOV @2(10),7 S @4(14->10) ITONB3 DEC 6 STORE IN REVERSE MOVB *6,*7+ DEC 5 JNE ITONB3 IF MORE SB *7,*7 ZERO BYTE RTWP B *13 * BUF# BSS 8 #endasm