Printing integer

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
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Printing integer

Post 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
Sorry for my bad English...
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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 :D samozrejme, potrebuješ procedúru PUTC.

inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Post 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);
};

};
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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 :D samozrejme, potrebuješ procedúru PUTC.

inflater
Why have you always replied in English and randomly decided not to now?
My OS is Perception.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

RE: value to hex

Post 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.
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post 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.
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

thx

Post by Cmaranec »

Thanks to all
Sorry for my bad English...
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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 :D 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
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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.
My OS is Perception.
Post Reply