Keyboard handler causes segfault (djgpp)
<p>Recently I decided, it would be funny, to code some simple MSDOS game. Needles to say, I need code for handling keyboard events.</p>

<p>This is what I came up with for testing:</p>

<pre><code>int i, c = 0;
for ( i = 0; i &lt; 10; i++ )
{
    asm
    (
        "mov $0x00, %%ah         \n"
        "mov $0x00, %%al         \n"
        "int $0x16               \n"
        //"jnz keydn             \n"
        //"mov $0x00, %%al       \n"
        //"keydn:                \n"
        "movw %%ax, (%0)         \n"
            : "=r"(c)
    );

    printf( "%d\n", c &amp; 0xFF );
}
</code></pre>

<p>The code is supposed to wait for keypress, and then print out ASCII value of the character.
And everything works as expected unless I press key like <kbd>backspace</kbd> or <kbd>esc</kbd> - then segmentation fault occurs.</p>

<p><a href="https://i.stack.imgur.com/77sKn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/77sKn.png" alt="enter image description here"></a></p>

<p>I'm unfamiliar with assembly, but really I can't figure out what may cause this error.</p>

<p>I compile with <code>djgpp</code>, and run executables in <code>DosBox</code></p>

<p>Everything is based on information provided here:</p>

<ul>
<li><a href="https://courses.engr.illinois.edu/ece390/books/artofasm/CH20/CH20-3.html" rel="nofollow noreferrer">https://courses.engr.illinois.edu/ece390/books/artofasm/CH20/CH20-3.html</a></li>
<li><a href="https://en.wikipedia.org/wiki/BIOS_interrupt_call" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/BIOS_interrupt_call</a></li>
</ul>

<p>Thank you in advance! :)</p>