Hi all!

I've added a new feature to my run-time code generator; to automagically allocate variables on the stack. So now I can just write things like:


Int t;

mov(t, dword_ptr [in]);
mul(t, dword_ptr [something]);
mov(dword_ptr [out], t);

Yes, this is C++ code. The allocator sorts out what register(s) can be used. The only reason it also allocates stack space is when there are not sufficient registers and spilling is needed. So every allocated register is associated with a memory location.

Now my actual question: should I use esp or ebp as my stack pointer? Currently I'm using esp, but I see ebp is used very often. Why is that? It works fine now, but I just want it to behave the way people expect it to behave.
Posted on 2004-03-13 06:55:55 by C0D1F1ED
ESP is the stack pointer, of course, but it is often copied into EBP at the start of a function. The reason for this is that the addressing mode is 1 byte longer than the mode. Note that there's no addressing mode. This means that if you have more than 1 stack variable which is used 5 times or more, you should use EBP as a base register.
Another thing to consider is using PUSH and POP wisely.
Posted on 2004-03-13 07:07:23 by Sephiroth3
Using EBP also reduces dependancy on ESP. The down side is obviously the loss of a general purpose register. Often when optimizing for speed, the loss of a register is too great to counter balance the benefits of using EBP.
Posted on 2004-03-13 09:09:25 by bitRAKE
Thanks guys, it makes sense now!
Posted on 2004-03-13 16:48:10 by C0D1F1ED
Why not make a mix of using ebp and esp?

For example, the variables that will be used in all the function/proc use the ebp, and the variables that will be used inside loops and other things use esp?


proc x
local l1:dword
local l2:dword ;the two use ebp
if(...){
local l_l1:dword ;use the esp
}
endproc


Instead of declare all the locals at start..., also ?I see that in this way, you have sepaarted a little more the variables that are really local to the funct/proc, than the ones used for calculations?


Have a nice day or night.
Posted on 2004-03-13 18:08:58 by rea

Why not make a mix of using ebp and esp?

Why would I want to do that? Then I waste two registers, ?nd create longer binaries.
Posted on 2004-03-14 07:56:02 by C0D1F1ED
When you enter in a proc that have stack frame is used the ebp, and if you have locals is used the esp, also you can 'allocate' the espace for the locals (in the start of the proc) and after let the esp be used like a general register, and at the end when you reset the stack frame, you get the esp in the normal use.

If you take a more deep look, at how C have his scope limits about his vrs, you can see that in asm there exist (if you whant) more than the defined in C, for example, there exist locals that the only purpouse is for save temporary a data (push/pop) you can call they local saves, also if you dont have trouble in save a flash value after the esp for example for save a computation, this local (flash value) will be overwrited when you use push, because is after the esp, and you have a little life cycle.


I refer to use boot, for some like this:

(the sintaxis is a gybrid or some like that).



proc x
local x1:dword ;@
local x2:dword ;@
for( i:dword, i<10, i++){
local inside: structX ;#
mov eax, i
.......
}
...
switch (x1){
case 1:{
local c1: unionT ;#
... ;#
local c8: structXdword ;#
}
case 2:
.....
}
endproc



What I refer to use boot is in the case of the local scopes, there not exist the necesity of allocate all the space in the start of the proc, specially if:

1) The total size of all the locals inside is to large, compared to the size of the locals that are really direct to the proc/funct.

2) If the proc will be used for recursing, because you allocate innesesary space for locals (and probably the selections will never reach that part of code), you will lost space at runtime, and the recursive use will cause more of this lost.



The locals marked with @; are of real 'final' use for the proc/funct, but the ones marked with ;# are only for use in this scope, for me make sense use the stack frame and a sub (or add) to the esp for hold the locals that are like ;@ and use a extra sub esp, val (or add) for hold locals of the type ;#.



Have a nice day or night.
Posted on 2004-03-14 10:06:25 by rea