Writing assembly in C ++ without using other variables

I am writing a simple C ++ program that contains some asm instructions.

void call(){
    __asm__("mov    -0x10(%rbp),%eax;"
            "add    $0x10,%eax;"
            "mov    %eax,%edx;"
            "shr    $0x1f,%edx;"
            "add    %edx,%eax;"
            "sar    %eax;"
            "add    %eax,-0x4(%rbp);"
            "mov    $0x4c,%eax;"
            "mov    -0x8(%rbp),%eax;"
            "add    %eax,%eax;"
            "sub    -0x4(%rbp),%eax;"
            "add    %eax,-0xc(%rbp);");
}

However, from the execution behavior, I realized that the registers controlled by this asm object are actually used by other variables in the function.

Is there a way to call the compiler to highlight the registers used in the asm tag and make sure they are not running?

OS: Linux Compiler: g ++

Non-compiler approaches are also welcome.

+6
source share
3 answers

, , pusha/popa, .

:

void callOne(){
    __asm__(
        "pusha;"
        "mov    -0x10(%rbp),%eax;"
        "add    $0x10,%eax;"
        "mov    %eax,%edx;"
        "shr    $0x1f,%edx;"
        "add    %edx,%eax;"
        "sar    %eax;"
        "add    %eax,-0x4(%rbp);"
        "mov    $0x4c,%eax;"
        "mov    -0x8(%rbp),%eax;"
        "add    %eax,%eax;"
        "sub    -0x4(%rbp),%eax;"
        "add    %eax,-0xc(%rbp);"
        "popa;");
}
+2

, - , , , , copmiled code, / . . , . - C/++, asm , asm.

+1

asm() , , "" . , asm(), , , .

- GCC Extended ASM ( asm()), (, ...) , , , , , , ASM, - " " , ..

OSDev Wiki GCC, ( ) a href= "http://wiki.osdev.org/Inline_Assembly/Examples" rel= "nofollow noreferrer" > examples.


Microsoft has its own flavor of Inline Assembly (and, quite typically, it doesn't play very well with asm(), but insists on __asm(), and really also insists that you save / restore any registers manually.

+1
source

Source: https://habr.com/ru/post/1015721/


All Articles