使用内联asm编写一个带有2次比较的for循环
我想将以下代码中的 for 循环转换为汇编语言,但我不知道如何开始。如果能解释一下如何做到这一点以及为什么它有效,我们将不胜感激。
我正在使用VS2010,C++,为x86编写。代码如下:
for (n = 0; norm2 < 4.0 && n < N; ++n)
{
__asm{
///a*a - b*b + x
fld a // a
fmul st(0), st(0) // aa
fld b // b aa
fmul st(0), st(0) // bb aa
fsub // (aa-bb) // st(0) - st(1)
fld x // x (aa-bb)
fadd // (aa-bb+x)
/// 2.0*a*b + y;
fld d // d (aa-bb+x)
fld a // d a (aa-bb+x)
fmul // ad (aa-bb+x)
fld b // b ad (aa-bb+x)
fmul // abd (aa-bb+x)
fld y // y adb (aa-bb+x)
fadd // b:(adb+y) a:(aa-bb+x)
fld st(0) //b b:(adb+y) a:(aa-bb+x)
fmul st(0), st(0) // bb b:(adb+y) a:(aa-bb+x)
fld st(2) // a bb b:(adb+y) a:(aa-bb+x)
fmul st(0), st(0) // aa bb b:(adb+y) a:(aa-bb+x)
fadd // aa+bb b:(adb+y) a:(aa-bb+x)
fstp norm2 // store aa+bb to norm2, st(0) is popped.
fstp b
fstp a
}
}
I want to convert the for loop in the following code into assembly but i am not sure how to start. An explanation of how to do it and why it works would be appreciated.
I am using VS2010, C++, writing for the x86. The code is as follows:
for (n = 0; norm2 < 4.0 && n < N; ++n)
{
__asm{
///a*a - b*b + x
fld a // a
fmul st(0), st(0) // aa
fld b // b aa
fmul st(0), st(0) // bb aa
fsub // (aa-bb) // st(0) - st(1)
fld x // x (aa-bb)
fadd // (aa-bb+x)
/// 2.0*a*b + y;
fld d // d (aa-bb+x)
fld a // d a (aa-bb+x)
fmul // ad (aa-bb+x)
fld b // b ad (aa-bb+x)
fmul // abd (aa-bb+x)
fld y // y adb (aa-bb+x)
fadd // b:(adb+y) a:(aa-bb+x)
fld st(0) //b b:(adb+y) a:(aa-bb+x)
fmul st(0), st(0) // bb b:(adb+y) a:(aa-bb+x)
fld st(2) // a bb b:(adb+y) a:(aa-bb+x)
fmul st(0), st(0) // aa bb b:(adb+y) a:(aa-bb+x)
fadd // aa+bb b:(adb+y) a:(aa-bb+x)
fstp norm2 // store aa+bb to norm2, st(0) is popped.
fstp b
fstp a
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决此类问题的最快、最简单的方法是首先以尽可能简单的形式用 C 或 C++ 编写代码,然后使用 C/C++ 编译器生成 asm。然后,您可以使用此生成的 asm 作为您自己的 asm 代码的模板。使用像 gcc 这样的适当编译器,您可以使用 gcc -S 来执行此操作。我非常确定 Visual Studio 在其 GUI 中的某处埋藏着类似的选项(显然命令行开关是
/Fa
)。The quickest and easiest way to get a running start on this kind of problem is to first write the code in C or C++ in as simple a form as possible, then use your C/C++ compiler to generate asm. You can then use this generated asm as a template for your own asm code. With a proper compiler like gcc you would use
gcc -S
to do this. I'm pretty sure Visual Studio has a similar option buried somewhere in its GUI (apparently the command line switch is/Fa
).我不会在这里编写 asm,但您应该研究三件事:
将所有内容保留在寄存器中
不要为 a^2-b 重新计算 a^2 和 b^2 ^2 当您已经计算了 a^2 + b^2 时
尝试找到一个允许将 n 设置为 N 而无需迭代的条件
I won't write asm here, but three things you should investigate:
keep everything in registers
don't recompute a^2 and b^2 for a^2-b^2 when you have already computed them for a^2 + b^2
try to find a condition which allows setting n to N without iterating
for 循环大致与
The for loop is roughly the same as