尝试使用C ++和MASM填充地图时获取访问冲突

时间:2018-04-26 17:11:25

标签: c++ assembly keyboard masm irvine32

我的项目的总体目的是使用Irvine的库通过MASM读取键盘输入,将该输入传递给C ++文件,然后显示键盘的模型,显示正在按下哪个键。我正在使用C ++中的映射来保存Irvine读取的虚拟键代码,以及相关键的字符串表示。我不确定是什么问题。我的教授尝试使用GAS程序集运行类似的代码并且有效。这是我的两个文件:

C ++:

#include <iostream>
#include <map>
#include <inttypes.h>
#include <iterator>
#include <string>

using namespace std;

extern "C" void _asmMain();
void clearScreen();
extern "C" void populateKeyboardKeysMap();
extern "C" void startingBoard();

map<_Uint32t, string> keyboardKeys = map<_Uint32t, string>();

// Key is virtual key code (stored in dx), value is string representing key
void populateKeyboardKeysMap() {
   keyboardKeys.insert(pair <_Uint32t, string>(0x001B, "| ESC |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0070, " F1 |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0071, " F2 |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0072, " F3 |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0073, "  F4  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0074, "  F5  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0075, "  F6  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0076, "  F7  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0077, "  F8  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0078, "  F9  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0079, "  F10  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x007A, "  F11  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x007B, "  F12  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x002C, "  PRTSC  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x002D, "  INSERT  "));
   keyboardKeys.insert(pair <_Uint32t, string>(0x002E, "  DELETE   |\n")); // Newline for end of row

   keyboardKeys.insert(pair <_Uint32t, string>(0x00C0, "|   `~   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0031, "   1!   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0032, "   2@   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0033, "   3#   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0034, "   4$   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0035, "   5%   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0036, "   6^   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0037, "  7&  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0038, "  8*  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0039, "  9(  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0030, "  0)  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BD, "  -_  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BB, "  =+  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0008, "  BACKSPACE   |\n")); // Newline for end of row

   keyboardKeys.insert(pair <_Uint32t, string>(0x0009, "|  TAB   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0051, "   Qq   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0057, "   Ww   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0045, "   Ee   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0052, "   Rr   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0054, "   Tt   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0059, "   Yy   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0055, "   Uu   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0049, "   Ii   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004F, "   Oo   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0050, "   Pp  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00DB, "  {[  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00DD, "  }]  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00DC, "  |\\   \n")); // Newline for end of row

   keyboardKeys.insert(pair <_Uint32t, string>(0x0014, "|  CAPSLK  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x0041, "   Aa   |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0053, "   Ss  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0044, "  Dd  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0046, "  Ff  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0047, "  Gg  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0048, "  Hh  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004A, "  Jj  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004B, "  Kk  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004C, "  Ll  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BA, "  :;  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00DE, "  \"'  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x000D, "           ENTER            |\n")); // Newline for end of row

   keyboardKeys.insert(pair <_Uint32t, string>(0x00A0, "|     SHIFT     |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x005A, "   Zz  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0058, "   Xx  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0043, "  Cc  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0056, "  Vv  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0042, "  Bb  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004E, "  Nn  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x004D, "  Mm  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BC, "  <,  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BE, "  >.  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00BF, "  ?/  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00A1, "   SHIFT    |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x0026, "        UP        |\n")); // Newline for end of row

   keyboardKeys.insert(pair <_Uint32t, string>(0x00A2, "|  CTRL  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(NULL, "  FN  |")); // Irvine won't recognize solo key press, no MSDN entry found
   keyboardKeys.insert(pair <_Uint32t, string>(0x005B, "  WIN  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0012, "  ALT  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x0020, "              SPACE               |"));
   //keyboardKeys.insert(pair <_Uint32t, string>(0x0012, "  ALT  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x005D, "  OPT  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x00A3, "  CTRL  |")); // Irvine won't recognize solo key press, found on MSDN
   keyboardKeys.insert(pair <_Uint32t, string>(0x0025, "  LEFT  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0028, "  DOWN  |"));
   keyboardKeys.insert(pair <_Uint32t, string>(0x0027, "  RIGHT  |\n"));
}

int main() {
   _asmMain();
}

void startingBoard() {
   cout << "| ESC | F1 | F2 | F3 |  F4  |  F5  |  F6  |  F7  |  F8  |  F9  |  F10  |  F11  |  F12  |  PRTSC  |  INSERT  |  DELETE   |" << endl;
   cout << "|   `~   |   1!   |   2@   |   3#   |   4$   |   5%   |   6^   |  7&  |  8*  |  9(  |  0)  |  -_  |  =+  |  BACKSPACE   |" << endl;
   cout << "|  TAB   |   Qq   |   Ww   |   Ee   |   Rr   |   Tt   |   Yy   |   Uu   |   Ii   |   Oo   |   Pp  |  [{  |  ]}  |  \\|   |" << endl;
   cout << "|  CAPSLK  |   Aa   |   Ss  |  Dd  |  Ff  |  Gg  |  Hh  |  Jj  |  Kk  |  Ll  |  ;:  |  '\" |           ENTER             |" << endl;
   cout << "|     SHIFT     |   Zz  |   Xx  |  Cc  |  Vv  |  Bb  |  Nn  |  Mm  |  ,<  |  .>  |  /?  |   SHIFT    |        UP        |" << endl;
   cout << "|  CTRL  |  FN  |  WIN  |  ALT  |              SPACE               |  ALT  |  OPT  |  CTRL  |  LEFT  |  DOWN  |  RIGHT  |" << endl;
}

MASM:

include Irvine32.inc
include Macros.inc

populateKeyboardKeysMap PROTO C
startingBoard PROTO C

.DATA

.CODE
   _asmMain PROC
      push ebp
      mov ebp, esp

      mWriteLn "Escape to exit"
      call startingBoard
      call populateKeyboardKeysMap

      KeyPress:
         mov eax, 50
         call Delay

         xor eax, eax
         call ReadKey

         ; This code will be used once populating the map works
         ;push ebx
         ;push dx
         ;call displayKeyboard

         cmp dx, VK_ESCAPE
         jne KeyPress

      pop ebp
      exit
   _asmMain ENDP
END

我得到的具体错误是:

Unhandled exception at 0x010C70B7 in finalTwo.exe: 0xC0000005: Access violation reading location 0x00000004.

我真的不知道该怎么做。我的教授认为这是一个堆栈问题。我认为它可能与使用全局变量有关,因为当我在populateKeyboardKeysMap()中定义地图而不是全局时,程序似乎执行。为了清楚起见,我没有包含使用地图显示键盘的功能,这就是为什么我所展示的代码就预期目的而言是不完整的。

0 个答案:

没有答案