assembly - Is this an overflow, or maybe more keyboard data? -


i writing bootloader, , it's functionality limited printing string, copying keyboard characters screen typed. while writing routines read , write key, noticed print routine not detecting null terminator in offset (plus) 1 of double word array stores typed key. right i'm resetting terminator, thought i'd ask happening here. line in question marked ; line.

                bits    16                 org     0x7c00 start:          jmp     main  ; imported key blocking ; in: none ; out: ax bgetkey:                 pusha                 mov     ax, 0                 mov     ah, 10h                 int     16h                 mov     [.buf], ax                 popa                 mov     ax, [.buf]                 ret                 .buf    dw 0 ; end imported file  ; imported print string screen ; in:   ds->si ; out:  none prints:                 mov     ah, 0x0e                 mov     al, [si]                 cmp     al, 0                 jz      print_end                 mov     bh, 0x00                 mov     bl, 0x07                 int     0x10                 inc     si                 jmp     prints print_end:                 ret ; end imported file  main:                 mov     ax, 0x0000              ; clear ax ,                 mov     ds, ax                  ; data segment                 mov     si, welcome                 call    prints type:                 mov     si, qbuf                 call    bgetkey                 mov     [qbuf], ax                 mov     dword [qbuf + 1], 0      ; line                 call    prints                 jmp     type                 welcome db "moose os", 0x0a, 0x0d, 0x00                 newline db 0x0d, 0x0a, 0x00                 qbuf    dw 0, 0  times 0200h - 2 - ($ - $$)  db 0                 dw 0xaa55 

this output of typing "abcdefg" if comment out line in question:

undesired output correct size

this desired output line uncommented:

desired output

why must reset qbuf + 1?

the problem int 16h ah=00h returns ascii character code in al , scan code in ah. mov [qbuf], ax instruction stores both in buffer, int 10h ah=0eh prints ascii characters. ends interpreting scan code stored in buffer ascii character , displays on screen accordingly.

your mov dword [qbuf + 1], 0 statement fixes problem writing 4 0 bytes after first character in qbuf. overwrites scan code stored in second byte of qbuf. sets remaining 2 bytes 0 along 1 more byte beyond end of 4 byte long qbuf. after qbuf overwritten statement, fortunately there's nothing there.

what should doing this:

call    bgetkey mov     [qbuf], al mov     byte [qbuf + 1], 0   call    prints 

the second mov instruction isn't necessary in program now, byte 0. it's still idea though code doesn't break if end using qbuf else earlier in program.


Comments