【问题标题】:Inline Assembly Code Does Not Compile in Visual C++ 2010 Express内联汇编代码无法在 Visual C++ 2010 Express 中编译
【发布时间】:2025-11-23 16:20:04
【问题描述】:

我有一些从 Github 获得的程序集合并排序代码,我正在尝试将其嵌入到 C++ 中的内联程序集中,但它无法编译并不断返回这些错误:

1>c:\users\mayank\desktop\assembly\assembly\main.cpp(147): 错误 C2415: 不正确的操作数类型

我试图运行的代码是这样的:

#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;
const int ARRAYSIZE = 30;

int main()
{
    int arr[ARRAYSIZE];
    int temp_arr[ARRAYSIZE];
    int number;

    for(int x = 0; x < ARRAYSIZE; x++)
    {
        number = (rand() % 99) + 1;
        arr[x] = number;
    }
/*  
READ_ARR_LEN:
    __asm
    {
        // Read the length of the array
        //GetLInt [30]      // Size of input array
        //PutLInt [30]
    }

GET_ARRAY:
    __asm
    {
         //intel_syntax
         // Get values in arr from the user
         mov   EAX, arr
         mov   ECX, ARR_LEN
         call  Read_Arr

         // Run Merge Sort on the array
         mov   EAX, arr
         mov   EBX, temp_arr
         mov   ECX, ARR_LEN
         call  Merge_Sort

        // EXIT
    };;
*/
Merge_Sort:
    __asm
    {
        // EAX - Array start
        // ECX - array length

        // Arrays of size 0 or 1 are already sorted
        cmp   ARRAYSIZE, 2
        jl    Trivial_Merge_Sort

        // Merge_Sort (first half)
        // Length of the first half
        // ECX /= 2
        push  ARRAYSIZE
        shr   ARRAYSIZE, 1
        call  Merge_Sort
        pop   ARRAYSIZE

        // Merge_Sort (second half)
        push  arr
        push  EBX
        push  ARRAYSIZE

        // Length of the second half
        // ECX = ECX - ECX/2
        mov   EDX, ARRAYSIZE
        shr   EDX, 1
        sub   ARRAYSIZE, EDX
        imul  EDX, 4
        // Start index of the second half
        // EAX = EAX + (ECX/2) * 4
        add   arr, EDX
        push  EDX
        call  Merge_Sort
        pop   EDX

        pop   ARRAYSIZE
        pop   EBX
        pop   arr

        pushad
        // Merge (first half, second half)
        // Length of first half = ECX/2
        // Length of second half = ECX - ECX/2
        mov   EDX, ECX
        shr   ECX, 1
        sub   EDX, ECX

        // Start of second half = EAX + (ECX/2) * 4
        mov   EBX, EAX
        mov   EDI, ECX
        imul  EDI, 4
        add   EBX, EDI
        // Index of temp array = 0
        sub   EDI, EDI
        call  Merge
        popad

        // Copy back the merged array from temp_arr to arr
        call  Merge_Copy_Back_Temp

        ret
    };

Trivial_Merge_Sort:
    __asm
    {
        // In case of arrays of length 0 or 1
        ret
    };
Merge:
    __asm
        {
        // Merge two arrays contents.
        // The final merged array will be in temp_arr
        // Merging is done recursively.

        // Arguments:
        // EAX - First array's start
        // EBX - Second array's start
        // ECX - Length of first array
        // EDX - Length of second array
        // EDI - Index in temp array
        pushad

        // Handle the cases where one array is empty
        cmp   ARRAYSIZE, 0
        jz    First_Array_Over
        cmp   EDX, 0
        jz    Second_Array_Over

        // Compare first elements of both the arrays
        push  ARRAYSIZE
        push  EDI
        mov   ARRAYSIZE, [arr]
        mov   EDI, [ARRAYSIZE]
        cmp   ARRAYSIZE, EDI
        pop   EDI
        pop   ARRAYSIZE

        // Pick which ever is the least and update that array
        jl    Update_First_Array
        jmp   Update_Second_Array
    };

Update_First_Array:
   __asm
   {
        // min_elem = min (first elements of first array and second array)
        // Put min_elem into the temp array
        push  dword ptr [EAX]
        pop   dword ptr [temp_arr + EDI * 4]
        add   EAX, 4
        dec   ECX
        inc   EDI

        // Recursively call Merge on the updated array and the
        // other array
        call  Merge
        popad
        ret
   };

Update_Second_Array:
   __asm
   {
       // min_elem = min (first elements of first array and second array)
        // Put min_elem into the temp array
        push  dword ptr [EBX]
        pop   dword ptr [temp_arr + EDI * 4]
        add   EBX, 4
        dec   EDX
        inc   EDI

        // Recursively call Merge on the updated array and the
        // other array
        call  Merge
        popad
        ret
   };

Merge_Copy_Back_Temp:
   __asm
   {
        // Copy back the temp array into original array
        // Arguments:
        // EAX - original array address
        // ECX - original array length
        pushad

        // For copying back, the destination array is EAX
        mov   EBX, EAX
        // Now, the source array is temp_arr
        mov   EAX, temp_arr
        call  Copy_Array
        popad
        ret
   };

Trivial_Merge:
   __asm
   {
        // Note: One array is empty means no need to merge.
        popad
        ret
   };

First_Array_Over:
   __asm
   {
        // Copy the rest of the second array to the temp arr
        // because the first array is empty
        pushad
        mov   EAX, EBX
        mov   ECX, EDX
        mov   EBX, temp_arr
        imul  EDI, 4
        add   EBX, EDI
        call  Copy_Array
        popad
        popad
        ret
   };

Second_Array_Over:
   __asm
   {
    // Copy the rest of the first array to the temp arr
    // because the second array is empty
    pushad
    mov   EBX, temp_arr
    imul  EDI, 4
    add   EBX, EDI
    call  Copy_Array
    popad
    popad
    ret
   }; 
Copy_Array:
   __asm
   {
    // Copy array to destination array
    // EAX - Array start
    // EBX - Destination array
    // ECX - Array length

    // Trivial case
    cmp   ECX, 0
    jz    Copy_Empty_Array

    push  ECX
    sub   EDI, EDI
   };
copy_loop:
   __asm
   {
    // Copy each element
    push  dword ptr [EAX + EDI * 4]
    pop   dword ptr [EBX + EDI * 4]
    inc   EDI
    loop  copy_loop

    pop   ECX
    ret
   };

Copy_Empty_Array:
   __asm
   {
    ret
   };

Read_Arr:
   __asm
   {
        // EAX - array start
        // ECX - array length
        mov   ESI, EAX
        sub   EDI, EDI
   };
loop1:
   __asm
   {
        // Read each element
        lea eax,[esi+edx*4]
        inc   EDI
        loop  loop1
        ret
   };

    return 0;
}

【问题讨论】:

  • 为什么需要这么多的内联汇编?您不相信编译器会正确优化代码吗?你对不可维护的代码有迷恋吗?
  • 请注意,我进行了两次编辑:1) 我们不想看到相同的错误重复 N 次。 2) 题目:问题不在于你的代码没有run,而在于它没有compile。有一个巨大的差异,您必须了解差异。
  • 您使用的是 64 位编译器吗?如果是这样,你将不得不找到一种不同的方法来解决这个问题。您确定手工组装的合并排序与编译器生成的内容相比有意义吗?如果是这样,我想如果你停止使用 pushad/poppad,你会获得更多的性能。
  • 我使用 inline 只是为了测试和实验。这不会是我使用的实际程序。仅供个人学习。我也理解其中的区别。我没有意识到我说的是跑。另外,我应该用什么来代替 pushad/poppad?
  • @Mayankmmmx - 我们以前见过这个,它不是很好的汇编代码。使用 C++ 库中的std::stable_sort 会更好。 非常可能会使用归并排序。并且确实有效。

标签: c++ visual-c++ assembly inline-assembly


【解决方案1】:

(更新:在问题中发布的原始代码中,有人尝试将内存寻址为DWORD [address],这与我在我的文章中指出的 Visual C++ 内联汇编器使用的语法不兼容在下面回答。)

Visual C++ 对其内联汇编使用 MASM 语法,因此您需要使用 DWORD PTR 而不仅仅是 DWORD。这就是导致这些编译错误的原因。

参见例如this table 来自组装艺术。

【讨论】:

  • 非常感谢您的帮助!我再次编译代码,只有一个错误!错误是:1>c:\users\mayank\desktop\assembly\assembly\main.cpp(289): error C2400: inline assembler syntax error in 'opcode';找到'['
  • 可能是GetLInt [ESI + EDI * 4] 行。我不确定GetLInt 应该是什么(您在其他地方声明和定义的函数?)。在这种情况下,这不是您在 x86 程序集中执行函数调用的方式。您必须执行lea eax,[esi+edx*4] / push eax / call GetLInt 之类的操作,然后如果函数遵循标准调用约定,则返回值应该在eax 中。
  • 原来我忘记了这段代码: READ_ARR_LEN: __asm { // 读取数组的长度 GetLInt [30] // 输入数组的大小 PutLInt [30] } 但是,现在当我运行代码,我收到以下错误: 1>c:\users\mayank\desktop\assembly\assembly\main.cpp(31): error C2400: inline assembler syntax error in 'opcode';发现 '[' (在第 32 行重复)由于我添加了更多代码,第 31 和 32 行是: GetLInt [30] //输入数组的大小 PutLInt [30] 由于我进行了上述编辑,第 297 行是: lea eax,[esi+edx*4] 调用 GetLInt
  • 我建议 call 因为我认为 GetLInt 是您试图调用的函数。既然您现在说这是一个数组(intlong?),您不应该调用它。而GetLInt[30] 不是有效的 x86 汇编指令。您可能应该使用完整的当前代码更新您的原始帖子,指出您在哪些行遇到编译错误,以及您对那些失败的代码行的意图是什么(即您试图做什么?)。
  • 我更新了上面的代码以及我得到的错误。基本上,函数的目的是调用数组,然后读取每个元素。
【解决方案2】:

这看起来像是来自this github repository的代码。

在该代码中,GetLInt 实际上是一个 NASM 宏,它包含在外部宏定义文件中并调用函数 proc_GetLInt,而该函数又在目标文件 io.o 中提供,源代码不是那里。因此,问题很简单

  • 您没有意识到 GetLint 是您缺少的外部代码

  • 即使您从该存储库中获取所有文件,它也可以工作,因为 NASM 宏不能直接在 VC++ 内联汇编中工作

  • 即使你修复了宏问题,你仍然没有 GetLInt 函数,因为它只是作为 linux 对象文件提供的,你必须自己编写它

    李>

你如何解决这个问题?

该代码旨在提供一个独立的汇编程序,该程序可以自行处理所有输入/输出。由于您在 VC++ 中内联它,因此您已经拥有了更强大的 I/O 处理功能。改用它们,即在内联汇编开始之前,确保要排序的值已经在 arr 中。

然后,查看代码:Merge_Sort 期望数组的开头为 EAX,长度为 ECX。您可以从您的 C++ 代码中获取两者。当您这样做时,您不再需要汇编代码中的 READ_ARR_LENGET_ARRAY 块。

我不太愿意通过修改来重现部分代码,因为我在 github 上找不到表明我可以这样做的许可文件。让我尝试描述一下:您需要在汇编程序的最开始手动将指向arr 的指针移动到EAX 中,并将ARRAYSIZE 的内容移动到EBX 中。 (*) 如我所见,您已经处理好用数字填充数组,因此您无需在此处进行任何操作。

然后你需要删除所有不必要的汇编函数和对它们的调用。您还应该将所有单独的 __asm 块压缩为一个,或者使用外部变量来保存和恢复块之间的寄存器(or read the tutorial here,但只使用一个块就可以了,而且麻烦更少)。

最后,你必须小心堆栈帧:每个call 必须有一个匹配的ret。由于合并排序过程是递归的,因此很容易出错。

(*) 小心 VC++ 处理 asm 块内部变量的方式,确保在需要时实际使用指针。

总而言之,将其移植到 VC++ 并非易事。

【讨论】:

  • 是的,我确实将 Github 存储库中的代码作为测试内联汇编的源代码。这更能说明为什么它不起作用。我不完全理解我应该用什么替换 GetLInt 函数的意思。
  • @mayankmmmx:哇,哇。在您的问题中,您声称:... that I wrote in Inline Assembly in C++ ...。我必须说我对此有点恼火。你显然没有写它。
  • 我没有意识到我这么说。我想说的是我把它翻译成内联汇编。我为此道歉并编辑了该声明。你能帮我解决我的问题吗?
  • @Mayankmmmx:好的,你编辑了你的问题:)。我在回答中提供了一些附加信息,可以帮助您解决问题。让我知道这是否足够。
  • 这绝对是我的错误,我不应该这么说:)。现在我的问题是,如果我取出 READ_ARR_LEN 和 GET_ARR,如何将数组的开头和数组长度添加到 Merge_Sort?