flat assembler
Message board for the users of flat assembler.

Index > Main > retn and esp

Author
Thread Post new topic Reply to topic
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox 20 Jan 2008, 01:02
why returned value by retn is added to esp?
Post 20 Jan 2008, 01:02
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4623
Location: Argentina
LocoDelAssembly 20 Jan 2008, 01:13
If you think that "retn value" is returning a value you are wrong, values for functions are generally returned in (E)AX register and the operand for ret is to tell the CPU how many bytes the stack pointer should advance.

You can use plain ret and later the caller of the function is responsible for restoring the stack pointer (add esp, args*4), or make the function restore the stack pointer for you (ret args*4).
Post 20 Jan 2008, 01:13
View user's profile Send private message Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox 20 Jan 2008, 01:28
okay, so instead add esp, 64 i use retn -64 ;]

and why its only 2byte long? stack can have more than 1626 argumernts
Post 20 Jan 2008, 01:28
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4623
Location: Argentina
LocoDelAssembly 20 Jan 2008, 01:47
But what function could push soooooo many arguments? Note that printf doesn't count since it can't be stdcall (unless you use some relatively complicated stack handling but if the fmt doesn't match the number of arguments you destroy the stack so is better to use cdecl calling convention where the caller must use the "add esp, xxx" rather than using stdcall convention that is meant just for no-vargars functions).

Quote:

okay, so instead add esp, 64 i use retn -64 ;]

The joke would be "add esp, 64"->"retn -60". I have not good sense of humor maybe but do you realize that are not the same at all no? Wink

I leave some examples
Code:

push 5
push 10
call  stdcall_func
; EAX = 15; no need to adjust the stack

push 3
push eax
call  cdecl_func
; EAX = 18; stack still holds the room for the two args so we must release them (or reuse the space for something else if seems appropiate but we have nothing else to do this time)
add esp, 8
; Now we can use RET since the stack is adjusted and hence ESP points to the return address instead of the first argument of cdecl_func (15 since the function doesn't write the args)
ret 
; Two args funcs
stdcall_func:
mov  eax, [esp+4]
add  eax, [esp+8]
ret 8

cdecl_func:
mov  eax, [esp+4]
add  eax, [esp+8]
ret    
Post 20 Jan 2008, 01:47
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.