The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: nocturncal on February 21, 2007, 11:49:15 AM

Title: Converting from an int to ascii
Post by: 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? 
Title: Re: Converting from an int to ascii
Post by: dsouza123 on February 21, 2007, 01:27:43 PM
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.
Title: Re: Converting from an int to ascii
Post by: TomRiddle on February 21, 2007, 05:24:48 PM
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
Title: Re: Converting from an int to ascii
Post by: P1 on February 21, 2007, 06:51:00 PM
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)
Title: Re: Converting from an int to ascii
Post by: dsouza123 on February 21, 2007, 07:40:21 PM
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]
Title: Re: Converting from an int to ascii
Post by: TomRiddle on February 21, 2007, 08:10:51 PM
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]