;;Queries the keyboard for input and prints the resulting keycode
        ORG     100H        ;Tell the assembler to start at addr 0x100
START:  LD      HL,GREET    ;Load the start addr. of the banner text
        LD      B,15H       ;The length of the string
        LD      DE,0H       ;Start output in the top left corner
        CALL    84CDH       ;Call str_out
LOOP:   CALL    8881H       ;Call the char_in
        JR      NC,LOOP     ;If no key was pressed, jump back to scan
        CP      51H         ;Checks if the pressed key was "ON/Break"
        RET     Z           ;If the key was "ON" end the program.          
        LD      HL,BUF      ;Load the buffer addess
        CALL    BTOHEX      ;Convert the byte in A into a HEX-string and store it at BUF
        LD      B,2         ;Set the length of BUF
        LD      DE,15H      ;Position cursor in row 0, col 22
        CALL    84CDH       ;Print the Keycode
        JR      LOOP        ;Retrun to the loop
        RET                 ;End the program
;;Convert the contents of A into a hex-string and stores the result in the buffer pointed in HL
BTOHEX: PUSH    AF          ;Store A onto the stack for later use
        LD      B,4         ;Init a loop for shifting the register
LP1:    SRL     A           ;Shift the MSN down to the least significant Nibble
        DJNZ    LP1         ;Shift again, until all 4 bits where shifted.
        CALL    HEXDGT      ;Converts the LSN to hex
        INC     HL          ;Point HL to the next byte in buffer
        POP     AF          ;Retrieve the original A
        AND     0FH         ;Cut the first digit of the hex no.
        CALL    HEXDGT      ;Call HEXDIGIT again for the second digit.
        LD      HL,BUF      ;Restore the buffer address
        RET
;;Converts the least significant nibble in A into a ASCII hex-digit and stores the result
HEXDGT: CP      0AH         ;Check if the number is smaller than 10
        JR      C,SKIP      ;Skip the next instruction, if it is
        ADD     A,07H       ;If the value is >10, we need to skip the chars between '9' and 'A'
SKIP:   ADD     A,30H       ;Converts the number into its ASCII digit
        LD      (HL),A      ;Store the digit in the correct location.
        RET
GREET:  DB      'Test keyboard input: '
BUF:    DS      2           ;Define a 2 byte buffer to store the keycode in ASCII 
        END                 ;Mark the end of code for the assembler
