简体   繁体   English

C ++设置堆栈指针

[英]C++ set stack pointer

In my C++ / C project I want to set the stack pointer equal to the base pointer ... Intuitively I would use something like this: 在我的C ++ / C项目中,我想将堆栈指针设置为等于基指针 ...直观地,我会使用这样的东西:

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. 我使用gcc / g ++版本4.9.1编译器。

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. 我不知道我是否需要设置特定的g ++或gcc标志...应该有一种方法来操作espebp寄存器,但我只是不知道正确的方法来做到这一点。

Doe anybody know how to manipulate these two registers in c++? 有谁知道如何在c ++中操纵这两个寄存器? Maybe I should do it with hexed OP codes? 也许我应该使用hexed OP代码?

You're using GNU C Basic Asm syntax (no input/output/clobber constraints), so % is not special and therefore, it shouldn't be escaped. 您正在使用GNU C Basic Asm语法 (没有输入/输出/ clobber约束),因此%并不特殊,因此不应对其进行转义。

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 ). 只有在扩展的Asm (有约束)中, %需要被转义,最终在编译器的asm输出中的硬编码寄存器名称前面有一个%根据AT&T语法的要求 )。

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 . 没有输出操作数的asm语句是隐式易失性的,但写一个显式的volatile也没什么坏处。

Note, however, that putting this statement inside a function will likely interfere with the way the compiler handles the stack frame . 但请注意,将此语句放在函数中可能会干扰编译器处理堆栈帧的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM