Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

number to text

number to text

number to text

how do I convert a number to text?
i.e  the number 1000 to "1000"

RE: number to text

In which assembler?

RE: number to text

(1) presumeably you need to output your number in decimal? If so, you need first to find a way to get individual digits out of it. The obvious way is to divide by 10 keeping the remainder (if in x86 processor, look at the div instruction). Also keep the divided number, because when you divide that by 10 again, it will give you the next digit.
(2) Unfortunately this will give you the digits in the reverse of the correct order... check it on paper. You will need to reverse the digits. Have a little think about how to do that and reply again if you get stuck.
(3) Now you need to output each digit as text. That means converting each digit to ascii. Write out a little conversion table on paper of 0 to "0" ascii etc... up to 9. It will become obvious how to make the conversion.
(4) You will have to have a think about your operating system for how to output an ascii. If in dos, have a look at the functions of int 021h.

RE: number to text

Thanks, I figured it out last night.

divide the number by 10
add 48 to the remainder to convert to ascii
store result in an array
repeat above with quotient, storing result in array in descending elements

btw, I'm using masm32.

RE: number to text

Absolutely! Array is nice because it's flexible about length. Some people prefer to push the digits onto the stack as they're calculated, and then pop them back off to print.
Obviously when you've divided your number down to zero then you can stop, and that way you won't even get leading zeros.

One day someone will explain to me how to print out a decimal from a binary floating point....

RE: number to text

I've figured out this much on Floating-Point to Decimal:

The most significant bit (bit31 for single precision or bit 63 for double precision) is the sign.  0 is +, 1 is -.

For single precision (32 bit) the next 8 bits (bit30 - bit23) are the exponent.  For double precision (64 bit) the next eleven bits (bit 62 - bit 52) are the exponent.

In single precision, a bias of 127 is added to the exponent.  In double, a bias of 1023 is added.

The mantissa or significand are the remaining bits.  Since the first digit of the number is always one, it is not explicity stored in the floating point format.

Subtract the bias from the exponent and convert into decimal.

If the exponent is positive, the integer portion of the number is 1 (always there though not specified) followed by the bits from the most significant bit of the mantissa ((bit 22 for single precision, bit 51 for double) to msb-exponent+1.
i.e. if the floating point number is stored as
01000011100110011001100110011001 (32 bit)
the sign is +,
the biased exponent is 10000111,
and the mantissa is 00110011001100110011001

The exponent becomes 10000111 - 01111111 = 1000 base2 or 8 base10.

The integer part of the mantissa is 100110011 (notice the 1 in the msb position followed by the first 8 digits of the mantissa).
Convert this to decimal to get 307.

For the fractional part you take the remaining bits of the mantissa and multiply that value by 2^exponent.
001100110011001 * 2^8 = 00110011001101 * 100000000 = 110011001100100000000 base2 or 1677568 base10.

The final result then is  307.1677568

RE: number to text

Thanks both! Much appreciated...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close