Getting an access violation when trying to populate map using C++ and MASM

1

The overall purpose of my project is to read keyboard input via MASM using Irvine's library, passing that input to a C++ file, and then display a mock-up of my keyboard showing which key is being pressed. I'm using a map in C++ to hold the virtual key codes that Irvine reads, as well as string representations of the associated key. I'm not sure what the issue is. My professor tried running similar code using GAS assembly and that worked. Here are my two files:

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

The specific error I get is:

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

I really just have no idea what to do. My professor believes it's a stack issue. I think it may have something do with using global variables because when I define the map inside populateKeyboardKeysMap() instead of globally, the program seems to execute. I have not included the function to display the keyboard using the map for the sake of clarity, which is why the code I'm showing is incomplete as far as the intended purpose is concerned.

c++
assembly
keyboard
masm
irvine32
asked on Stack Overflow Apr 26, 2018 by Jacob Beckerman • edited Apr 26, 2018 by Jacob Beckerman

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0