Difference between revisions of "Input"

From SizeCoding
Jump to: navigation, search
(Created page with "== Checking for ESCAPE == This is fairly easy: Monitor the keyboard scancode port for ESC which is #1, then deal with it: <syntaxhighlight lang=nasm> in al,60h...")
 
(Reading the Mouse)
 
(5 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
         jnz    mainloop        ;fall through if 0, do jump somewhere else if otherwise
 
         jnz    mainloop        ;fall through if 0, do jump somewhere else if otherwise
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Sometimes your <code>AH</code> register does not contain <code>00h</code> at any given time. In that case <code>dec ax</code> will never trigger the zero flag, so you would have to spend the extra byte and use <code>dec al</code>. Alternatively you can redesign your codeflow as to have <code>00h</code> in <code>AH</code> at least sometimes ;)
 +
 +
== Checking for '''any''' keypress ==
 +
 +
If you need to shave a byte, it might be possible in some environments to check for any keypress instead of just escape using less code:
 +
 +
<syntaxhighlight lang=nasm>
 +
        in      al,60h          ;read whatever is at keyboard port
 +
        das                    ;If non-zero, parity will be set
 +
        jp      mainloop        ;Jump if parity
 +
</syntaxhighlight>
 +
 +
An alternative if not running DOSBox:
 +
 +
<syntaxhighlight lang=nasm>
 +
        in      al,60h          ;read whatever is at keyboard port
 +
        aaa
 +
        jz      mainloop        ;Jump if zero
 +
</syntaxhighlight>
 +
 +
The above are somewhat flaky due to different hardware, VMs, and emulators not honoring flags correctly.
 +
 +
== The DOS alternative ==
 +
 +
In very small productions, it's sometimes impossible to use one of the above, since <code>AL</code> contains intermediate results or pixel values all the time. Luckily there is <code>int 16h</code> which checks for any key with the following code :
 +
 +
<syntaxhighlight lang=nasm>
 +
        mov      ah,1          ; subfunction AH = 1, check for key
 +
        int      16h
 +
        jz      mainloop      ; ZF set if no keystroke available
 +
</syntaxhighlight>
 +
 +
See also : [http://www.ctyme.com/intr/rb-1755.htm Ralf Browns Interrupt List : Int 16/AH=01h]
 +
 +
== Reading the Mouse ==
 +
 +
For more sophisticated interaction with the mouse, there is <code>int 33h</code>. The most important interactions :
 +
 +
* [http://www.ctyme.com/intr/rb-5957.htm show mouse cursor - Int 33/AX=0001h]
 +
* [http://www.ctyme.com/intr/rb-5959.htm return position and button status - Int 33/AX=0003h]
 +
* [http://www.ctyme.com/intr/int-33.htm general overview]
 +
 +
For a case study in putting this together in a tinyprog, [[Paint16b|a little "Paint" implementation]] is possible in 16 bytes.

Latest revision as of 14:16, 14 August 2016

Checking for ESCAPE

This is fairly easy: Monitor the keyboard scancode port for ESC which is #1, then deal with it:

        in      al,60h          ;read whatever is at keyboard port; looking for ESC which is #1
        dec     ax              ;if ESC, AX now 0
        jnz     mainloop        ;fall through if 0, do jump somewhere else if otherwise

Sometimes your AH register does not contain 00h at any given time. In that case dec ax will never trigger the zero flag, so you would have to spend the extra byte and use dec al. Alternatively you can redesign your codeflow as to have 00h in AH at least sometimes ;)

Checking for any keypress

If you need to shave a byte, it might be possible in some environments to check for any keypress instead of just escape using less code:

        in      al,60h          ;read whatever is at keyboard port
        das                     ;If non-zero, parity will be set
        jp      mainloop        ;Jump if parity

An alternative if not running DOSBox:

        in      al,60h          ;read whatever is at keyboard port
        aaa
        jz      mainloop        ;Jump if zero

The above are somewhat flaky due to different hardware, VMs, and emulators not honoring flags correctly.

The DOS alternative

In very small productions, it's sometimes impossible to use one of the above, since AL contains intermediate results or pixel values all the time. Luckily there is int 16h which checks for any key with the following code :

        mov      ah,1           ; subfunction AH = 1, check for key
        int      16h
        jz       mainloop       ; ZF set if no keystroke available

See also : Ralf Browns Interrupt List : Int 16/AH=01h

Reading the Mouse

For more sophisticated interaction with the mouse, there is int 33h. The most important interactions :

For a case study in putting this together in a tinyprog, a little "Paint" implementation is possible in 16 bytes.