flat assembler
Message board for the users of flat assembler.

Index > Main > trivial question

Author
Thread Post new topic Reply to topic
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 10 Dec 2003, 10:51
what's a difference between return and ret retn retf?
Very Happy nice day

harry

_________________
Microsoft: brings power of yesterday to computers of today.
Post 10 Dec 2003, 10:51
View user's profile Send private message Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv 10 Dec 2003, 12:25
return is a marco in fasm if I am not wrong. Intel specifies ret to be both retn and retf though retn and retf does not exist in Intel's manual. retn is return near which is a return to a call from a calling procedure within the current code segment, while retf is return far which is a return to a call from a calling procedure located in a different segment from the current segment.
Post 10 Dec 2003, 12:25
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 10 Dec 2003, 12:53
Like Petzold said :there is no near and far addresses in 32bit modes...
so what's a difference???


Code:


macro return                              ; return from procedure
 { leave
   ret ..ret }

    


what does exactly 'return' do? and why return instead of ret ?

_________________
Microsoft: brings power of yesterday to computers of today.
Post 10 Dec 2003, 12:53
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 10 Dec 2003, 13:51
Return is a macro that together with proc macro permits structured procedures that uses the stack.
Example: This procedure is going to use 2 dword arguments and 2 dword locals from stack:
Code:
proc Name,Arg1,Arg2
    .local1 dd ?
    .local2 dd ?
    begin
    mov eax,[.local1]
    mov ebx,[Arg2]
    return
endp

; is assembled as ...

00401000  C8 080000  enter   8,0
00401004  8B45 F8    mov     eax,[ebp-8]
00401007  8B5D 0C    mov     ebx,[ebp+C]
0040100A  C9         leave
0040100B  C2 0800    retn    8    

LEAVE OPCODE C9 -> Set ESP to EBP, then pop EBP
Description: Releases the stack frame set up by an earlier ENTER instruction. The LEAVE instruction copies the frame pointer (in the EBP register) into the stack pointer register (ESP), which releases the stack space allocated to the stack frame. The old frame pointer (the frame pointer for the calling procedure that was saved by the ENTER instruction) is then popped from the stack into the EBP register, restoring the calling procedure?s stack frame.
A RET instruction is commonly executed following a LEAVE instruction to return program control to the calling procedure.

See "Procedure Calls for Block-Structured Languages" in Chapter 6 of the Intel® Architecture Software Developer's Manual, Volume 1, for detailed information on the use of the ENTER and LEAVE instructions.
Post 10 Dec 2003, 13:51
View user's profile Send private message Yahoo Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 10 Dec 2003, 14:01
Hi.

At firtst about retn/retf differences:

retn - (return from near call) loads IP (or EIP) from the stack.
retf - (return from far call) loads from the stack not only IP (EIP) but also CS:

All this is because near call changes only IP (EIP), but far call changes IP(EIP) and CS simultaneously.

ret instructions allow one imediate parameter that should be added to ESP after return address is extracted from the stack.

Here is some pseudo code describing retn/retf action:
Code:
retn imm
    pop    eip
    add    esp, imm

retf imm
    pop    eip
    pop    cs
    add    imm
    


BTW: Using of retf in Windows is very limited. You have to use it only in DOS and if you want to write your own OS.

You know that procedure arguments in stdcall calling convention should be pushed in the stack and procedure use them via [ebp+offset]. So after finishing procedure should remove these arguments from the stack - this is a purpose of imm constant in ret instruction. It simply moves esp to point below arguments. This imm constant should be as great as the number of bytes passed as arguments to the procedure.

return macro is "inteligent" return instruction for use with "proc" macro. Proc macro calculate the size of arguments and return macro use this number as imm argument of retn instruction. It simply frees the programmer from counting how many bytes he use as procedure arguments.

Regards.
Post 10 Dec 2003, 14:01
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 10 Dec 2003, 14:57
now I know(thank You) what for is the return macro but still have some lack of understanding
,why there is the code like this:

Code:

proc WindowProc, hwnd,wmsg,wparam,lparam
        enter
        push    ebx esi edi; look at this  Smile Smile Smile :p
        cmp     [wmsg],WM_CREATE
        je      wmcreate
        cmp     [wmsg],WM_SIZE
        je      wmsize
        cmp     [wmsg],WM_SETFOCUS
        je      wmsetfocus
        cmp     [wmsg],WM_COMMAND
        je      wmcommand
        cmp     [wmsg],WM_DESTROY
        je      wmdestroy
  defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     finish  
    

Code:

 finish:
        pop     edi esi ebx ; and this Smile Smile Smile ;p
        return   
    

why someone push and pop the registers ?

_________________
Microsoft: brings power of yesterday to computers of today.
Post 10 Dec 2003, 14:57
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 10 Dec 2003, 15:35
WindowProc procedure is called in the most cases from Windows. So, Windows needs these registers to be preserved, because it uses them for it's internal needs. If you never change esi, edi or ebx you don't need to preserve them in the stack. But in the most cases it is hard to track whole big WindowProc whether some message handler uses or not these registers, so the most programmers simply push them on the begin ant pop them on the end of WindowProc. It is important to preserve registers only in the code that will be called from Windows. For the code that only your program call yoo may preserve/not preserve the registers you want/need.

Regards.
Post 10 Dec 2003, 15:35
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle 10 Dec 2003, 21:22
Now I am fully satisfied and thx you all for explanation.
I very thank you JohnFound.

this is my curse that I am very nosy Wink and always want to know all the details I'm interested in...


Once more THX!,

harry

_________________
Microsoft: brings power of yesterday to computers of today.
Post 10 Dec 2003, 21:22
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.