Page 1 of 1
Printing integer
Posted: Sat Mar 10, 2007 1:44 pm
by Cmaranec
Hello,
how can i print integer? If i call standart unsigned char 'puts' function, it prints unknown char or nothing. I am begginer.
(this OS is in C and ASM)
Please help
Posted: Sat Mar 10, 2007 1:50 pm
by inflater
Dosaď do EAX hodnotu integeru, ktorého chceš zobraziť. Ak má céčko vkladaný assembler, tak do video ramky dosaď to číslo a je to
Aký je to dátový typ? Štandardný integer alebo long, comp, atď. ?
//EDIT 2: Toto určite pomôže:
http://src.opensolaris.org/source/xref/ ... dio/putw.c
Len vynechaj tie procedúrky FILE atď. a máš to
samozrejme, potrebuješ procedúru PUTC.
inflater
Posted: Sat Mar 10, 2007 1:58 pm
by muisei
You have to split the integer into digits and then print each digit.
I'll give you an example:
/**
* Print unsigned decimal
* @param dec The number to print
*/
void print_udec(u_int dec)
{
u_char digits[10];
char i;
if(dec==0)
putc('0');
else
{
for(i=9;i>=0;i--)
{
digits='0'+dec%10;
dec/=10;
};
for(i=0;i<10;i++)
if(digits=='0')digits=0;
else break;
for(i=0;i<=9;i++)
if(digits!=0)putc(digits);
};
};
Posted: Sat Mar 10, 2007 4:06 pm
by AndrewAPrice
inflater wrote:Dosaď do EAX hodnotu integeru, ktorého chceš zobraziť. Ak má céčko vkladaný assembler, tak do video ramky dosaď to číslo a je to
Aký je to dátový typ? Štandardný integer alebo long, comp, atď. ?
//EDIT 2: Toto určite pomôže:
http://src.opensolaris.org/source/xref/ ... dio/putw.c
Len vynechaj tie procedúrky FILE atď. a máš to
samozrejme, potrebuješ procedúru PUTC.
inflater
Why have you always replied in English and randomly decided not to now?
RE: value to hex
Posted: Sat Mar 10, 2007 5:07 pm
by Kevin McGuire
Code: Select all
unsigned char cvtb[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned char *utoa(unsigned char *buffer, unsigned int value){
buffer[0] = cvtb[(value>>28)&0xF];
buffer[1] = cvtb[(value>>24)&0xF];
buffer[2] = cvtb[(value>>20)&0xF];
buffer[3] = cvtb[(value>>16)&0xF];
buffer[4] = cvtb[(value>>12)&0xF];
buffer[5] = cvtb[(value>>8)&0xF];
buffer[6] = cvtb[(value>>4)&0xF];
buffer[7] = cvtb[(value)&0xF];
return &buffer[8];
}
This works well, for hex output.
I know you should look this up, since it is so easy.. but maybe if it did not take someone so long to learn everything about writing a kernel. We might get more written, and maybe some interesting ones.
Posted: Sun Mar 11, 2007 1:37 am
by XCHG
I am not a C programmer but I can tell you how it is done, generally:
You have to take the number (Suppose it is 32-bits long). Now you have to decide whether you want to print it as a signed or an unsigned number. The below steps are for unsigned numbers and then I will tell you how you can do that with signed numbers:
1) Take the 32-bit number and divide it by 10.
2) Hold the remainder and recursively, call your function/procedure again until your number is 0x00000000 or zero.
3) Extract the remainders in reverse (For having written a recursive function/procedure) and convert them to their corresponding characters (OR 0x00000030).
4) Put these characters in your string and perhaps terminate your string with a null terminator (0x00).
If you have a signed number, in which Bit #31 is set, subtract 1 from your number and then reverse all the bits with NOT or in C I guess it is ~.
Therefore, if you want to print a signed number:
1) Decrement the value once (Subtract 1 from it).
2) Reverse all the value's bits (NOT/~).
3) Print a "-" character to the string to denote a signed number.
4) Now print the number as an UNsigned number onto the screen.
HTH.
thx
Posted: Sun Mar 11, 2007 2:46 am
by Cmaranec
Thanks to all
Posted: Sun Mar 11, 2007 4:09 am
by inflater
MessiahAndrw wrote:inflater wrote:Dosaď do EAX hodnotu integeru, ktorého chceš zobraziť. Ak má céčko vkladaný assembler, tak do video ramky dosaď to číslo a je to
Aký je to dátový typ? Štandardný integer alebo long, comp, atď. ?
//EDIT 2: Toto určite pomôže:
http://src.opensolaris.org/source/xref/ ... dio/putw.c
Len vynechaj tie procedúrky FILE atď. a máš to
samozrejme, potrebuješ procedúru PUTC.
inflater
Why have you always replied in English and randomly decided not to now?
Sorry, I really forgot to add the English translation:
You can visit here:
http://src.opensolaris.org/source/xref/ ... dio/putw.c
There is a source code to procedure PUTW. You need to skip the FILE
declarations to work properly in your OS.
P.S.: I forgot to add EN translation because Cmaranec knows Slovak language and i was, well, tired from my all day work...
inflater
Posted: Sun Mar 11, 2007 5:07 am
by AndrewAPrice
oh okay.. I was only asking.. I know there's no written rule about posting in English, but it seems like the language everyone on this forum can understand.