Printing variables value

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
morphycs

Printing variables value

Post by morphycs »

I have those functions to print on screen:
void puts(unsigned char *text);
void putch(unsigned char c);

puts() sends each char to putch().
When I do something like:
int x = 5;
putch(x);

It doesn't print the value of x, it print only text if:
putch('x');
puts("string");
Kemp

Re:Prints variables value

Post by Kemp »

You appear to be confused as to both what those functions do and also what text is. putch is meant for printing characters, if you want to print a number you need a function designed to do that. Passing a number to putch will be the same as passing a character to it as they are both (within the constraints of the language) stored the same, think of 'x' as a convenient shortcut to the ASCII value of x.
morphycs

Re:Printing variables value

Post by morphycs »

I wrote this function that checks the ASCII of digits (0 to 9) but it didn't work.

void putch(unsigned char c)
{
if(c >= 0x30 && c <= 0x39)
{
    where = textmemptr + (csr_y * 80 + csr_x);

*where = c | att; /* Character AND attributes: color */

csr_x++;
}
}
User avatar
ces_mohab
Member
Member
Posts: 77
Joined: Wed Oct 18, 2006 3:08 am

Re:Printing variables value

Post by ces_mohab »

to print a digit x simply use

Code: Select all

putch('0' + x) ;
put num prints an integer
will loop through all digits -according to format- and print each digit.
while

Code: Select all

void putch(unsigned char c)
should print any character so why restrict it to digits
To write an OS you need 2 minds one for coding and other for debugging.
Habbit

Re:Printing variables value

Post by Habbit »

That damned auto-casting...
What you need is an itoa function, i.e. one that creates a string of characters from any number (for OOP thinkers that would be something like int.toString()). This one is a bloated example taken from a C++ tutorial:

Code: Select all

char* itoa(uint32_t number, int radix)   // Accepts radices 2 to 32
{
   const char* values = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
   char temp[32];   // Max length: 32 binary digits
   char* output = new char[33]; // + \0
   int length = 0;
   if (radix >= 2 && radix <= 32)
   {
      if (number == 0)
      {
         output[0] = '0';
         length++;
      }
      // Divide by radix and select a symbol from values. The remainder is
      // the value index. This will make the number reversed (1234 -> 4321)
      while(number > 0)
      {
         temp[length] = values[number % radix];
         number /= radix;
         length++;
      }
      // Now, put the number straight (was reversed)
      for(int i = 0; i<length; i++)
         output[i] = temp[length - (i + 1)];
      // Null-terminate the string
      output[length] = '\0';
   }
   return output;
}
Satan
Posts: 4
Joined: Fri Nov 10, 2006 11:25 am

Post by Satan »

Actually I don't understand a thing of it. I mean, i tried the putch ('0' + x); thing, and I didn't work for me. Can somebody give a full code of this? I found others example's but I don't understand them either.

Thanks in advantage
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Would you mind telling WHAT you dont understand about it?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Satan
Posts: 4
Joined: Fri Nov 10, 2006 11:25 am

Post by Satan »

Well my function of printing a character is something like

video = 0xB8000 + ((scr_y * 160) + (scr_x * 2));
*video = *string;
video++;
*video = col;

I tried the '0' + var on that, didn't work (got some strange char)
In the code give before they work with a some sort of attribute thing

*where = c | att; /* Character AND attributes: color */

I really don't have a clue why there is a "| att". I'm actually just a beginner. So please forgive me if it's a stupid question.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I tried the '0' + var on that, didn't work (got some strange char)
In the code give before they work with a some sort of attribute thing
First of all, var should be between 0 and 9 inclusive. Any other value will indeed produce garbage
Well my function of printing a character is something like

video = 0xB8000 + ((scr_y * 160) + (scr_x * 2));
*video = *string;
video++;
*video = col;
Please, include prototypes - I think some types are changed.
a character is not a character array, so why you are working with a string is way beyond me.
*where = c | att; /* Character AND attributes: color */
Probably, if the buffer is considered an array of short ints, this makes perfectly sense. Each index would then both contain the character and its attributes. c would be the character (the lower 8 bits), and att would be the attributes ( should be the higher 8 bits, like 0x0700, or color << 8 )

You may also want to freshen up on your pointer math and binary operations.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Satan
Posts: 4
Joined: Fri Nov 10, 2006 11:25 am

Post by Satan »

well string is where the char is saved. so only 1 char. The X value is betwheen 0 and 9.

the prototype for the print function is:

void schrijven (char *string, int col)

so what the | means is like I use 2 addresses, video and video++ pointer this will automaticly write 2 bytes? Maybe I'm just gissing, so I probably should go reading some manuals.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

void schrijven (char *string, int col)
This is a prototype for a string function. not a character function. Like:
schrijven("Hello World!", 7);

still, i am missing the prototype for video: it should normally be
char * video = (char *) 0xB8000;
so please, do include all your code, not snippets of it.

That includes, I dont see how you try to feed a char to a char * function.
so what the | means is like I use 2 addresses
the logical OR does not imply the amount of bytes. The amount of bytes being accessed at one time depends on the type of the where pointer.

if you use char * video, you could access video mem as follows:
video[160*y+2*x] = character;
video[160*y+2*x+1] = color;
if its: short int * video
video[x+80*y] = character | color << 8;

If this confuses you even slightly, go back to the C tutorials and master pointer math before you return to hardware programming. (maybe reading "how to ask questions" would help as well)

p.s. seeing dutch and english mixed together gives me the shivers :shock:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Satan
Posts: 4
Joined: Fri Nov 10, 2006 11:25 am

Post by Satan »

ok, sorry fo that. The function I posted was an old one, and not wokring as I tested. Sorry. The function that is working now is ==>

void schrijven (char *string, int col) {
char *video;
//
while (*string != 0) {
begin:
switch (*string) {
case '\n': {
scr_y++;
scr_x = 0;
string++;
goto begin;
}

case '\b': {
if (strlen(hostname) == scr_x) {
string++;
goto begin;
}
scr_x--;
video = 0xB8000 + ((scr_y * 160) + (scr_x * 2));
*video = ' ';
video++;
*video = 0x00;
string++;
goto begin;
}

case '\0': {
break;
}

default: {
video = 0xB8000 + ((scr_y * 160) + (scr_x * 2));
*video = *string;
video++;
*video = col;
scr_x++;
string++;
goto begin;
}
}
}
gotoxy (scr_x, scr_y);
}

The dutch-englisch problem will be fixed. Didn't mention when I started coding that english people should read it.
Post Reply