gcc/g++ and ebp

Programming, for all ages and all languages.
Post Reply
bluecode

gcc/g++ and ebp

Post by bluecode »

hi,

as I have changed my message passing system (see the other thread), I now can only store three parameters (12byte) per message. This is not very much and now I want to use the last cpu register possible, namely ebp. But I don't know if this is a good idea in combination with C/C++ (gcc/g++), because normaly a copy of the original esp is stored in ebp. Ok, I know want to know, if I'm supposed to save ebp on the stack or if gcc/g++ will do that automatically when I tell it, that my inline assembly will return/need a specific value in ebp.
Now the more important question: How can I use ebp in the input/output list of the inline assembly. I mean is there something like "a", "b", "c", "d", "S", "D" for ebp? I could not find anything on the inet.
JoeKayzA

Re:gcc/g++ and ebp

Post by JoeKayzA »

I don't think that gcc lets you add esp to the input/output list since it doesn't like the programmer mess with it anyway. :P

When you really need it, you should address it directly (by using %%esp with at&t syntax). If you need to set/get the value of esp within an inline asm statement, you should go indirectly over another register (and do a 'movl %0, %%esp', for example)

cheers Joe
bluecode

Re:gcc/g++ and ebp

Post by bluecode »

JoeKayzA wrote: I don't think that gcc lets you add esp to the input/output list since it doesn't like the programmer mess with it anyway. :P
You misunderstood me. I want to use ebp, not esp. Using esp would screw up everything obviously 8)
JoeKayzA

Re:gcc/g++ and ebp

Post by JoeKayzA »

:o sorry, I should really think about getting glasses..... :D

But I think the issues are the same with ebp - it's used internally for creating frame pointers (unless you specify -fomit-frame-pointers)
bluecode

Re:gcc/g++ and ebp

Post by bluecode »

Yes, sure, but notheless my question is, if it is possible to use ebp in the inline assembly input/output register list. If so, how. Then I could test if gcc/g++ is pushing/poping ebp in order to save its value. If so, I will use it. If not I woun't.

btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it ;)
Ryu

Re:gcc/g++ and ebp

Post by Ryu »

I don't recommend using EBP mixing it with c/c++ code, its better if you intend to use the register to do this entirely in assembly. If inline assembly is desirable, it should be possible only if you don't mix c/c++ code within the same function, but require that gcc allows you to create a "naked" function (one without an prologue and epilogue). A typical generated c/c++ prologue/epilogue would create/restore a stack frame looking something like this:

Code: Select all

push ebp        ; create stack frame 
mov ebp, esp   ; of the prologue
...
      ;[ebp+04h]  return address
      ;[ebp+08h]  3rd pushed parameter
      ;[ebp+0Ch] 2nd pushed parameter
      ;[ebp+10h] 1st pushed parameter
...
mov esp, ebp   ; restore stack frame
pop ebp         ; of the epilogue
Now if you knock out EBP, this means you need to keep track the stack to find your parameters from current value of ESP. Also, you wouldn't be able to refer the parameter by its gcc namespace (basically plain ole assembly we got here).
JoeKayzA

Re:gcc/g++ and ebp

Post by JoeKayzA »

bluecode wrote: Yes, sure, but notheless my question is, if it is possible to use ebp in the inline assembly input/output register list. If so, how. Then I could test if gcc/g++ is pushing/poping ebp in order to save its value. If so, I will use it. If not I woun't.
What I meant was, (as Ryu stated above) ebp is used by the compiler internally and is not meant to be dealt with by the programmer. Therefore gcc (AFAIK) does not allow to put ebp into the input/output list. The only way is to go indirectly over another register, or even better, use a seperate pure asm routine.
bluecode wrote: btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it ;)
Indeed, thanks! 8)

cheers Joe
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:gcc/g++ and ebp

Post by Candy »

JoeKayzA wrote:
bluecode wrote: btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it ;)
Indeed, thanks! 8)

cheers Joe
How do you tell people have a german keyboard...


You can use EBP in inline assembly. If all's ok with GCC (and I guess/expect it is) it'll either save automatically (just like with eax etc) or not at all (if you use -fomit-frame-pointer).

Code: Select all

int test(int input) {
   int retval;
   asm("mov %2, %1":"=a"(retval):"B"(input));
   return retval;
}
Should work and generate a pointless function.
JoeKayzA

Re:gcc/g++ and ebp

Post by JoeKayzA »

Candy wrote:
JoeKayzA wrote:
bluecode wrote: btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it ;)
Indeed, thanks! 8)

cheers Joe
How do you tell people have a german keyboard...
??? ... ok, that one took me some time, but I got it. ;D
Candy wrote: You can use EBP in inline assembly. If all's ok with GCC (and I guess/expect it is) it'll either save automatically (just like with eax etc) or not at all (if you use -fomit-frame-pointer).

Code: Select all

int test(int input) {
   int retval;
   asm("mov %2, %1":"=a"(retval):"B"(input));
   return retval;
}
Should work and generate a pointless function.
Interesting, I didn't know that either. :-[

cheers Joe
bluecode

Re:gcc/g++ and ebp

Post by bluecode »

Candy wrote:
JoeKayzA wrote:
bluecode wrote: btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it ;)
Indeed, thanks! 8)

cheers Joe
How do you tell people have a german keyboard...
ROFLMAO ;D

Big thanks @ all for the information :)
earlz

Re:gcc/g++ and ebp

Post by earlz »

Quote from: Candy on 04-Jul-06, 11:39AM
Quote from: JoeKayzA on 03-Jul-06, 02:50PM
Quote from: bluecode on 03-Jul-06, 10:27AM
btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it

Indeed, thanks!

cheers Joe


How do you tell people have a german keyboard...
I still don't get it???

doesn't the inline operator thingy make the function behave like a macro and not use the stack
like this:

Code: Select all

inline unsigned int blah(unsigned int blah2){
//look! no stack
}
JoeKayzA

Re:gcc/g++ and ebp

Post by JoeKayzA »

When you call an inline function, the compiler won't allocate a new stack frame (AFAIK), but there _is_ still a stack in use, you can reference the stack pointer as well. Yes, inline functions behave a bit like macros...

But i can't see where inlining of functions was mentioned in this discussion - it was all about inline assembly statements (AFAICS).

cheers Joe
earlz

Re:gcc/g++ and ebp

Post by earlz »

lol misread sorry about that
bluecode

Re:gcc/g++ and ebp

Post by bluecode »

Jordan3 wrote:
Quote from: Candy on 04-Jul-06, 11:39AM
Quote from: JoeKayzA on 03-Jul-06, 02:50PM
Quote from: bluecode on 03-Jul-06, 10:27AM
btw. if you're using firefox, then press Strg + '+' to increase the font size and Strg + '-' to decrease it

Indeed, thanks!

cheers Joe


How do you tell people have a german keyboard...
I still don't get it???
Strg is an abreviation for the german word "Steuerung", which is "control" in english. So on an english keyboard (yes, this forum is english ;) ) it would be Ctrl.
bluecode

Re:gcc/g++ and ebp

Post by bluecode »

Candy wrote:

Code: Select all

int test(int input) {
   int retval;
   asm("mov %2, %1":"=a"(retval):"B"(input));
   return retval;
}
This is not working for me (gcc 3.3.6). With which version did you test it?
Post Reply