2014-03-25 2 views
0

배열 프로그램에서 C++ 함수에 배열을 전달해야합니다. 필자는 마침내 두 파일을 어떻게 처리하는지 알아 냈습니다. 이제 C++ 함수에 MASM 배열의 주소를 전달하는 방법을 알 수 없습니다. mssm에서 ptr과 addr을 모두 사용하여 재귀 DFS를 호출하려고했습니다. 내가 2 개월 넘게 어셈블리와 C++로 프로그래밍을 해왔으므로 내가 뭘 잘못하고 있는지 확신 할 수 없다. 호출 된 printSomething 함수가 표시되므로 두 프로그램이 통신하고 있음을 알 수 있습니다. 그러나 DFS가 호출 될 때 dereferenceable이 아닌 dequeue iterator를 얻습니다. 그래서 무슨 일이 일어나는지 확실하지 않지만 내 스택을 사용하는 DFS와 관련이 있습니다. DFS 알고리즘에서 "경로". 나는 int * hex_array []를 DFS 함수에서 인자로 사용하려고 시도했으나 그것을 좋아하지 않았다. DFS는 어레이의 값 1 (빨간색 검색)을 검색하고 방문한 각 빨간색 헥스에 3을 추가합니다. 경로가 발견되지 않으면 각 "방문한"칸에서 3을 빼서 "방문한"칸을 재설정하고 -1을 반환하며, 유효한 경로가 발견되면 1을 반환합니다 .DFS는 전체 프로그램이 C++이기 때문에 작동합니다 함수 자체 또는 스택 문제가되지 않습니다. 내가 VS2012 내 디버거를 통해 단계 때 스택 = 1 때마다 실패 할 것으로 나타납니다 및 경로 위쪽에 array_index 설정할 스택 및 호출 DFS with array_index. 그 시점에서 스택이 비어 있습니다.하지만 전체 프로그램이 C++ 파일에있을 때 완벽하게 작동했을 때 왜 여기서 실패하는지 이해할 수 없습니다 .MASM과 관련이 있다고 생각합니다.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에 액세스 안한다 내 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 
거기에

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 
    } 

답변

0

그것은 아마 좋은 생각이다 매개 변수 유형의 경우 일치해라. DFS (color)의 첫 번째 매개 변수는 MASM PROTO 지시문에서 BYTE으로 선언되어 있지만 C++ 코드에서는 int입니다.

+0

흠. 내가 그걸 어떻게 일치 시킬지 모르겠다. – TryChick

+0

@TryChick - SDWORD가 아니어야 할까? – Ferruccio

+0

오케이 그래서 색상 매개 변수를 proto에서 SDWORD로 지정 하시겠습니까? – TryChick

관련 문제