flat assembler
Message board for the users of flat assembler.

Index > Windows > Correct way to convert binary to decimal in windows?

Author
Thread Post new topic Reply to topic
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 03 Oct 2008, 23:45
Is wsprintf with %u the right way to convert to decimal? Here is some code I wrote with it:

Code:
include "win32ax.inc"

.data
  test1 db "Length: ",0
  out1 rb 100
  windir rb 100

.code
  start:
        invoke GetWindowsDirectory,windir,100
        invoke wsprintf,out1,"%u",eax
        invoke lstrcat,test1,out1
        invoke MessageBox,NULL,windir,test1,MB_OK
        invoke ExitProcess,0
.end start           
    


I'm new to windows programming, I've only done dos programming.

_________________
----> * <---- My star, won HERE
Post 03 Oct 2008, 23:45
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 04 Oct 2008, 00:04
It is not wrong but you could save yourself from doing the string concatenation by just doing it this way
Code:
include "win32ax.inc"
PATH_SIZE = 256

.data
  fmt db "Length: %u", 0
  buff rb $ - fmt + 10
  windir rb PATH_SIZE

.code
  start:
        invoke GetWindowsDirectory, windir, PATH_SIZE ; The documentation says that it should be MAX_PATH
        cinvoke wsprintf, buff, fmt, eax
        invoke MessageBox, NULL, windir, buff, MB_OK
        invoke ExitProcess, 0
.end start    


[edit]It was something wrong, wsprintf must be called with cinvoke or "ccall [wsprintf], ..."
Post 04 Oct 2008, 00:04
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 06 Oct 2008, 00:35
What is cinvoke and why does it need to be used instead of invoke?
Does this work on all windows versions 95 and up?
Thanks

_________________
----> * <---- My star, won HERE
Post 06 Oct 2008, 00:35
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 06 Oct 2008, 00:49
Well, you have to read http://flatassembler.net/docs.php?article=win32 , however it is not explained with an example what cinvoke/ccall does so here the same call without macros
Code:
push eax
push fmt
push buff
call [wsprintf]
add  esp, 4*3 ; Here the important part that invoke/stdcall don't have    


Because wsprintf uses cdecl calling convention instead of stdcall, you need cinvoke.

According to MSDN the minimum operating systems are Windows 95 and Windows NT 3.1.

BTW, this is not "the way" to convert to decimal, it is just one way but nothing stops you from doing your own routine (like you did it in DOS?). But it is very handy and many times the preferable way. I think it is better to use a function already loaded in memory than wasting more RAM writing your own for few to nothing speed improvement (which if it is used to show something on the display then it is very stupid to optimize for speed rather than for size).

PS: I know that importing wsprintf takes more space than a simple IntToStr, but when you need to produce a string with many values in it, the whole code dedicated to that will be bigger than just importing the function.
Post 06 Oct 2008, 00:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 06 Oct 2008, 00:50
wsprintf (wsprintfA & wsprintfW) is the only Win32 API that uses CDECL calling convention so the caller must restore the stack. All other Win32 APIs use STDCALL convention so the stack is restored the the function called.


Last edited by revolution on 06 Oct 2008, 01:22; edited 1 time in total
Post 06 Oct 2008, 00:50
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 06 Oct 2008, 01:17
Ok, thanks for explaining that to me.
Post 06 Oct 2008, 01:17
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.