我如何正确地将asm文件链接到c ++?

时间:2011-11-17 04:59:43

标签: c++ visual-studio-2010 assembly linker

这是一个hw问题,香港专业教育学院完成了所有的编码,但我无法将asm与c ++连接,我使用windows visual studio 2010,我把主要放在源文件中,我的asm文件放在资源文件中,当我尝试编译它只是给我一个链接错误

1>------ Build started: Project: clearArray, Configuration: Debug Win32 ------ 
1>clearArray.cpp 
1>clearArray.obj : error LNK2019: unresolved external symbol _clearPointerOp referenced in function _main 
1>clearArray.obj : error LNK2019: unresolved external symbol _clearIndexOp referenced in function _main 
1>C:\Users\Joe Chen\documents\visual studio 2010\Projects\clearArray\Debug\clearArray.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这个hw的目的是使用索引方法和指针方法清除数组,然后优化生成的asm代码

请帮助!!!

继承我的代码: main.cpp中

// clear array using unoptimized code with index and pointers
#include<iostream>
#include<fstream>
#include"timer.h"

using namespace std;

extern "C" {void clearIndexOp(int A[], int size);}
extern "C" {void clearPointerOp(int *A, int size);}

const int size = 100000;
int A[size] = {0};

void clearIndex(int A[], int size)
{
    for(int i=0; i<size; i++)
        A[i]=0;
}

void clearPointer(int *A, int size)
{
    int *p;
    for(p=&A[0]; p<&A[size]; p++)
        *p=0;
}

int main()
{   
    double timeIndex = 0;
    double timeIndexOp = 0;
    double timePointer = 0;
    double timePointerOp = 0;
    StopWatch time;
    ofstream myfile;
    myfile.open("results.txt");

    for(int n=2000; n<1000000; n=n*2)
    {
        // put values into the array
        for(int i=0; i<size; i++)
            A[i]=i+rand()%10+1;

        time.startTimer();
        clearIndex(A, size);
        time.stopTimer();
        timeIndex =  time.getElapsedTime();

        // put values into the array
        for(int i=0; i<size; i++)
            A[i]=i+rand()%10+1;

        time.startTimer();
        clearPointer(A, size);
        time.stopTimer();
        timePointer = time.getElapsedTime();

        // put values into the array
        for(int i=0; i<size; i++)
            A[i]=i+rand()%10+1;

        time.startTimer();
        clearIndexOp(A, size);
        time.stopTimer();
        timeIndexOp =  time.getElapsedTime();

        // put values into the array
        for(int i=0; i<size; i++)
            A[i]=i+rand()%10+1;

        time.startTimer();
        clearPointerOp(A, size);
        time.stopTimer();
        timePointerOp = time.getElapsedTime();

        myfile << "n is now: " << n << "\n";
        myfile << "timeIndex is: " << timeIndex << "\n";
        myfile << "timePointer is: " << timePointer << "\n";
        myfile << "timeIndexOp is: " << timeIndexOp << "\n";
        myfile << "timePointerOp is: " << timePointerOp << "\n";        
    }
    myfile.close();
}

clearIndexOp.asm

.386
.model flat
.stack
.code

global _clearIndexOp proc
_i$ = -8                            ; size = 4
_A$ = 8                                 ; size = 4
_size$ = 12                             ; size = 4

; {
    push    ebp
    mov ebp, esp
    sub esp, 204                        ; 000000ccH
    push    ebx
    push    esi
    push    edi
    lea edi, DWORD PTR [ebp-204]
    mov ecx, 51                         ; 00000033H
    mov eax, -858993460                 ; ccccccccH
    rep stosd

; for(int i=0; i<size; i++)
; initialize the variables
    mov eax, 0                          ; init i=0 to eax
    mov ebx, DWORD PTR _size$[ebp]      ; size stored in ebx for faster access than memory
    mov ecx, DWORD PTR _A$[ebp]         ; get base addr of array
    jmp SHORT $LN3@clearIndex           ; jump into the loop
$LN2@clearIndex:
    add eax, 1                          ; increase eax since eax=i
$LN3@clearIndex:
    cmp eax, ebx                        ; check that i < size
    jge SHORT $LN4@clearIndex           ; exits if i >= size

; A[i]=0;
    mov DWORD PTR [ecx+eax*4], 0        ; A[i]=0
    jmp SHORT $LN2@clearIndex           ; go back to loop body

; after removing useless/repetitive codes 
; we shrunk this code from 10 instructions to only 5 instructions

$LN4@clearIndex:

; }
    pop edi
    pop esi
    pop ebx
    mov esp, ebp
    pop ebp
    ret 0
_clearIndexOp ENDP              

clearPointerOp.asm

.386
.model flat
.stack
.code

global _clearPointerOp proc 
_p$ = -8                                    ; size = 4
_A$ = 8                                     ; size = 4
_size$ = 12                                 ; size = 4

; {
    push ebp
    mov ebp, esp
    sub esp, 204                            ; 000000ccH
    push ebx
    push esi
    push edi
    lea edi, DWORD PTR [ebp-204]
    mov ecx, 51                             ; 00000033H
    mov eax, -858993460                     ; ccccccccH
    rep stosd

; int *p;
; for(p=&A[0]; p<&A[size]; p
; initialize the variables
    mov eax, DWORD PTR _A$[ebp]             ; base addr of the array
    mov DWORD PTR _p$[ebp], eax             ; init p = A[0]
    mov ebx, DWORD PTR _p$[ebp]             ; move p to ebx
    mov ecx, DWORD PTR _size$[ebp]          ; size stored in ecx for faster access from register
    lea edx, DWORD PTR [ecx+eax*4]          ; last index of array, A[size-1]
    jmp SHORT $LN3@clearPoint               ; jump into loop
$LN2@clearPoint:
    add eax, 4                              ; since it is pointer we increase eax by 4 to move to next element
$LN3@clearPoint:
    cmp ebx, edx                            ; check that p < size
    jae SHORT $LN4@clearPoint               ; exit if p >= size

; *p=0;
    mov DWORD PTR [ebx], 0
    jmp SHORT $LN2@clearPoint

; after removing useless/repetitive codes
; we shrunk this code from 11 instructions to only 5 instructions

$LN4@clearPoint:

; }
    pop edi
    pop esi
    pop ebx
    mov esp, ebp
    pop ebp
    ret 0
_clearPointerOp ENDP            

3 个答案:

答案 0 :(得分:8)

问题是你的asm没有被视为源文件。

修复:

1)右键单击您的项目并选择Build Customizations,然后选中masm

旁边的框

2)右键单击.asm文件,选择Properties,然后将Item Type更改为Microsoft Macro Assembler。

编辑#2:我现在看到你正在使用VS生成的asm代码的修改版本,它几乎没问题。

从PROC声明中删除“global”,然后在asm文件的末尾添加END。

那应该让asm正确组装和链接。但看起来你可能在clearPointerOp中搞砸了一些东西,因为它最后会进入一个无限循环。一旦你的代码编译和链接,你应该能够从中找到它。

答案 1 :(得分:2)

我认为我在asm代码中看到了许多错误和效率低下,但首先,为什么不使用memset()标准C函数?

答案 2 :(得分:1)

我也有很长一段时间 - 但你似乎已经使用C调用约定导入函数名称,这很好(删除C ++名称修改)。

有些问题:

  1. 您是否编译了asm文件并将它们作为目标文件链接
  2. 您是否检查了您正在使用的c ++编译器使用的调用约定,并在您的asm函数中匹配它们