如何将数组引用作为参数从程序集传递给c++函数
How to pass an array reference as a parameter from assembly to a c++ function
我在两个Visual Studio 2012项目中有两个独立的文件。一个是MASM,另一个是C++。MASM程序应该调用C++程序中的DisplayBoard函数,并且需要将引用传递给它正在显示的数组。我不知道我到底需要做些什么才能让这件事成功。该程序是作为一个C++程序整体创建的,并且按照它应该的方式工作,但我们应该在MASM中完成大部分编码,并且具有最少的C++函数,所以我们试图让这两个文件进行对话,但遇到了问题。以下是我的MASM和C++文件的骨架代码。我不确定C++文件是否需要main,但它确实在没有main的情况下编译。此外,如果将板阵列作为参数传入,是否需要在C++文件中声明板阵列?我想没有,但不确定。我不知道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
这就是我们得到的错误:
------生成已启动:项目:HexAssembly,配置:调试Win32------1> 正在组装HexAssemblyTest.asm。。。1> HexAssemblyTest.obj:错误LNK2019:函数_main中引用的未解析外部符号_ShowBoard1> C:\Irvine\Examples\Assembly Hex programming\Debug\HexAssembly.exe:致命错误LNK1120:1个未解析的外部==========生成:0成功,1失败,0最新,0跳过==========
我想我现在工作正常了。。。我修改了DFSAlgorithm.cpp和DFSAlgarithm.h,编译了C++文件,并将DFSAlsogrithm.obj文件添加到有汇编文件的项目中。它们现在正在链接,但当C++DFS搜索运行时,我收到了一条"deque迭代器不可取消引用"的错误消息。当整个程序都在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++中时,它工作得很好,所以我不确定为什么它现在不工作。我认为这与C++函数如何访问array_index有关。
错误告诉您问题非常清楚;您没有全局函数CCD_ 1的定义。
如果你期待DFSAlgorithm::showBoard
的定义,那么你会失望的原因有两个:
DFSAlgorithm::showBoard
不是全局函数,而是成员函数(它将在DFSAlgorithm
的哪个实例上操作?(- 5和CCD_ 6的拼写不同
至于main
,您的C++文件不应该定义main
,因为您的程序集文件定义了CCD_8。而且您只希望在整个程序中有一个这样的定义。
- 具有外部"c"和程序集的未定义函数
- C++变量在调用 x64 程序集函数后重置为 0
- 将内联程序集尾调用函数尾声替换为用于x86/x64 msvc的Intrinsics
- 如何在程序集函数中将元素数组作为参数传递时转发 ARM 寄存器的地址指针
- 混合 c++ 和汇编不能将多个参数从C++函数传递到程序集
- 对 constexpr 与内联函数的调用编译为不同的程序集,并禁用优化
- 从x86_64程序集调用函数
- 在结构中嵌入函数程序集代码
- 在程序集文件内部调用C++函数
- 将引用参数传递给程序集函数
- 如何使用 x86 程序集在 c 中访问 main 函数中的变量
- 为什么用户编写的构造函数会影响生成的程序集
- 调用 x64 VC++ 的程序集函数
- 性能 AVX/SSE 程序集与内部函数
- c++的构造函数和析构函数程序集
- 为什么包装在函数中的 GAS 内联程序集为调用方生成的指令与纯程序集函数不同
- 如何从程序集调用C++函数
- GCC 内联程序集 JMP 地址;裸函数
- 在不同的编译器中定义程序集上的函数
- 如何使用程序集获取返回float的__stdcall函数的结果