错误 LNK2019:无法解析的外部符号 C++和 ASM

发布于 2025-01-12 07:16:01 字数 3418 浏览 0 评论 0原文

当我尝试运行这个程序来比较使用 C++ 和 ASM 的 C++ 中同一数组的排序速度时,我遇到了这个错误。

错误信息:

错误 LNK2019:函数 _main 中引用的外部符号 _AsmSelectionSort 无法解析 关于已定义且可能匹配的符号的提示: AsmSelectionSort

声明它的 C++ 代码部分如下:

#include <iostream>
#include <iomanip>
#include <chrono>

using namespace std;

// Function prototypes for the selection sort and swap functions.
void selectionSort(int array[], int size);
void swap(int &a, int &b);

// Function prototype for the boolean isAscending function.
bool isAscending(int numbers[], int size);

// Function Prototype for the assembly language version of selection sort.
extern "C" {
    void AsmSelectionSort(int array[], int count);
}

这是调用 asm 的地方:

    cout << " Using the Assembly Language Selection Sort Algorithm." << endl;
    start = chrono::high_resolution_clock::now();
    for (int n = 0; n < REPETITIONS; n++)
    {
        // Copy the randomly generated array.
        for (int i = 0; i < SIZE; i++)
        {
            numbers[i] = element[i];
        }

        // Use the ASM version of the Selection Sort algorithm.
        AsmSelectionSort(numbers, SIZE);
    }
    end = chrono::high_resolution_clock::now();
    auto asmDiff = end - start;

我的 ASM:

.486
.model small
.code
AsmSelectionSort PROC
   
   PUSH AX                        ; push AX onto the STACK  
   PUSH BX                        ; push BX onto the STACK
   PUSH CX                        ; push CX onto the STACK
   PUSH DX                        ; push DX onto the STACK
   PUSH DI                        ; push DI onto the STACK

   CMP BX, 1                      ; compare BX with 1
   JLE @Skip_Sorting              ; jump to label @Skip_Sorting if BX<=1

   DEC BX                         ; set BX=BX-1
   MOV CX, BX                     ; set CX=BX
   MOV AX, SI                     ; set AX=SI

   @Outer_Loop:                   ; loop label
     MOV BX, CX                   ; set BX=CX
     MOV SI, AX                   ; set SI=AX
     MOV DI, AX                   ; set DI=AX
     MOV DL, [DI]                 ; set DL=[DI]

     @Inner_Loop:                 ; loop label
       INC SI                     ; set SI=SI+1

       CMP [SI], DL               ; compare [SI] with DL
       JNG @Skip                  ; jump to label @Skip if [SI]<=DL

       MOV DI, SI                 ; set DI=SI
       MOV DL, [DI]               ; set DL=[DI]

       @Skip:                     ; jump label

       DEC BX                     ; set BX=BX-1
     JNZ @Inner_Loop              ; jump to label @Inner_Loop if BX!=0

     MOV DL, [SI]                 ; set DL=[SI]
     XCHG DL, [DI]                ; set DL=[DI] , [DI]=DL
     MOV [SI], DL                 ; set [SI]=DL

   LOOP @Outer_Loop               ; jump to label @Outer_Loop while CX!=0

   @Skip_Sorting:                 ; jump label

   POP DI                         ; pop a value from STACK into DI
   POP DX                         ; pop a value from STACK into DX
   POP CX                         ; pop a value from STACK into CX
   POP BX                         ; pop a value from STACK into BX
   POP AX                         ; pop a value from STACK into AX

   RET                            ; return control to the calling procedure
AsmSelectionSort ENDP
end

我做错了什么?

I ran into this error when trying to run this program that compares the sorting speed of the same array in C++ using both C++ and ASM.

Error Message:

error LNK2019: unresolved external symbol _AsmSelectionSort referenced in function _main
Hint on symbols that are defined and could potentially match:
AsmSelectionSort

A section of the C++ Code where it is declared is as follows:

#include <iostream>
#include <iomanip>
#include <chrono>

using namespace std;

// Function prototypes for the selection sort and swap functions.
void selectionSort(int array[], int size);
void swap(int &a, int &b);

// Function prototype for the boolean isAscending function.
bool isAscending(int numbers[], int size);

// Function Prototype for the assembly language version of selection sort.
extern "C" {
    void AsmSelectionSort(int array[], int count);
}

And this is where the asm is called:

    cout << " Using the Assembly Language Selection Sort Algorithm." << endl;
    start = chrono::high_resolution_clock::now();
    for (int n = 0; n < REPETITIONS; n++)
    {
        // Copy the randomly generated array.
        for (int i = 0; i < SIZE; i++)
        {
            numbers[i] = element[i];
        }

        // Use the ASM version of the Selection Sort algorithm.
        AsmSelectionSort(numbers, SIZE);
    }
    end = chrono::high_resolution_clock::now();
    auto asmDiff = end - start;

My ASM:

.486
.model small
.code
AsmSelectionSort PROC
   
   PUSH AX                        ; push AX onto the STACK  
   PUSH BX                        ; push BX onto the STACK
   PUSH CX                        ; push CX onto the STACK
   PUSH DX                        ; push DX onto the STACK
   PUSH DI                        ; push DI onto the STACK

   CMP BX, 1                      ; compare BX with 1
   JLE @Skip_Sorting              ; jump to label @Skip_Sorting if BX<=1

   DEC BX                         ; set BX=BX-1
   MOV CX, BX                     ; set CX=BX
   MOV AX, SI                     ; set AX=SI

   @Outer_Loop:                   ; loop label
     MOV BX, CX                   ; set BX=CX
     MOV SI, AX                   ; set SI=AX
     MOV DI, AX                   ; set DI=AX
     MOV DL, [DI]                 ; set DL=[DI]

     @Inner_Loop:                 ; loop label
       INC SI                     ; set SI=SI+1

       CMP [SI], DL               ; compare [SI] with DL
       JNG @Skip                  ; jump to label @Skip if [SI]<=DL

       MOV DI, SI                 ; set DI=SI
       MOV DL, [DI]               ; set DL=[DI]

       @Skip:                     ; jump label

       DEC BX                     ; set BX=BX-1
     JNZ @Inner_Loop              ; jump to label @Inner_Loop if BX!=0

     MOV DL, [SI]                 ; set DL=[SI]
     XCHG DL, [DI]                ; set DL=[DI] , [DI]=DL
     MOV [SI], DL                 ; set [SI]=DL

   LOOP @Outer_Loop               ; jump to label @Outer_Loop while CX!=0

   @Skip_Sorting:                 ; jump label

   POP DI                         ; pop a value from STACK into DI
   POP DX                         ; pop a value from STACK into DX
   POP CX                         ; pop a value from STACK into CX
   POP BX                         ; pop a value from STACK into BX
   POP AX                         ; pop a value from STACK into AX

   RET                            ; return control to the calling procedure
AsmSelectionSort ENDP
end

What did I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文