flat assembler
Message board for the users of flat assembler.

Index > Main > Problem with function

Author
Thread Post new topic Reply to topic
Cthulhu



Joined: 12 May 2005
Posts: 29
Cthulhu 26 Feb 2008, 11:29
I'm having problems creating functions with fasm it seems it has to do with calling coventions but I can't figure out what is wrong Embarassed
Here is the code:

Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.code' code readable executable

;---------------------------------------------------------------
proc Dummy a1, a2, a3
   MOV EAX, [a1]
       ADD EAX, [a2]
       ADD EAX, [a3]
       RET
endp     

;---------------------------------------------------------------
start:

  push 1
      push 2
      push 3
      call Dummy
  
    PUSH 0                  ; <<====== Code never returns here
    CALL [ExitProcess]

;=====================================
section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'
  import  kernel, ExitProcess,'ExitProcess'


    


After calling dummy the code never returns to PUSH 0.
What I did wrong?
Post 26 Feb 2008, 11:29
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 26 Feb 2008, 11:35
try "ret" instead "RET"
Post 26 Feb 2008, 11:35
View user's profile Send private message Visit poster's website Reply with quote
Cthulhu



Joined: 12 May 2005
Posts: 29
Cthulhu 26 Feb 2008, 11:38
shoorick wrote:
try "ret" instead "RET"


I could never imagine it was the case of the ret instruction Shocked
Thanks a lot shoorick!
Post 26 Feb 2008, 11:38
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap 26 Feb 2008, 12:23
You should pop the arguments from the stack when the function returns. You can simply write "ret 12" instead of ret (because there are 12 bytes of arguments left on the stack). That's how the stdcall convention handles it. Or you can use C convention where you add manually the number of bytes to ESP after the function returns (add esp, 12). If you have only one argument you can also simply pop it from the stack (in ECX for example which is a register you can freely use), it will be shorter than adding 4 to ESP.

_________________
(French only) http://dap.developpez.com
Post 26 Feb 2008, 12:23
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4071
Location: vpcmpistri
bitRAKE 26 Feb 2008, 15:09
"ret" has been redefined by win32a.inc (actually proc32.inc) to work with "proc" macro. When you use "RET" the macro for ret isn't executed. This macro changes the ret into "ret 12" - based on proc arguments.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 26 Feb 2008, 15:09
View user's profile Send private message Visit poster's website Reply with quote
Cthulhu



Joined: 12 May 2005
Posts: 29
Cthulhu 26 Feb 2008, 15:45
Thanks a lot guys!
Post 26 Feb 2008, 15:45
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.