Visual C 和内联装配代码在一个程序中具有相同的输出,但在另一个程序中具有不同的输出

visual C++ and inline assembly code have same output in one program, but different in another

本文关键字:程序 输出 另一个 代码 Visual 一个      更新时间:2023-10-16

我正在处理一个分配,在该分配中,我必须将C代码的摘要转换为内联汇编。该代码是渲染朱莉娅分形的程序的一部分。

我已经测试了两个代码片段的输出,并且它们完全匹配,但是我的程序仍然输出了不同的图像(适合C代码的朱莉娅fractal,一个用于内联装配代码的平面粉红色屏幕)。

这是功能的起始部分,也是返回

COLORREF render_point(const double &a, 
                      const double &b, int N) {
  double cRe = -0.5;
  double cIm = -0.05;
  double x(a), y(b);
  double norm = x*x+y*y;
  int n;
  double three = 3.0;
  (loop goes here)
  return HSVtoRGB(n % 256, 255 , 255 *(n<N));
}

这是C代码

for (n = 0; norm < 4.0 && n < N; ++n) 
{
    double old_x = x;
    double old_y = y;
    x = (old_x * old_x * old_x) - (3 * old_y * old_y * old_x) + cRe;
    y = (3 * old_y * old_x * old_x) - (old_y * old_y * old_y) + cIm;
    norm = x*x+y*y;
}

和内联装配代码:

for (n = 0; norm < 4.0 && n < N; ++n) 
  {
    __asm {
        // Create (old_x * old_x * old_x)
        fld x;
        fmul x;
        fmul x;
        // Create (3 * old_y * old_y * old_x)
        fld three;
        fmul y;
        fmul y;
        fmul x;
        // Create the full equation for x
        fsubp st(1), st(0);
        fadd cRe;
        // Create (3 * old_y * old_x * old_x) + cIm
        fld three;
        fmul y;
        fmul x;
        fmul x;
        fadd cIm;
        // Create (old_y * old_y * old_y)
        fld y;
        fmul y;
        fmul y;
        fsubp st(1), st(0); // Create the full equation for y
        fst y;              // Store in y to use for next loop
        fmul st(0), st(0);  // Get y*y
        fxch st(1);         // Swap places of y*y with newly calculated x
        fst x;              // Store in x to use for next loop
        fmul st(0), st(0);  // Get x*x
        faddp st(1), st(0); // Get x*x + y*y
        fst norm;           // Set loop variable
    }
  }

两个循环之间可能会导致程序中的输出不同?

as 1201 ProgramAlarm在评论中提到的,只需在每个环路迭代末尾弹出fPU的剩余值规范。