Note that there are some explanatory texts on larger screens.

plurals
  1. POLinux C creating custom printf function without header libraries
    primarykey
    data
    text
    <p>I am working on creating my own prinf() for a boot loader I am working on for a class assignment, this means I have to use the BCC compiler and I cannot use system libraries since they do not exist. I do have the ability to use the putc() function designed in assembly and the string library functions strcmp, etc to help me as needed.</p> <p>I seem to have run into a logic issue.</p> <p>If I define this in a test file compiled on Linux (cc):</p> <pre><code>int a = 0; int b = 1; int i1 = 0; int i2 = 1; unsigned long x = 42949672; printf("\nHI! :%d: :%d: :%d: :%d: :%d: :%d:\n", x,a,b,i1,i2,x); </code></pre> <p>I can then run ./a.out and I receive <code>HI! :42949672: :0: :1: :0: :1: :42949672:</code> which is correct.</p> <p>I've created my own printf function and when I see things printed I see <code>HI! :23592: :655: :0: :1: :0: :1:</code>, which is not correct. I've tried printing with integers only and it works fine, but when I try to print the unsigned long, I run into problems.</p> <p>Here is my code:</p> <pre><code>void prints(s) char *s; { int i; for(i=0; i&lt;strlen(s); i++) putc(s[i]); } void gets(s) char *s; { //LEC9.pdf while( (*s=getc()) != '\r') { putc(*s++); } *s = '\0'; } //EXAMPLE CODE char *ctable = "0123456789ABCDEF"; int rpi(x, BASE) unsigned long x; int BASE; { char c; if (x) { c = ctable[x % BASE]; rpi(x / BASE, BASE); putc(c); } return 0; } void printc(ip) unsigned long; { putc(ip); } int printd(ip) unsigned long; { if(ip &lt; 0) { putc('-'); ip = -ip; } if(ip == 0) { putc('0'); return 0; } rpi(ip, 10); } void printx(ip) unsigned long; { prints("0x"); //PUT OR OUTPUT LOOK LIKE INT rpi(ip, 16); } int printl(ip) unsigned long; { if(ip == 0) { putc('0'); return 0; } rpi(ip, 10); putc('L'); } void printf(fmt) char *fmt; { char *cp; //POINTER TO LOOP THROUGH unsigned long *ip; //POINTER FOR cp = fmt; //SET POINTER TO START POINTER {FMT} ip = &amp;fmt+1; //Board says &amp;fmt:but will not work without +1 while(*cp) { //IF C != % if(*cp != '%') { printf("%c", *cp); if(*cp == '\n') { //putc('\n'); //implied putc('\r'); } cp++; continue; //NEXT CHAR } else { //MOVE ONE CHARACTER (%{x}) SO WE CAN GET x cp++; switch(*cp) { case 'c': printc(*ip); break; case 's': prints(*ip); break; case 'd': printd(*ip); break; case 'x': printx(*ip); break; case 'l': printl(*ip); break; default: break; } } cp++; ip++; } } </code></pre> <p>Anyone have any tips as I've been stuck and need some help.</p> <p>EDIT (2:06pm): I've changed all my u16/unsigned short to unsigned long and things have changed to printing <code>HI! :L: :0: :0: :: :: :1:</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload