News:

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

Converting from an int to ascii

Started by nocturncal, February 21, 2007, 11:49:15 AM

Previous topic - Next topic

nocturncal

Hello I am programming in x86 assembly and want to print an integer on the screen.  How do I convert an integer to ascii without using calls such as sprintf? 

dsouza123

If you don't want to use calls, you need to write code to do it.

If the desired result is the base10 (decimal) string version of the integer
rather than hexadecimal or binary.

An extremely limited version that only deals with the values 0 to 9:

.data
   num1  dd 7
   buffer db 32 dup (0)

.code
   .if (num1 >= 0) && (num1 <= 9)
      mov eax, num1
      add eax, 30h
      mov dword ptr buffer, eax
   .endif

For positive integers,the normal way way is to
repeat
get the last digit of the number, by getting the result of the number mod 10
store it (converted to ascii)
divide the number by ten
until number is 0

Depending on the storage method the string may have to be reversed.

TomRiddle

Quote from: nocturncal on February 21, 2007, 11:49:15 AM
Hello I am programming in x86 assembly and want to print an integer on the screen.  How do I convert an integer to ascii without using calls such as sprintf? 

Show me your code and I will show you mine :D

P1

Quote from: TomRiddle on February 21, 2007, 05:24:48 PMShow me your code and I will show you mine :D
Humn, do we really want to beleive a politician?   :bdg

Regards,  P1  :8)

dsouza123

A modified version of MichaelW's routine as posted by Vortex.
Link to the original post. Printing decimals.
http://www.masm32.com/board/index.php?topic=163.msg863

This version is Win32 with dwords instead of DOS with words
along with direct character placement.

[attachment deleted by admin]

TomRiddle

I think he has homework, so I will let him figure this one out :P
Only thing I never got working was Signed Decimal Numbers :'(

.Data
Number db 13495
.Data?
String db 10 dup(?)
.Code
ConvertDWordToString32 Number, String, UseHex + UseUpperCase + UseCrLf + UseWindowsEndChar
ConvertDWordToString32 Number, String, UseDec + UseComma + UseWindowsEndChar
ConvertDWordToString32 Number, String, UseBin + UseDosEndChar
ConvertDWordToString32 Number, String, UseOct


Edit: Moved it all into Code.zip

[attachment deleted by admin]