如何将数组引用作为参数从程序集传递给c ++函数

时间:2014-03-21 16:01:14

标签: c++ assembly parameter-passing masm irvine32

我在两个Visual Studio 2012项目中有两个单独的文件。一个是MASM,另一个是C ++。 MASM程序应该在C ++程序中调用DisplayBoard函数,并且需要传递对它正在显示的数组的引用。我无法弄清楚我需要做些什么来完成这项工作。该程序作为一个C ++程序完全创建并以它应该的方式工作,但是我们应该在MASM中进行大部分编码并且具有最小的C ++函数,所以我们试图让这两个文件说话但是有问题。以下是我的MASM和C ++文件的框架代码。我不确定C ++文件是否需要main,但它确实没有编译。另外,如果作为参数传入,则需要在C ++文件中声明board数组吗?我认为它没有,但我不确定。我不知道数组参数是否在C ++文件中正确引用。

汇编代码:

TITLE HexAssemblyTest    (HexAssemblyTest.asm)
    .586
    .model flat,C

includelib kernel32.lib
includelib Irvine32.lib

ShowBoard PROTO near C, hexBoard:SDWORD

.data

board SDWORD 121 DUP (0)        ;array to hold the hex board


.code

main PROC 

    INVOKE ShowBoard, ADDR board    ;display board

Retn
main ENDP

END main

C ++代码:

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<Windows.h>
#include<stack>

using namespace std;

extern "C" void showBoard(int hex_array[]);



//Class DFS definition
class DFSAlgorithm{

public:
int board[121]; //board array


//function to display the board 
    void showBoard(int hex_array[]){

    //code here...

    }
//other functions...removed
    }

};//end DFSAlgorithm class

这是我们得到的错误:

  

------ Build build:Project:HexAssembly,配置:Debug Win32 ------   1 GT;组装HexAssemblyTest.asm ...   1&gt; HexAssemblyTest.obj:错误LNK2019:函数_main中引用的未解析的外部符号_ShowBoard   1&gt; C:\ Irvine \ Examples \ Assembly Hex programming \ Debug \ HexAssembly.exe:致命错误LNK1120:1未解析的外部   ==========构建:0成功,1个失败,0个最新,0个跳过==========

我认为我现在正常运行...我修改了DFSAlgorithm.cpp和DFSAlgorithm.h,编译了C ++文件并将DFSAlsogrithm.obj文件添加到具有汇编文件的项目中。他们现在正在联系,但是我得到了一个&#34; deque迭代器而不是可解除引用的&#34;现在运行C ++ DFS搜索时出现错误消息。它工作得很好而整个程序都是用C ++编写的,所以我不知道我需要更改什么才能使它正常工作,因为从程序集文件中访问数组。在使用我的调试器时,我可以看到它正在生成邻接数组,但我不认为该数组实际上正在被访问...

TITLE HexAssemblyTest    (HexAssemblyTest.asm)

INCLUDE Irvine32.inc

printSomething PROTO C ;displays "GoobersX"
DFS PROTO C, color:BYTE, bptr:PTR DWORD, index:SDWORD

PDWORD TYPEDEF PTR DWORD

.data

bptr PDWORD board   
board SDWORD 121 DUP (0)        ;array to hold the hex board
arrayIndex SDWORD 0         ;variable to hold arrayIndex  


.code

main PROC 

INVOKE printSomething   ;tests if MASM and C++ are talking

Start:          
    CALL PlaceRed       ;prompt user to place a red stone
    CALL clrscr
    CALL crlf
    CALL ShowBoard      ;redraw the board

    ;check if there is a valid path using C++ DFS 
    PUSH EDX
    PUSH EBX
    PUSH ECX
    INVOKE DFS, 1, ADDR board, 0    ;color red, board address, arrayIndex 0
    POP ECX
    POP EBX
    POP EDX
    CMP EAX,1       ;if eAx == 1 winning path found
    JNE Continue        ;eAx != 1 no valid path...continue game

  ;the rest of this code removed for brevity

END_GAME:

Retn
main ENDP

我的C ++头文件如下所示:

C++ header file DFSAlgorithm.h

#ifndef DFSAlgorithm_H
#define DFSAlgorithm_H
extern "C" void printSomething();
extern "C" int DFS(int color, int hex_array[], int array_index);

#endif

我的C ++ cpp文件(缩写)如下所示:

#include "stdafx.h"
#include<iostream>
#include<stack>
#include "DFSAlgorithm.h"//include definition of class DFSAlgorithm
using namespace std;

int adjacency[6];
stack<int> path; //stack to hold the last hex visited

//test printsomething
extern "C" void printSomething(){
    cout<<"Goobers2014";
}

//First call of DFS always starts with array_index ==  0
extern "C" int DFS(int color, int hex_array[], int array_index){    

    if (hex_array[array_index] == color){ //if hex has an appropriately colored stone

        hex_array[array_index] += 3;    //mark the hex as visited

        path.push(array_index); //push hex onto path stack
    }
    if ((color == 1 && array_index % 11 == 10 && hex_array[array_index] == 4) || 
        (color == 2 && array_index / 11 == 10 && hex_array[array_index] == 5)){

    return 1; //winner base case==>reached the other side
    }

//If a visited/unvisited hex has a stone of correct color==> search adjacent hexes
if ((color == 1 &&  hex_array[array_index] == 4)  || 
    (color == 2  &&  hex_array[array_index] == 5)){

    //get adjacencies
    //removed from code for brevity
        }

    /*Initialize adjacentHexes to zero: if == 0 after all 6 adjacencies are 
    checked it is a dead end as there are no unvisited adjacent hexes with 
    the correct color stone*/
    int adjacentHexes = 0;  
        for(int b = 0; b < 6; b++){//traverse adjacency array of passed in index

//if one of the adjacent hexes has a red/blue stone
    if((color == 1 && hex_array[adjacency[b]] == color) ||
    (color == 2 && hex_array[adjacency[b]] == color )){ 

        adjacentHexes++;    //increment adjacentHexes count

        hex_array[adjacency[b]] += 3;   //mark the hex as visited

        path.push(adjacency[b]); //push visited adjacent hex onto path 

        //recursively call DFS with that adjacent hex index
                return DFS(color, hex_array,adjacency[b]);  

                }
            }
        //If adjacentHexes == 0 ==> dead-end
                if(adjacentHexes == 0 && path.size() > 1){

        path.pop();//pop the top hex from the stack if stack > 1

        //recursive call of DFS with the new top red/blue hex
        return DFS(color, hex_array,path.top());
                    }
        if(adjacentHexes == 0 && path.size() == 1){//back to Row 0/Column 0

                    //make the array_index = the top of the path stack      
//+++++this line generates a "deque iterator not dereferenceable" error++++++++++++++
                    array_index = path.top();

                    //pop remaining element from the stack so path is now zero
                    path.pop();
                }
    }
        //if checking for a red path and path is empty
        if (color == 1 ){

            //search remaining column 0 hexes for unvisited red hex 
            for(array_index ; array_index <= 99; ){ 

                //recursively call DFS with next Column 0 hex
                return DFS(color, hex_array, array_index + 11);
                }
        }

        //if checking for a blue path and path is empty
        if (color == 2){

        //search remaining row 0 hexes for unvisted blue hex
            for(array_index ; array_index <= 9; ){

                //recursively call DFS with next Row 0 hex
                return DFS(color, hex_array, array_index + 1);
                }
            }
            //Traverse hex_array and reset all visited hexes to unvisited
            for(int a = 0; a < 121; a++){
                if(hex_array[a] >= 4)//if hex has been visited
                    hex_array[a] -= 3;//remove visited designation  
            }

        return -1;//return false as no path exists

}

我不确定为什么它在我将array_index设置为path.top()的行上失败然后从堆栈顶部弹出,因为当整个文件在C ++中时它工作正常所以我&#39 ;我不确定为什么它现在不起作用。我假设它与C ++函数如何访问array_index有关。

1 个答案:

答案 0 :(得分:3)

错误非常清楚地告诉您问题;你没有全局函数ShowBoard的定义。

如果您期待DFSAlgorithm::showBoard的定义,那么您会因两个原因感到失望:

  1. DFSAlgorithm::showBoard不是一个全局函数,而是一个成员函数(它将运行DFSAlgorithm的实例?);
  2. showBoardShowBoard拼写不同。
  3. 对于main,您的C ++文件不应该定义main,因为您的程序集文件有,并且您只需要在程序中一个这样的定义。