3

In my C++ / C project I want to set the stack pointer equal to the base pointer... Intuitively I would use something like this:

asm volatile(
    "movl %%ebp %%esp"
);

However, when I execute this, I get this error message:

Error: bad register name `%%ebp %%esp'

I use gcc / g++ version 4.9.1 compiler.

I dont know whether I need to set specific g++ or gcc flag though... There should be a way to manipulate the esp and ebp registers but I just don't know the right way to do it.

Doe anybody know how to manipulate these two registers in c++? Maybe I should do it with hexed OP codes?

JFMR
  • 23,265
  • 4
  • 52
  • 76
Aksim Elnik
  • 425
  • 6
  • 27
  • 7
    a comma between `%%ebp` and `%%esp` is missing – JFMR May 22 '17 at 10:00
  • 1
    You probably want `movl %%ebp, %%esp`. But don't do that unless you create a "naked" function, as your code will conflict with the stack frame setup which is already done automagically by the compiler. – Daniel Kamil Kozar May 22 '17 at 10:00
  • 6
    Also, in simple asm, you don't need to double `%`. What you are trying to do is a very bad idea though and sounds like an XY problem. – Jester May 22 '17 at 10:01
  • @AdrianoRepetti C++, but it also applies to C since inline assembler syntax is the same there... I guess... @Neroku You are right , but i still get " Error: bad register name `%%ebp' " @DanielKamilKozar What is a naked function? Currently I am creating a C++ Interceptor agent for my java application... – Aksim Elnik May 22 '17 at 10:02
  • @Jester I know its a bit hacky, but I do not see any other better solution... [link](http://stackoverflow.com/questions/43976035/c-forward-function-call) [link](http://stackoverflow.com/questions/43089692/jni-intercepting-native-methods-outputs) – Aksim Elnik May 22 '17 at 10:05
  • @AksimElnik Don't _hack_ please! If you don't see a _better solution_ there's probably a problem with your overall design. – πάντα ῥεῖ May 22 '17 at 10:21
  • 3
    why's this downvoted? is there an automatism that 'C' tagged Questions start with a -3 malus or something? – Tommylee2k May 22 '17 at 10:38
  • @Tommylee2k man this question had -10 in the beginning hahah))) – Aksim Elnik May 22 '17 at 10:57
  • _"C++, but it also applies to C since inline assembler syntax is the same there..."_ Not a sufficient reason to language-tag-spam. Tag the language you're actually using, and nothing else. – Lightness Races in Orbit May 22 '17 at 11:14
  • @DanielKamilKozar :When _GCC_ has an x86 target the `naked` function attribute isn't available. – Michael Petch May 22 '17 at 12:20

1 Answers1

5

You're using GNU C Basic Asm syntax (no input/output/clobber constraints), so % is not special and therefore, it shouldn't be escaped.

It's only in Extended Asm (with constraints) that % needs to be escaped to end up with a single % in front of hard-coded register names in the compiler's asm output (as required in AT&T syntax).

You also have to separate the operands with a comma:

asm volatile(
    "movl %ebp, %esp"
);

asm statements with no output operands are implicitly volatile, but it doesn't hurt to write an explicit volatile.

Note, however, that putting this statement inside a function will likely interfere with the way the compiler handles the stack frame.

JFMR
  • 23,265
  • 4
  • 52
  • 76
  • Why do I need double '%' in front of eax or ebx , but only single in front of ebp and esp??? And yeah, thank you, it worked :) – Aksim Elnik May 22 '17 at 10:08
  • @AksimElnik I think you only need the double `%` if a colon (`:`) comes after the assembly string – JFMR May 22 '17 at 10:10
  • Ahh, makes sence... Thank you – Aksim Elnik May 22 '17 at 10:10
  • Your inline assembly takes no parameters, therefore there is no reason to escape the registers (because there are no `%1`, `%2`, ...). – JFMR May 22 '17 at 10:12
  • @AksimElnik: GNU C basic (no operands) vs. extended (with operand) asm has other differences. For example, Basic asm statements are implicitly `volatile` (but so are Extended asm statements with no output operands). https://gcc.gnu.org/onlinedocs/gcc/Using-Assembly-Language-with-C.html – Peter Cordes Aug 26 '17 at 01:41
  • @眠りネロク: you should probably say that putting this statement inside a function will not be useful, and will just step on the compiler's toes (as [Jester pointed out](https://stackoverflow.com/questions/44109992/c-set-stack-pointer#comment75239309_44109992).) I already expanded on your answer to explain why, but adding that too would be going too far in putting words in your mouth, I think. Especially since the question is asking "how to manipulate these two registers in C++". – Peter Cordes Aug 26 '17 at 01:50