Writing a print-function that can print variables.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Espen

Writing a print-function that can print variables.

Post by Espen »

I'm having a problem writing a print-function that can print variables in C.

Something like:

Code: Select all

int i = 50;
print(I);
The only thing i have now i a function that writes literal text
like:

Code: Select all

print("Hello");
I hope any of you have any good ideas for me
:)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Writing a print-function that can print variables.

Post by Pype.Clicker »

get a bit further on page 2 of the board ...
http://www.mega-tokyo.com/forum/index.p ... eadid=7462

should help.
Espen

Re:Writing a print-function that can print variables.

Post by Espen »

Iv`e now created a neat function, the only problem is that it writes to many zeros in the start

Code: Select all

void printint(unsigned int i)
{
         putch((char)(((i / 1000000000) %10) + '0'));
         putch((char)(((i / 100000000) %10) + '0'));
         putch((char)(((i / 10000000) %10) + '0'));
         putch((char)(((i / 1000000) %10) + '0'));
         putch((char)(((i / 100000) %10) + '0'));
         putch((char)(((i / 10000) %10) + '0'));
         putch((char)(((i / 1000) %10) + '0'));
         putch((char)(((i / 100) %10) + '0'));
         putch((char)(((i / 10) %10) + '0'));
         putch((char)(((i / 1) %10) + '0'));
         csr_y++;
}

putch prints on char onto the screen.
My problem now is when i is 50, 00000000050 is the output.
Red Shaya

Re:Writing a print-function that can print variables.

Post by Red Shaya »

A cool fix to your problem would be something like this pseodo code:

Code: Select all

int divider = 1000000000
int digit = 0
bool printFlag = False

while divider > 0 do
  digit  = (i/divider) % 10
  if digit > 0
     PrintFlag = True
  endIf
  if PrintFlag then
     putch((char) digit)
  endIf
  divider = divider / 10
endWhile
Crazed123

Re:Writing a print-function that can print variables.

Post by Crazed123 »

That'll work OK for decimal, but what about hex? Or do the ASCII values for A, B, C, D, E and F come straight after the one for '9'? I can't remember.
Just Another Guest

Re:Writing a print-function that can print variables.

Post by Just Another Guest »

No they don't (... 8 9 : ; < = > ? ...). Use a conversion array.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Writing a print-function that can print variables.

Post by Pype.Clicker »

Crazed123 wrote: That'll work OK for decimal, but what about hex? Or do the ASCII values for A, B, C, D, E and F come straight after the one for '9'? I can't remember.
For hex display, it's even simpler:
1. because you often want it to be fixed-width, so you don't have to do a first pass counting how many digits there will be (e.g. you usually prefer 0x0102 or 0x00000102 rather than 0x102 for pointers, etc)
2. because division per 16 is only >>4 and remainder is &0xf

The trick here is to have a lookup array char hexdigits[]="0123456789ABCDEF" (something i found in start32 code by Tran/Renaissance somewhere around '97)
Red Shaya

Re:Writing a print-function that can print variables.

Post by Red Shaya »

Look at the PrintReg16 function in the following code to get a simple and good example of using a lookup array.
dh

Re:Writing a print-function that can print variables.

Post by dh »

Don't see much of crazybudda now-a-day :'(

Cheers, DH.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Writing a print-function that can print variables.

Post by Candy »

Might I suggest +7 or +39?

In ASCII, UTF-8 and Unicode (yes, them all) the numbers go from 0x30 to 0x39 and the characters from 0x41-0x46 or 0x61-0x66, depending on whether you want upper case or lower case (respectively). So,

Code: Select all

char * tohexprint(unsigned int x) {
  char buffer[9];
  int i;
  for (i=0; i<8; i++) {
    int asc = ((x & (15 << (4*i))) >> (4*i)) + 0x30;
    if (asc > 0x39) asc += 0x7; // or 0x27
    buffer[7-i] = asc;
  }
  return buffer;
}
This is of course excellent for mmx optimization, although I don't consider it worth your trouble. For exercise however, it's good.
Chris Giese

Re:Writing a print-function that can print variables.

Post by Chris Giese »

Code: Select all

void print_unsigned_long(unsigned long val, unsigned radix) {
   char buf [33], *s = buf + sizeof(buf);
   unsigned remainder;

/* store terminating nul character in buf */
   s--;
   *s = '\0';
/* repeatedly divide 'val' by 'radix' until 'val' == 0 */
   do {
/* ...it's the remainder we really want... */
      remainder = (unsigned)(val % radix);
      val = val / radix;
/* convert binary remainder to ASCII digit */
      remainder += '0';
      if(remainder > '9')
         remainder += ('A' - ('9' + 1));
/* store ASCII digits RIGHT-TO-LEFT in buf */
      s--;
      *s = remainder;
   } while(val != 0);
/* et voil?, display numeric string */
   puts(s); }
Some questions to ask yourself about this code:
  • What are the legal values for 'radix'?
  • Why is 'buf' 33 bytes long? Will this buffer ever overflow?
  • What would happen if we used while() { } instead of do { } while() ?
Support for printing signed numbers, fixed field widths,
pad-left-with-zeroes, etc. is left as an exercise...
B.E

Re:Writing a print-function that can print variables.

Post by B.E »

he is another example of printing a integer in hex string

Code: Select all

char* tohexprint(unsigned int x) 
{
   char* buffer = new char[8]; //Buffer to put the string
   buffer[8] = 0;             //Terminate the string
  char _Hex[17] = "0123456789ABCDEF";//Hex string
  _asm
  {
     mov ecx,8        //cycle though 8 times
     mov edi,[buffer] //edi -> points to buffer
     add edi,8          //edi -> points to end of buffer
     mov eax,[x]      //eax -> integer to convert
     lea edx,_Hex      //edx -> points to hex string
NextDig:
      push eax         //save eax
     and eax,0Fh      //last 4 bits of eax
     mov al,byte ptr [edx+eax]//al -> _Hex[eax]
     mov byte ptr[edi],al   //put char into buffer
     pop eax         //restore eax
     shr eax,4       //get now bits
     dec edi         //goto next char of buffer
     loop NextDig    //do it all again 8 times
     
  }
  return buffer;
}
Post Reply