Home > other >  8086 MSDOS Assembly: Strange mouse behaviour in simple paint program
8086 MSDOS Assembly: Strange mouse behaviour in simple paint program

Time:07-17

I have a program written in 8086 targeting MSDOS which is supposed to draw a pixel under the mouse pointer whenever the left mouse button is pressed. It's like a primitive paint program.

data segment

ends

stack segment
    dw   128  dup(0)    
ends

code segment
start:
    mov ax, data
    mov ds, ax
    
    ; Set video mode to 640x400
    mov al, 12h
    mov ah, 0
    mov bh, 0
    int 10h
    
    ; initialize mouse
    mov ax, 0
    int 33h
                   
    cmp ax, 0             
    je end
    
    ; Show mouse pointer
    mov ax, 1
    int 33h
    
    ; Limit mouse speed (doesn't fix a thing)
    mov cx, 16
    mov dx, 32
    mov ax, 0fh
    int 33h
    
    main:
    mov bx, 0
    ; Get mouse status
    mov ax, 3
    int 33h
    cmp bx, 0
    je mouse_skip ; mouse did nothing go back to main
    cmp bx, 1
    jne mouse_skip ; if anything else other than m1 pressed go back to main
    mov al, 1100b  ; <-- m1 pressed, draw pixel under mouse pointer
    mov ah, 0ch
    int 10h
    mouse_skip:
    jmp main ; repeat
    end:
    ; return control to the operating system 
    mov ah, 4ch
    mov al, 0
    int 21h

The pixels have very apparent and visible gaps in between them. It fails to draw a lot of pixels (I think like 5/6 are not drawn). Is this because it's too busy doing all the mouse stuff so it doesn't care for the 10h interrupt? Or am I just missing something crucial?

CodePudding user response:

to draw a pixel under the mouse pointer

You are supposed to remove the mouse arrow from the screen prior to plotting a pixel! Most of your plots are lost when the mouse handler restores the screen prior to moving the mouse position and drawing a new arrow.
The few pixels that you did get, originate from the fact that due to the asynchronicity of the mouse operation, sometimes the arrow already moved away from the action point before you plot the pixel.

    ; Show mouse pointer
    mov  ax, 1
    int  33h

main:
    ; Get mouse status
    mov  ax, 3
    int  33h
    and  bx, 1
    jz   main

    ; Hide mouse pointer
    mov  ax, 2
    int  33h

    ; Plot action point
    mov  ax, 0C0Ch
    int  10h

    ; Show mouse pointer
    mov  ax, 1
    int  33h

    jmp  main

CodePudding user response:

The mouse position will always be discontinuous. The mouse hardware itself will usually have a refresh rate between 125Hz and 1000Hz (for USB mouses). There is no realistic way to refresh fast enough to keep up with any mouse speed, and this is not desirable since handling such a rate would consume too much resources for negligible gain.

If you want continuous data, you need to interpolate the mouse positions yourself, for example by drawing a line between each position, instead of just the positions themselves. This is what MS Paint does, you can see the straight lines if you move the mouse fast enough in a curve.

  • Related