Printing Hex and Decimal Codes on the screen
Printing Hexadecimal Codes on screen is an important feature of an operating system.It is used in dumping registers, printer the pointers, etc.But printing decimal goes to be optional as it might be rare this function being used, but it would be nice to print something with base 10.
The code below is self explanatory, so I have not intended much on explaination.
First let us start with print Hexadecimal, which is important.
-------------------------------------------------------------------------------------------------------------
signed int print_hex(unsigned int hexvalue)
{
signed int hextemp;//A signed variable where the hex will be stored as temp
char zerocount = 1;
int counter; //printstring("Hex:");
printstring(" 0x");//We know that every hex code starts with "0x"
for (counter = 28; counter > 0; counter -= 4)//Loop to keep on printing until all hex code have been printed {
hextemp = (hexvalue >> counter) & 0xF;
if(hextemp == 0 && zerocount != 0)
{
continue;
}
if(hextemp >= 0xA)
{
zerocount = 0;
printstring(hextemp-0xA+'a' );
}
else
{
zerocount = 0;
printstring(hextemp+'0' );
}
}
hextemp = hexvalue & 0xF;
if (hextemp >= 0xA)
{
printsring(hextemp-0xA+'a');
}
else
{
printstring(hextemp+'0'); }
return hextemp ;//Al returns the hex value }
-------------------------------------------------------------------------------------------------------------Printing Decimal Vlaues as simple as 'ABC'
char print_dec(unsigned int value)
{
signed int tmp = value;
int i = 0;
int j=0;
char chra[32];
char chrb[32];
if(value == 0)
{
print('0');
return;
}
while (tmp > 0)
{
chra[i] = '0' + tmp%10;
tmp /= 10;
i++;
}
chra[i] = 0;
chrb[i--] = 0;
while(i >= 0)
{
chrb[i--] = chra[j++];
}
printstring(chrb);
return chrb;
}
The code below is self explanatory, so I have not intended much on explaination.
First let us start with print Hexadecimal, which is important.
-------------------------------------------------------------------------------------------------------------
signed int print_hex(unsigned int hexvalue)
{
signed int hextemp;//A signed variable where the hex will be stored as temp
char zerocount = 1;
int counter; //printstring("Hex:");
printstring(" 0x");//We know that every hex code starts with "0x"
for (counter = 28; counter > 0; counter -= 4)//Loop to keep on printing until all hex code have been printed {
hextemp = (hexvalue >> counter) & 0xF;
if(hextemp == 0 && zerocount != 0)
{
continue;
}
if(hextemp >= 0xA)
{
zerocount = 0;
printstring(hextemp-0xA+'a' );
}
else
{
zerocount = 0;
printstring(hextemp+'0' );
}
}
hextemp = hexvalue & 0xF;
if (hextemp >= 0xA)
{
printsring(hextemp-0xA+'a');
}
else
{
printstring(hextemp+'0'); }
return hextemp ;//Al returns the hex value }
-------------------------------------------------------------------------------------------------------------Printing Decimal Vlaues as simple as 'ABC'
char print_dec(unsigned int value)
{
signed int tmp = value;
int i = 0;
int j=0;
char chra[32];
char chrb[32];
if(value == 0)
{
print('0');
return;
}
while (tmp > 0)
{
chra[i] = '0' + tmp%10;
tmp /= 10;
i++;
}
chra[i] = 0;
chrb[i--] = 0;
while(i >= 0)
{
chrb[i--] = chra[j++];
}
printstring(chrb);
return chrb;
}