繁体   English   中英

如何将MASM数组传递给C ++函数

[英]How to pass MASM array to c++ function

我必须将数组从汇编程序传递给C ++函数。 我终于想出了如何让两个文件对话,现在我想不通如何将MASM数组的地址传递给我的C ++函数。 我尝试在masm中同时使用ptr和addr调用递归DFS。 不知道我做错了什么,因为我仅用汇编和C ++进行了2个月的编程。 调用printSomething函数的确显示,因此我知道两个程序正在通信,但是,调用DFS时,我得到了一个不可取消引用的双端队列迭代器,因此我不确定发生了什么,但这与使用我的堆栈的DFS有关DFS算法中的“路径”。 我尝试在DFS函数中使用int * hex_array []作为参数,但事实并非如此。 DFS在数组中搜索值1(红色搜索),向每个访问的红色十六进制加3。 如果未找到路径,则通过从每个“访问的十六进制”中减去3来重置“访问的”十六进制,并返回-1。如果找到有效的路径,则返回1。DFS在整个程序位于C ++中时起作用。并不是函数本身还是堆栈问题,尽管当我在VS2012中逐步调试时,我发现每次堆栈= 1时,它似乎都失败了,我将array_index设置为路径top,弹出堆栈并调用带有array_index的DFS。那时候,栈是空的。但是我不明白为什么当整个程序都在C ++文件中时它在正常工作时会在这里失败。我想它与MASM有关C ++函数无法以应有的方式访问数组???

我的汇编代码的一部分:

INCLUDE Irvine32.inc

printSomething PROTO C ;displays "Goobers" 
DFS PROTO C, color:BYTE, bptr:PTR DWORD, arrayIndex:SDWORD

PDWORD TYPEDEF PTR DWORD

.data

ALIGN SDWORD

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

.code

main PROC 

INVOKE printSomething   ;test to see if MASM talking to C++ program

Start:              
    CALL PlaceRed       ;prompt user to place a red stone
    CALL ShowBoard      ;redraw board to show update

    ;check if there is a valid path using C++ DFS 
    ;What needs to be saved...? not aX since DFS will return -1 (no path) or 1(path) in aX from C++ function
    PUSH EDX
    PUSH EBX
    PUSH ECX
    ;INVOKE DFS, 1, ADDR board, 0   ; color red, board pointer, arrayIndex 0
    INVOKE DFS, 1, bptr, 0      ; color red, board pointer, arrayIndex 0
    POP ECX
    POP EBX
    POP EDX
    CMP AX,1            ;if Ax == 1 winning path found
    JNE Continue            ;Ax != 1 no valid path...continue game
    MOV EDX, OFFSET redWins     ; move "Red wins..." to eDx and display
    CALL WriteString    
    JMP END_GAME        

Continue:
    CALL PlaceBlue      ;place a blue stone
    CALL ShowBoard      ;redraw the board

    ;check if there is a valid path using C++ DFS
    PUSH EDX
    PUSH EBX
    PUSH ECX
    ;INVOKE DFS, 2, ADDR board, 0; color blue (2), pointer, arrayIndex 0
    INVOKE DFS, 2, bptr, 0; color blue (2), pointer, arrayIndex 0
    POP ECX
    POP EBX
    POP EDX
    CMP AX,1                ;if Ax == 1 winning path found
    JNE Start               ;Ax != 1 no valid path...continue game
    MOV EDX, OFFSET blueWins ; move "Blue wins..." to eDx and display
    CALL WriteString

END_GAME:

Retn
main ENDP

END main

和我的C ++代码的一部分

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

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

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

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

//DFS code here...blah blah...  
    }

我的头文件

//DFSAlgorithm.h 
//Definition of DFSAlgorithm class that does the DFS for the game of HEX
//Member functions are defined in DFSAlgorithm.ccp

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

#endif

根据Ferruccio的要求完整地包含DFS代码

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

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

//test printsomething
extern "C" void printSomething(){
    cout<<"I'm not dead yet...";
}

//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 the hex onto the 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 the adjacent hexes
if ((color == 1 &&  hex_array[array_index] == 4)  || 
    (color == 2  &&  hex_array[array_index] == 5)){

    //get adjacencies
    if(array_index == 0){//top left 2 corner
        adjacency[ 0 ] = 1;
        adjacency[ 1 ] = 11;
        adjacency[ 2 ] = - 1;
        adjacency[ 3 ] = - 1;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
        }

    else if(array_index == 10){//top right three corner
        adjacency[ 0 ] = 9;
        adjacency[ 1 ] = 20;
        adjacency[ 2 ] = 21;
        adjacency[ 3 ] = - 1;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
    }

    else if(array_index == 110){//bottom left corner
        adjacency[ 0 ] = 99;
        adjacency[ 1 ] = 100;
        adjacency[ 2 ] = 111;
        adjacency[ 3 ] = - 1;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
    }
    else if(array_index==120){//bottom right corner
        adjacency[ 0 ] = 109;
        adjacency[ 1 ] = 119;
        adjacency[ 2 ] = -1;
        adjacency[ 3 ] = -1;
        adjacency[ 4 ] = -1;
        adjacency[ 5 ] = -1;
    }

    else if(array_index / 11 == 0){//top row minus corners
        adjacency[ 0 ] = array_index - 1;
        adjacency[ 1 ] = array_index + 1;
        adjacency[ 2 ] = array_index + 10;
        adjacency[ 3 ] = array_index + 11;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
    }

    else if(array_index % 11 == 0){//left column minus corners
        adjacency[ 0 ] = array_index - 11;
        adjacency[ 1 ] = array_index + 11;
        adjacency[ 2 ] = array_index - 10;
        adjacency[ 3 ] = array_index + 1;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
    }

    else if (array_index / 11 == 10){//row 10 minus corners
        adjacency[ 0 ]= array_index - 1;
        adjacency[ 1 ]= array_index + 1;
        adjacency[ 2 ]= array_index - 11;
        adjacency[ 3 ]= array_index - 10;
        adjacency[ 4 ]= - 1;
        adjacency[ 5 ]= - 1;
    }

    else if( array_index % 11 == 10){//right column minus corners
        adjacency[ 0 ] = array_index - 11;
        adjacency[ 1 ] = array_index + 11;
        adjacency[ 2 ] = array_index - 1;
        adjacency[ 3 ] = array_index + 10;
        adjacency[ 4 ] = - 1;
        adjacency[ 5 ] = - 1;
    }

    else{//all interior hexes
        adjacency[ 0 ] = array_index - 11;
        adjacency[ 1 ] = array_index + 11;
        adjacency[ 2 ] = array_index - 10;
        adjacency[ 3 ] = array_index + 10;
        adjacency[ 4 ] = array_index - 1;
        adjacency[ 5 ]= array_index + 1;
        }

    /*Initialize adjacentHexes count to zero: if == 0 after all 6 adjacencies are 
    checked it means 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 the 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 the 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      
                    array_index = path.top();//this is the line causing deque iterator not dereferenceable problems+++++++++++++++++++++++

                    //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 hexes 
            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 hexes
            for(array_index ; array_index <= 9; ){

                //recursively call DFS with next Row 0 hex
                return DFS(color, hex_array, array_index + 1);
                }
            }
            //No path exists 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
    }

如果参数类型匹配,可能是一个好主意。 DFScolor )的第一个参数在MASM PROTO指令中声明为BYTE ,但在C ++代码中为int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM