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
Printing integer
Printing integer
Sorry for my bad English...
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
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
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English )
Derrick operating system: http://derrick.xf.cz (Slovak and English )
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);
};
};
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);
};
};
- AndrewAPrice
- Member
- Posts: 2309
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Why have you always replied in English and randomly decided not to now?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
My OS is Perception.
- Kevin McGuire
- Member
- Posts: 843
- Joined: Tue Nov 09, 2004 12:00 am
- Location: United States
- Contact:
RE: value to hex
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];
}
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.
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.
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.
Sorry, I really forgot to add the English translation:MessiahAndrw wrote:Why have you always replied in English and randomly decided not to now?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
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
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English )
Derrick operating system: http://derrick.xf.cz (Slovak and English )
- AndrewAPrice
- Member
- Posts: 2309
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
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.
My OS is Perception.