News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Converting any number into ASCII

Started by Stormbreaker, November 30, 2006, 06:50:10 PM

Previous topic - Next topic

Stormbreaker

Hello,

I want to convert a number into ASCII, it's easy with a one-digit-number: Just have to add 30h, but what about two-digit and so on?

Tedd

Divide by 10 :wink

When you do a DIV you get the answer (in eax) and the remainder (in edx).
So what you do is divide the number by 10, and use the value in eax as the last (rightmost) digit.
Then take the remainder (if it's not zero yet) and div by 10 again, to get the next digit.
Keep doing that until the remainder is zero :wink
No snowflake in an avalanche feels responsible.

Vortex

You would like to have a look at the dwtoa function from masm32.lib :

Quotedwtoa

dwtoa proc public uses esi edi dwValue:DWORD, lpBuffer:PTR BYTE

Note that the parameter lpBuffer is an address of DWORD size.

Description
dwtoa convert a DWORD value to an ascii string.

Parameters
   1. dwValue The DWORD value to convert.

   2. lpBuffer The address of the buffer to put the converted DWORD into.

Return Value
There is no return value.

Comments
The buffer for the converted value should be large enough to hold the string including the terminating zero

kermit

Quote from: Tedd on November 30, 2006, 06:58:00 PM
Divide by 10 :wink

When you do a DIV you get the answer (in eax) and the remainder (in edx).
So what you do is divide the number by 10, and use the value in eax as the last (rightmost) digit.
Then take the remainder (if it's not zero yet) and div by 10 again, to get the next digit.
Keep doing that until the remainder is zero :wink


i did an example of this method for 16bit masm, just take a look at the last code example on the page:
http://www.osap.de/asm_tut/asmtut2f.html
it prints out the value thats stored in the ax register...
( page is in german but i hope the code is readable anyway  :P

PBrennick

kermit,
That is nice, but I think Tedd's description of using a 32bit method is better suited for today's type of code.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

zooba

Quote from: Tedd on November 30, 2006, 06:58:00 PM
Divide by 10 :wink

When you do a DIV you get the answer (in eax) and the remainder (in edx).
So what you do is divide the number by 10, and use the value in eax as the last (rightmost) digit.
Then take the remainder (if it's not zero yet) and div by 10 again, to get the next digit.
Keep doing that until the remainder is zero :wink


You've got some of your eax's an edx's around the wrong way. :wink

So what you do is divide the number by 10, and use the remainder in edx as the last (rightmost) digit.
Then take the answer (eax) (if it's not zero yet) and div by 10 again, to get the next digit.
Keep doing that until the answer (eax) is zero

Cheers,

Zooba :U

Tedd

:lol Good point!

(No no, I meant to do it like that! It was an.. umm... exercise for the reader! :green2)
No snowflake in an avalanche feels responsible.