0

I'm doing homework and trying to read keys from keyboard. I tried to use 16h interrupt and started with this code

int main(){
    char key, shiftKey(2);
    asm ("movb %1, %%al;"
         "int $0x16;"
         "movb %%al, %0;"
    :"=r"(key)
    :"r"(shiftKey)
    :"%al"
    );
    std::cout << int(key);
}

Assembly algorithm is:

  1. pass function number (02) to al register movb 2, al
  2. call interruption int 16h
  3. al now should have mask of all keys like shift, capslock etc.

This code crashes with Process finished with exit code -1073741819 (0xC0000005). So I have no proper info what have I done wrong.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sashaaero
  • 2,618
  • 5
  • 23
  • 41
  • 3
    Seems you are trying to compile this under Windows with G++? `int $0x16` is a BIOS call that is not available under Windows. – Michael Petch Sep 05 '18 at 00:27
  • @MichaelPetch Yes, you are right. Do you have any suggestions about other interruptions? – sashaaero Sep 05 '18 at 00:28
  • I'm not sure what you are really trying to do or how low level you are going but maybe something like: https://learn.microsoft.com/en-gb/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017 – Michael Petch Sep 05 '18 at 00:33
  • @MichaelPetch Assembly is the target. I use C++ only for output detailed info. You also can post your mark as an answer. It really is. – sashaaero Sep 05 '18 at 00:34
  • 3
    The only stable system-call ABI on Windows is through DLL functions, not `int` or `syscall` directly. [Windows system calls](https://stackoverflow.com/q/21074334). You can call DLL functions from asm, but I wouldn't recommend making function calls from *inline* asm. It's hard to get right. Better to write your whole program in asm, or whole functions at least. (Use a debugger if you want to see values of things while running instead of debug-print). GNU C inline asm is very unforgiving. – Peter Cordes Sep 05 '18 at 00:40
  • Ok. It's more clear now. Thank you. – sashaaero Sep 05 '18 at 00:51

0 Answers0