0

I have a doubt because my code is not working. I'm implementing a bootloader that runs the code below, when I try to load it only works in a few cases, like I explain below. My code is the next one:

ChangeGameState:
    mov cx, 00H      ;Here I make a delay
    mov dx, 3240H    ;.
    mov ah, 86H      ;.
    int 15h          ;.

    jmp DetectKeyPress

DetectKeyPress:
    mov ah, 01h 
    int 16h 
    jz noKeyInBuffer 
    xor ah, ah 
    int 16h 
    jmp exitKeyPress 

    noKeyInBuffer:
        xor ax, ax 

    exitKeyPress:   
        jmp ProcessKey

ProcessKey:
    cmp ah, 0
    je ExitKey
    cmp ah, 'd'
    je ChangeDir1
    cmp ah, 's'
    je ChangeDir2
    cmp ah, 'a'
    je ChangeDir3
    cmp ah, 'w'
    je ChangeDir4

ExitKey:
    jmp ChangeGameState

ChangeDir1:
    ;DO SOMETHING1
    jmp ChangeGameState

ChangeDir2:
    ;DO SOMETHING2
    jmp ChangeGameState

ChangeDir3:
    ;DO SOMETHING3
    jmp ChangeGameState

ChangeDir4:
    ;DO SOMETHING4
    jmp ChangeGameState

Now, when I try to press keys S, A and W it doesn't works, only if I press D key.. Any idea of what's going on??

  • What do you see when you single-step it in BOCHS, or any other x86 simulator that has a built-in debugger? – Peter Cordes Aug 27 '16 at 07:32
  • Just the program stops.. Nothing happens – Arturo Salas Aug 27 '16 at 07:34
  • So what values are in what register? That's what the **debugger** that's built into BOCHS is for. – Peter Cordes Aug 27 '16 at 07:35
  • I installed Bochs and the code doesn't works since line #1.. – Arturo Salas Aug 27 '16 at 08:06
  • I can't even tell what you're trying to say. Does this help? http://bochs.sourceforge.net/doc/docbook/user/internal-debugger.html – Peter Cordes Aug 27 '16 at 08:08
  • No, but thank you so much @PeterCordes – Arturo Salas Aug 27 '16 at 08:12
  • Do you need to init your segment registers? I don't see you using any static memory, so the code you've shown is position-independent and shouldn't care what `ds` is set to. But maybe the "DO SOMETHING" blocks are having a problem? Also note that `jmp DetectKeyPress` is redundant, because it's jumping to the next instruction anyway. – Peter Cordes Aug 27 '16 at 08:15
  • I'm using "org 100h".. I saw that I don't need "jmp DetectKeyPress".. "DO SOMETHING2.3.4" it's ok, because is almost the same code than "DO SOMETHING1".. My code only jump to "ChangeDir1" if I press D key, if I press S, A or W it does't works... – Arturo Salas Aug 27 '16 at 08:25
  • So use a debugger to find out what's actually happening when it doesn't work. That's *by far* the best approach. Developing in asm without a debugger is like trying to build something blindfolded. It's worth taking the time now to learn how to use a debugger, because it will save you hours on many future problems you run into. – Peter Cordes Aug 27 '16 at 08:29
  • Ok, thank you so much @PeterCordes, I'll look for a debugger – Arturo Salas Aug 27 '16 at 08:32
  • BOCHS is the one that's normally recommended for this. See also other links in the [x86 tag wiki](http://stackoverflow.com/tags/x86/info), but there aren't many bootloader links. IDK if dosbox has a debugger or anything like that. – Peter Cordes Aug 27 '16 at 08:33
  • This code looks familiar lol – Michael Petch Aug 27 '16 at 09:44

1 Answers1

2

The problem is that int 16h returns the scancode in ah, and the ASCII character in al. So while your code is testing the scancode of the key, you should be testing the ASCII character.

So, in ProcessKey, change cmp ah, ... to cmp al, ....

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57