flat assembler
Message board for the users of flat assembler.

Index > Main > Wsprintf Problem

Author
Thread Post new topic Reply to topic
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
I'm trying to use wsprintf to write the value of one string into another. When I write the first value into vA and use a MessageBox to display the result it seems to work alright, but then when I check vA after I've written a string into another variable it contains the string I wrote into the second variable.

Can you help me sort this out? Thanks in advance,

Code:
Include 'C:\Program Files\FASM\Include\win32ax.inc'

.code

  Start:

    ;"bob"+"kim"
      invoke wsprintf, Buffer, '%s', s0
      invoke wsprintf, vA, '%s', Buffer
     invoke  MessageBox,HWND_DESKTOP,vA,"vA",MB_OK
    ;"jim"
      invoke wsprintf, Buffer, '%s', s1
      invoke wsprintf, vB, '%s', Buffer
      invoke  MessageBox,HWND_DESKTOP,vA,"vA",MB_OK
    ;End
      invoke ExitProcess

.data

  Buffer: rb 1024
  Buffer2: rb 1024

  vA: db ''
  vB: db ''

  s0: db 'bobkim',0
  s1: db 'jim',0

.end Start
    
Post 01 Apr 2006, 22:23
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
Code:
format PE GUI 4.0
entry start
include '%fasminc%\win32a.inc'
;______________________________________________________________________________
section '.text' code readable executable
start:

        ;"bob"+"kim"
        push    s0
        push    s3
        push    Buffer
        call    [wsprintf]
        
        push    Buffer
        push    s3
        push    vA
        call    [wsprintf]
        
        push    0
        push    s2
        push    vA
        push    0
        call    [MessageBox]
        
        ;"jim"
        push    s1
        push    s3
        push    Buffer
        call    [wsprintf]

        push    Buffer
        push    s3
        push    vB
        call    [wsprintf]

        push    0
        push    s4
        push    vB
        push    0
        call    [MessageBox]
        
        ;End
        push    0
        call    [ExitProcess]

;______________________________________________________________________________
section '.data' data readable writeable

        s0                      db 'bobkim',0
        s1                      db 'jim',0
        s2                      db 'vA',0
        s3                      db '%s',0
        s4                      db 'vB',0

        Buffer                  rb 1024
        vA                      rb 1024
        vB                      rb 1024
        vA                      rb 1024
        vB                      rb 1024

;______________________________________________________________________________
section '.idata' import data readable writeable

library kernel32,'kernel32.dll',\
                user32,'user32.dll'

import  kernel32,\
                ExitProcess,'ExitProcess'

import  user32,\
                wsprintf,'wsprintfA',\
                MessageBox,'MessageBoxA'
    


looks stupid because board uses bigger tab-width...

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 01 Apr 2006, 23:24
View user's profile Send private message MSN Messenger Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
Oh mate! You're a legend! Thanks for this Smile

BTW: You declared vA and vB twice in the data section, I removed the second declaration and it was fine - that was just a mistake right, or am I missing something?
Post 01 Apr 2006, 23:28
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
yep, a mistake...

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 02 Apr 2006, 01:07
View user's profile Send private message MSN Messenger Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
wsprintf not stdcall, it's cdecl

cinvoke wsprintf,szbuffer,szmask,szfigure
Post 02 Apr 2006, 14:27
View user's profile Send private message Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
I don't understand, sorry?
Post 02 Apr 2006, 14:28
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Vasilev Vjacheslav wrote:
wsprintf not stdcall, it's cdecl

cinvoke wsprintf,szbuffer,szmask,szfigure


Killswitch wrote:
I don't understand, sorry?


in a stdcall convention procedure the stack is balanced inside the procedure itself with 'retn #', but in cdecl (the default calling convention of the crt library) you must balance the stack after the call (i think this is probably for VARAGS like wsprintfA?)

so you would do
Code:
push dword 2
push dword 1
call [cdecl_procedure]
add esp, 8 ;8 bytes passed
    


'cinvoke' rather than 'invoke' will do the stack balancing for you, so use cinvoke in this case

_________________
redghost.ca
Post 02 Apr 2006, 23:21
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
RedGhost, thanx for help Smile
Post 03 Apr 2006, 07:07
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.