Printing variables value
Printing variables value
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");
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");
Re:Prints variables value
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.
Re:Printing variables value
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++;
}
}
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++;
}
}
Re:Printing variables value
to print a digit x simply use
put num prints an integer
will loop through all digits -according to format- and print each digit.
while
should print any character so why restrict it to digits
Code: Select all
putch('0' + x) ;
will loop through all digits -according to format- and print each digit.
while
Code: Select all
void putch(unsigned char c)
To write an OS you need 2 minds one for coding and other for debugging.
Re:Printing variables value
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:
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;
}
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.
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.
- Combuster
- 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:
First of all, var should be between 0 and 9 inclusive. Any other value will indeed produce garbageI 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
Please, include prototypes - I think some types are changed.Well my function of printing a character is something like
video = 0xB8000 + ((scr_y * 160) + (scr_x * 2));
*video = *string;
video++;
*video = col;
a character is not a character array, so why you are working with a string is way beyond me.
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 )*where = c | att; /* Character AND attributes: color */
You may also want to freshen up on your pointer math and binary operations.
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.
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.
- Combuster
- 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:
This is a prototype for a string function. not a character function. Like:void schrijven (char *string, int col)
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.
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.so what the | means is like I use 2 addresses
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
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.
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.