flat assembler
Message board for the users of flat assembler.

 Index > Windows > How to convert Integer to a String? Goto page Previous  1, 2, 3
Author
pearlz

Joined: 07 Jun 2010
Posts: 55
Location: Viet Nam
pearlz 17 Oct 2010, 05:39
you can use api wsprintf
like:
szTextOut rb 100
number dd
invoke wsprintf,strTextOut,"number =%d",dword[number]
17 Oct 2010, 05:39
shoorick

Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 18 Oct 2010, 05:16
cinvoke, as wsprintf is c-call function (or you may get problems with stack)
18 Oct 2010, 05:16
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 20 Oct 2010, 10:18
Can someone give me some site where all that are explained well ? cause I don't understand logic why to divide on 2, 10 or 16 to display output as binary/ascii/hex type. Tried to google but can't find any good tutorial about that.. thanks and sorry for dumb questions.
20 Oct 2010, 10:18
avcaballero

Joined: 02 Feb 2004
Posts: 203
avcaballero 20 Oct 2010, 10:58
They are the digits number in its numerical system, its basis. All number are composed by lineal combinations of its basis power.

For example, 5013 decimal:

5013 = 5*10^3 + 0*10^2 + 1*10^1 + 3*10^0 = 5000 + 0 + 10 +3

In order to express a number into another numerical system, you must expres it as lineal combinations of its new basis power.

Numbers in memory are not ascii, what is really displayed. In order to display a number (one digit) you must do: " or 30h" it to convert it to ascii. You can probe it with this tricky:

cmd -> holding alt key, press into the number keypad 49

49d = 31h. You will se a "1" into the screen.

Are we agree?
Is this what you mean?
cheers
20 Oct 2010, 10:58
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 20 Oct 2010, 13:06
avcaballero,

You've explained how number is composed from digits, not the inverse.

----8<----
Overflowz,

Let's assume that ecx==10 and convert eax==123==1×10²+2×10+3×1==(1×10+2)×10+3 to its decimal ASCII representation.

As a first step, eax is decomposed into 12×10+3 by div ecx instruction (eax==12; edx==3), then remainder in edx is converted to corresponding ASCII value for digit 3, namely 33h (add edx, '0' or or edx, '0'). The result of our conversion is actually dl (or dx if you want to be wide, or edx itself if you're bold enough ), now store it somewhere.

Second step takes eax (which was set to 12 by div) and splits it again (1×10+2). Now add edx, '0' yields '2', another ASCII digit for the result. Store it appropriately (while more significant bytes in multi-byte values have greater addresses, for string representing decimal number it's quite contrary).

Third step breaks down eax again, now as 0×10+1. '1' is stored, then this step is considered as final due to eax==0.

P.S. For rigorists (as I am): xor edx, edx is assumed before each div ecx.
20 Oct 2010, 13:06
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 20 Oct 2010, 19:55
avcaballero, I guess that short time ago ty for reply.
baldr, you wrote very nice tutorial but I have so many questions about that.. topic will be bombed if i'll ask.. but can you write with source code and very very basic comments ? Cause I tried thing like that with my mind how I imagined that.. here's code:
Code:
```format PE GUI
include 'WIN32AX.INC'
entry main
buffer rb 20
szLib db 'kernel32.dll',0
szFnc db 'Sleep',0
smsg db 'Hello World!',0
fmsg dd 10
proc main
mov [fmsg],eax
mov ecx,10
mov edi,[fmsg]
div ecx
mov dword[buffer],edi
invoke MessageBox,0,buffer,buffer,MB_OK
;invoke MessageBox,0,msg,msg,MB_OK
invoke ExitProcess,0
endp
library user32,'user32.dll',kernel32,'kernel32.dll'
include 'API\USER32.INC'
include 'API\KERNEL32.INC'
section '.reloc' fixups data discardable    ```

just guessing..
20 Oct 2010, 19:55
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 21 Oct 2010, 05:57
Overflowz,

Here you go:
Code:
```        include "Win32AX.Inc"
.code
mov     ecx, 10         ; we're converting to decimal
mov     edi, _bufend    ; edi points to NUL terminator, buffer is filled backwards
.repeat
xor     edx, edx
div     ecx           ; split eax==10·A+B into eax==A and edx==B
add     dl, '0'       ; convert dl to corresponding ASCII
dec     edi           ; pre-decrement pointer
mov     [edi], dl     ; store ASCII code
.until  eax=0
invoke  MessageBox, HWND_DESKTOP, edi, NULL, MB_OK; edi points to first (most significant) digit
ret

.data
_kernel32 db    "kernel32.dll", 0
_Sleep  db      "Sleep", 0
_buffer db      10 dup ' '; this buffer will hold the result
_bufend db      0

.end    here    ```
Can I ask why you're including extended header and not using its features? I mean that section '.idata' import stuff in your sources.
21 Oct 2010, 05:57
shoorick

Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 21 Oct 2010, 06:27
it is hard to learn everything at once. a newbie needs a "frame" - minimal application which is able to run, has input and output, and the place in the source where to put own code. when there is no good tutorial/explanations/etc. newbie tries to get first met source and modifies it in own way to get a result. sometime he uses constructions without understanding their sense - it is normal, even if isn't good. to reduce newbie puzzling we need to give him examples or easy to understand at whole, or where "supporting frame" is separated from tested code. from other side the newbie by himself has to look into as many examples as he can, and select most simple and clear for him to use it as such kind of "frame". this may increase "learning efficiency"
21 Oct 2010, 06:27
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 21 Oct 2010, 09:15
baldr, very useful source with comments thank you very much. and I dont understand 1 thing, when trying to 'div ecx' its dividing eax and edx same time or some of them ? like only eax/ecx or edx/ecx.. and btw about headers.. I dont know much about section '.xxx' things and writing where I saw someone's src and everytime I'm doing that..

shoorick, you're right. I can't find any tutorials and I am not so skilled to learn things like that in 1 day.. I started learning assembly on 1 sep I think. I'm also teenager I'm not adult. my brain is not too big to understand things like that and I'm also learning at school etc etc..
21 Oct 2010, 09:15
shoorick

Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 21 Oct 2010, 09:41
if you do not understand how does certain command work, you must just read about it in intel manuals or somewhere else similar. yes, a 64 bit number in edx/eax pair is divided on 32 bit number in specified register when "div" command executed.

of course, i would wish to have a brain like teenagers have it absorbs everything like sponge - just "feed" it correctly
21 Oct 2010, 09:41
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 21 Oct 2010, 10:17
Ahh I guess now I think.. for example
EDX has value 12
EAX has value 34
ECX has value 10
when div ecx command executed its like 1234/10 am i right ?
21 Oct 2010, 10:17
shoorick

Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 21 Oct 2010, 10:31
very close, but you have to know some details:
if you mean 12 and 34 are hex values, then edx/eax pair will contain 1200000034h. if you mean 12 and 34 as decimal, then edx/eax pair will contain 12*(2^32)+34 decimal number
21 Oct 2010, 10:31
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 25 Oct 2010, 20:12
I'm kinda stuck about moving.. I'm close to do this function! here's my code and hope someone will fix THANK YOU!
Code:
```format PE GUI 4.0
include 'WIN32AX.INC'
entry main
buffer db 50
proc main
mov ecx,10
mov eax,123
.here:
xor edx,edx
div ecx
mov [buffer],dl
dec [buffer]
test eax,eax
jnz .here
invoke MessageBox,0,buffer,buffer,MB_OK
invoke ExitProcess,0
endp
library user32,'user32.dll',kernel32,'kernel32.dll'
include 'API\USER32.INC'
include 'API\KERNEL32.INC'
section '.reloc' fixups data discardable    ```
25 Oct 2010, 20:12
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 25 Oct 2010, 21:38
almost.. but what difference between my post and baldr's ? here's mine code:
Code:
```format PE GUI 4.0
include 'WIN32AX.INC'
entry main
buffer db 0
_Kernel32 db "kernel32.dll",0
_Sleep    db "Sleep",0
proc main
mov ecx,10
mov edi,buffer
.here:
xor edx,edx
div ecx
dec edi
mov [edi],dl
test eax,eax
jnz .here
invoke MessageBox,0,edi,0,MB_OK
invoke ExitProcess,0
endp
library user32,'user32.dll',kernel32,'kernel32.dll'
include 'API\USER32.INC'
include 'API\KERNEL32.INC'
section '.reloc' fixups data discardable    ```
25 Oct 2010, 21:38
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 26 Oct 2010, 10:17
I got it! Fixed now. I didn't imagined that after buffer will be buffend with null-terminated string and when decreasing buffend its going on buffer.. fixed and I know now what logic are here and thank you all!
26 Oct 2010, 10:17
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 26 Oct 2010, 12:57
I just wrote my first piece of MMX, perhaps you could give me some pointers

Code:
```int2hex32: ; >eax <hexbuffer
push    eax
pxor    mm4,mm4
movd    mm0,eax
punpcklbw mm0,mm4
movq    mm1,mm0
psllw   mm0,12
psrlw   mm1,4
psrlw   mm0,12
packuswb mm0,mm4
packuswb mm1,mm4
punpcklbw mm0,mm1
movq    mm2,mm0
pcmpgtb mm2,[hexcmp]
pand    mm2,[hexalpha]
movd    eax,mm0
bswap   eax
mov     dword [hexbuffer+4],eax
psrlq   mm0,32
movd    eax,mm0
bswap   eax
mov     dword [hexbuffer],eax
emms
pop     eax
retn

align 16
hexcmp          dq      \$0909090909090909
hexalpha        dq      \$2727272727272727

hexbuffer       rq      1
```

_________________
This is a block of text that can be added to posts you make.
26 Oct 2010, 12:57
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page Previous  1, 2, 3

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum