flat assembler
Message board for the users of flat assembler.

Index > Main > saving regs in func havin local data without using macros.

Author
Thread Post new topic Reply to topic
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1
Suppose the following uses ebx, esi registers.
Then when should I push them to save.
Should I push the registers before:
sub esp, 4
or after:
sub esp, 4
Code:
myfunc:
 tmp equ ebp-4
        enter 0, 0
; push esi ebx here ...
       sub esp, 4
; or here ... ?
       leave
       ret
    

_________________
Code:
 o__=-
 )
(\
 /\  
    


Last edited by 0.1 on 06 Nov 2007, 04:46; edited 1 time in total
Post 05 Nov 2007, 14:32
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
in your case, second one, because if you pushed at first one, then "ebp-4" would point to saved registers.

But there is also option of saving them before enter, and that has advantage that you can exit procedure without unwinding stack, like this:
Code:
func:
label .tmp dword at ebp-4
push ebx ecx
push ebp
mov ebp, esp
sub esp, 4
...
push eax
sub esp, 10   
jmp .r    ;we can exit procedure properly even with things left on stack
...
.r:
mov esp, ebp
pop ebp
pop ecx ebx
retn
    


I remember this way had some disadvantage too, but i can't recall what it was now.
Post 05 Nov 2007, 15:19
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3035
Location: vpcmipstrm
bitRAKE
Very Happy
Code:
strLEN:
 or ecx, -1
  xor eax, eax
        pop edx                 ; return address
    xchg edi, [esp]
     repne scasb
 pop edi
     not ecx
     dec ecx
     jmp edx    
There are so many ways - it all depends on what your goals are.
Post 05 Nov 2007, 18:29
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
This particular way is unconvinient, Pentium processors and newer has a special buffer to predict where the RET instruction will go, but if the CALL is not paired with one RET then the next RET will be mispredicted due to the unbalance.

Besides that, it is a good example Smile
Post 05 Nov 2007, 19:22
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3035
Location: vpcmipstrm
bitRAKE
Very true. If speed is the goal then ESP should only be updated twice within a PROC - once on entry and once on exit. This small routine should only be used where speed is not a concern. Also, it might be good to mention that XCHG with a memory operand induces a bus lock!

Often a routine has ESP dependancies at the beginning because of frame or register preservation. Fastcall convention helps a little here, but often it is difficult to avoid all together. The tail code is usually easier to remove ESP dependancies on - reduced to just RET #, or establishing a return value in EAX along with changes to ESP.
Post 05 Nov 2007, 19:57
View user's profile Send private message Visit poster's website Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1
Thanks guyz!
So dumb is me!
But hey I am only 0.1, lot of bugs still remain!

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 06 Nov 2007, 04:48
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.