内联汇编程序导致另一个函数内部冻结

mic*_*874 0 c++ assembly fpu

我注意到,使用我的内联汇编代码要么非常慢,要么停止与我的C++代码相比很快完成.当我在一个不同的函数中调用内联汇编程序而不是让函数被调用的汇编程序时,我很好奇为什么会发生这种情况.我测试了两种方法,发现我的程序在省略该功能时没有冻结.

    __asm {

    push dword ptr[rw] //rw is a C++ floating-point variable
    fld[esp] // Using the stack as temporary storage in order to insert it into the FPU
    add esp, 4 //preserving the memory

    push dword ptr[lwB]
    fld[esp]
    add esp, 4

    fsubp ST(1), ST(0) // Subtracting rw - lwB

    push dword ptr[sp]
    fld[esp]
    add esp, 4

    fdivp ST(1), ST(0) // Dividing previous resultant by span -> (rw - lwB) / sp

    push dword ptr[dimen]
    fld[esp]
    add esp, 4

    fmulp ST(1), ST(0) // Multiplying previous resultant by dimension > ((rw - lwB) / (sp)* dimen)

    sub esp, 4 // Allocating space in order to save result temporarily to ram then to eax then to a C++ variable
    fstp[esp]
    pop eax 
    mov fCord, eax

    }
    return (int)fCord; //fCord is also a floating-point C++ variable
Run Code Online (Sandbox Code Playgroud)

更快的C++代码:

    return (int)(((rw - lwB) / (sp)* dimen));
Run Code Online (Sandbox Code Playgroud)

小智 5

今天的编译器更加先进,与手工编写的程序集相比,可以进行分支预测,减少内存操作等.这并不意味着手工编码的程序集总是很糟糕,但是对于大多数情况,编译器在配置正确的标志时可以做同样好或更好的优化工作.在您的情况下,您已经使用了大量的堆栈操作,并且每个都导致内存加载/存储,这在CPU周期方面是昂贵的.这可能是性能下降的原因.在发布模式下编译时,请参阅c ++实现的disassmbly代码,以便将手动编码的程序集与编译器生成的输出进行比较.