https://github.com/akkartik/mu/blob/main/102keyboard.subx

     1 # check keyboard for a key
     2 # return 0 on no keypress or unrecognized key
     3 #
     4 # We need to do this in machine code because Mu doesn't have global variables
     5 # yet (for the keyboard buffer).
     6
     7 == code
     8
     9 read-key:  # kbd: (addr keyboard) -> result/eax: byte
    10     # . prologue
    11     55/push-ebp
    12     89/<- %ebp 4/r32/esp
    13     # . save registers
    14     51/push-ecx
    15     # result = 0
    16     b8/copy-to-eax 0/imm32
    17     # ecx = keyboard
    18     8b/-> *(ebp+8) 1/r32/ecx
    19     81 7/subop/compare %ecx 0/imm32
    20     {
    21       75/jump-if-!= break/disp8
    22       # var buffer-byte-addr/ecx: (addr byte)
    23       8b/-> *Keyboard-buffer:read 1/r32/CL
    24       81 0/subop/add %ecx Keyboard-buffer:data/imm32
    25       # var next-key/eax: byte = *buffer-byte-addr
    26       8a/byte-> *ecx 0/r32/AL
    27       # if (next-key != 0) lock and remove from keyboard buffer
    28       81 7/subop/compare %eax 0/imm32
    29       {
    30         74/jump-if-= break/disp8
    31         fa/disable-interrupts
    32         c6 0/subop/copy-byte *ecx 0/imm8
    33         ff 0/subop/increment *Keyboard-buffer:read
    34         81 4/subop/and *Keyboard-buffer:read 0x0f/imm32
    35         fb/enable-interrupts
    36       }
    37       # return
    38       eb $read-key:end/disp8
    39     }
    40     # TODO: fake keyboard
    41 $read-key:end:
    42     # . restore registers
    43     59/pop-to-ecx
    44     # . epilogue
    45     89/<- %esp 5/r32/ebp
    46     5d/pop-to-ebp
    47     c3/return