flat assembler
Message board for the users of flat assembler.

Index > Windows > calling msvcrt`s functions

Author
Thread Post new topic Reply to topic
rambo



Joined: 28 Feb 2005
Posts: 22
Location: posen, poland
rambo
i`ve got problem: i would like to use some functions from msvcrt.dll, but if i want call some of them, program fails. source will say more than words. just compile that example and run under debuger:

Code:

format PE console
entry start

include "win32a.inc"

section ".code" code readable writeable executable

       string  db "where is my fault?",0

start:   enter   0,0

     push    string
      call    printf

  leave
       ret

section ".idata" import data readable writeable

library msvcrt,"msvcrt.dll"

import msvcrt,\
   printf,"printf"

    


any ideas, how i could use any function from msvcrt?
Post 03 Mar 2005, 07:19
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
Welcom to the forum.

You don't need to use the enter & leave statements:
Code:
format PE CONSOLE 4.0
entry start

Include '%fasminc%\win32a.inc'

section ".code" code readable writeable executable 

        string  db "Hello world :)",0 

start: 
        cinvoke printf,string
        invoke ExitProcess,0

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll',\
          msvcrt,'msvcrt.dll'

  import kernel32,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf'
    


All this code equivalent to ( Ollydbg view ):
Code:
PUSH sample.00401000
CALL DWORD PTR DS:[<&msvcrt.printf>]
ADD ESP,4
PUSH 0
CALL DWORD PTR DS:[<&kernel32.ExitProces>
    


Notice that after C function calls, you need to balance the stack manually if you don't use the cinvoke macro.

_________________
Code it... That's all...
Post 03 Mar 2005, 08:11
View user's profile Send private message Visit poster's website Reply with quote
rambo



Joined: 28 Feb 2005
Posts: 22
Location: posen, poland
rambo
thank you for fast reply. it works. but now i`ve got another problem. i won`t create new topic, so:

Code:
fld dword [ebp-0x2c]
    


how to assume ebp register to a stack segment, beacause that instruction is not decoded as i want..?

and why fasm includes "wait" instruction after "fcomx", "fstsw"..? i know, how exactly my code should look, so how can i disable this "advantage".. Smile
Post 03 Mar 2005, 08:34
View user's profile Send private message Visit poster's website Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 604
Location: Germany
MCD
There is no assume directive in Fasm (for good reasons), you should rather use the virtual directive. See Flat Assembler Programmer's manual for details. Here's an example which will assemble to the same code as you wanted it:
Code:
virtual at ebp-0x2C
 some_variable_name  dd some_fp_value
end virtual

;... maybe some code in between

fld some_variable_name
    


You can use the "fn" FPU-instructions, e.g. "fnstsw" instead of "fstsw" which could mean "Floating point No wait ...". If this is to unconventional for you, you can use equates to change the opcodes for those FPU instructions:
Code:
fstsw equ fnstsw
finit equ fninit
fclex equ fnclex
;...
    
Post 03 Mar 2005, 09:35
View user's profile Send private message Reply with quote
rambo



Joined: 28 Feb 2005
Posts: 22
Location: posen, poland
rambo
mcd :: thank you.
Post 03 Mar 2005, 13:22
View user's profile Send private message Visit poster's website 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 YouTube, Twitter.

Website powered by rwasa.