OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:13 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Keyboard Interrupt Handler 16 bit
PostPosted: Sat Nov 05, 2022 3:22 am 
Offline

Joined: Sat Nov 05, 2022 2:55 am
Posts: 4
I'm having some issues with trying to get multiple instances of keyboard input (more than one key). I have looked at https://wiki.osdev.org/Babystep5 and various tutorials online. I'll keep the mostly relevant portions of my code down below. This is still in 16 bit mode. I am using qemu inside of WSL to test.

A few of the common sense self checks that I have done so far:
1. I am registering the handler (it gets called the first time!)
2. The handler does read the input it is being sent and sends the EIO back to the PIC.
3. Interrupts are enabled.
4. The main loop is just a hlt statement.

Code:
keyboard_handler: # 0x7d11
    push %ax
    push %cx

    in $0x60, %al # We read from i/o port 0x60, this is the key code
    mov %al, %cl

    in $0x61, %al
    mov %al, %ah
    or $0x80, %al
    xchg %al, %ah
    out %al, $0x61

    mov $0x20, %al
    out %al, $0x20

    and $0x80, %cl

    push $0x1d
    push $detect
    call print_string

    pop %bx
    pop %ax
    iret

main:
    xor %ax, %ax
    mov %ax, %ds
    mov %ax, %ss
    mov $0xFFFF, %bp
    mov %bp, %sp

    mov $0x09, %bx # The interrupt handler for keyboard
    shl $2, %bx # left shift 2 = multiply by 4
    cli
    movw $keyboard_handler, (%bx)
    mov %ax, 2(%bx)
    sti

    mov $0xB800, %ax
    movw $0x0000, %bx
    mov %ax, %gs
    call clear_screen

    push $0xFF
    push $hello # Put the parameter on the stack
    call print_string
    add $4, %sp

    push %ax
    push $hex_array + 2 # hex_array points to the first byte ('0'), but we will only overwrite the 3rd and 4th bytes.
    call convert_16_bit_num_to_hex_string
    add $4, %sp

    push $0x07
    push $hex_array
    call print_string
    add $4, %sp

end:
    hlt
    jmp end
    . = _start + 510
    .byte 0x55
    .byte 0xAA



Top
 Profile  
 
 Post subject: Re: Keyboard Interrupt Handler 16 bit
PostPosted: Sat Nov 05, 2022 11:46 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
So, one thing I see immediately is that at the beginning of the keyboard handler, you push AX and CX, and at the end you pop BX and AX. That is going to cause register corruption one the main program gets longer.

The second thing I notice is that after calling print_string in the main program, you increase SP by four, but you don't do that in the keyboard handler. Is it possible you are corrupting the stack? In that case, the IRET instruction would not return you to the HLT loop but somewhere else, and then everything would go off the rails from there.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Keyboard Interrupt Handler 16 bit
PostPosted: Sat Nov 05, 2022 5:45 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
thebayliff wrote:
Code:
    in $0x61, %al
    mov %al, %ah
    or $0x80, %al
    xchg %al, %ah
    out %al, $0x61

You're trying to manipulate a register that doesn't exist in any AT-compatible or newer PC, and you're doing it incorrectly. What exactly is your target hardware?


Top
 Profile  
 
 Post subject: Re: Keyboard Interrupt Handler 16 bit
PostPosted: Sat Nov 05, 2022 11:07 pm 
Offline

Joined: Sat Nov 05, 2022 2:55 am
Posts: 4
nullplan wrote:
So, one thing I see immediately is that at the beginning of the keyboard handler, you push AX and CX, and at the end you pop BX and AX. That is going to cause register corruption one the main program gets longer.

The second thing I notice is that after calling print_string in the main program, you increase SP by four, but you don't do that in the keyboard handler. Is it possible you are corrupting the stack? In that case, the IRET instruction would not return you to the HLT loop but somewhere else, and then everything would go off the rails from there.


This was exactly it. The stack corruption was messing me up. Changing the registers worked.

Octocontrabass wrote:
thebayliff wrote:
Code:
    in $0x61, %al
    mov %al, %ah
    or $0x80, %al
    xchg %al, %ah
    out %al, $0x61

You're trying to manipulate a register that doesn't exist in any AT-compatible or newer PC, and you're doing it incorrectly. What exactly is your target hardware?


This was me copying from the https://wiki.osdev.org/Babystep5 trying to see if I was missing something from there (hail mary's sometimes work). Now that I got it working, I'll remove the parts that aren't necessary.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], SemrushBot [Bot] and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group