1

I am trying to make somewhat a TSR program (background process) for MS-DOS in C. I want the program to print "It works" and kill itself when a user press F2. But my code doesn't work. When I start .exe dos freezes and even spamming F2 for 18.2 seconds (interval interruption) doesn't help. I'm new to low-level programming so I can't google solutions for something like this and I don't understand how do interruptions work. Information for something like this is really messy nowadays. Thank you in advance.

void interrupt (*handler)();
void interrupt newHandler();

int main(void) {
  handler = getvect(8);
  setvect(8, newHandler);
  keep(0,10000);
 
  return 0;
}

void interrupt newHandler() {
    input = getch();
    if(input == 60) { 
        printf("It works\n");
    }
    else {
        (*handler)();
    }
}

I've tried adding this method from here Checking if a key is down in MS-DOS (C/C++). But I don't understand how am I supposed to have 3Ch in a char variable.

unsigned char read_scancode() {
    unsigned char res;
    _asm {
        in al, 60h
        mov res, al
        in al, 61h
        or al, 128
        out 61h, al
        xor al, 128
        out 61h, al
    }
    return res;
}
  • @hyde Yes. A char variable can store only 1 character in it, can't it? It's only 1 byte – DolphieDude Apr 22 '22 at 15:14
  • 2
    Ah, I remember playing with this stuff like... 25 years ago? Made a screensaver TSR, which worked once and as a side effect screwed up my Norton Commander in some weird way and beyond repair. Still have no idea how that could happen. – Eugene Sh. Apr 22 '22 at 15:26
  • `getch()` needs to be called *twice* if the first call indicates a function or cursor control key. For F2 it returns `0` on the first call and `60` on the second call. Otherwise `60` is the `<` character. – Weather Vane Apr 22 '22 at 15:54
  • You can't use `getch()` in interrupt handler. Likely it is implemented via DOS function 07h and DOS functions aren't reenterable. As well as `printf()`. For handling keyboard in TSR you can setup INT 9h handler as shown in the answer by the link from your question. – dimich Jul 16 '22 at 03:23
  • You don't wanna do the scancode check in interrupt 8 (timer), you would instead want to use interrupt 9 (keyboard). – Thomas Kjørnes Mar 21 '23 at 22:48

0 Answers0