Easyman121
Easyman121

Reputation: 11

Checking if the right mouse button was clicked and then "unclicked" in Assembly

I have a relatively simple TASM program that needs to draw using left mouse button, show cursor coordinates and change colors on right mouse click. The problem is it skips over some colors if you hold the button. So I need to detect if we have clicked and then unclicked our mouse. Tried adding a function to check this behavior but on right click it crashes the program or makes only 1 pixel or doesn't do anything at all. The code I am working with right now is this

.model small
.stack 256
.data
.386
stat db ?
col  dw ?
row  dw ?
color db 3
flag db 1
rezult db '000$'
mult10 db 10
mesx db 'x =$'
mesy db 'y =$'
mes_er db 'мышь не установлена',13,10,'$'
mes    db 'пpогpамма завеpшена',13,10,'$'
mask_m dw 16 dup(0ffffh)
       dw 07c0h,0c60h,1830h,3018h
       dw 6ccch,6ccch,600ch,600ch
       dw 644ch,3398h,1860h,0c60h
       dw 07c0h,0000h,0000h,0000h
.code
cursor_on proc near
      mov ax,01
      int 33h
      ret
cursor_on endp

cursor_off proc near
      mov ax,2
      int 33h
      ret
cursor_off endp

mouse_stat proc near
      mov ax,3
      int 33h
      ret
mouse_stat endp

pixel   proc near
        call cursor_off
    mov ah,0Ch
    mov al,color
    mov bh,0
        int 10h
        call cursor_on
        ret
pixel   endp

speed proc near
     mov ax,000fh
     mov cx,64
     mov dx,64
     int 33h
     mov ax,0013h
     mov dx,64
     int 33h
     ret
speed endp

text_curc proc near
    mov ax,000AH
    mov bx,01
    int 33h
    ret
text_curc endp

write   proc near
    mov bx,3
cicle:
    idiv mult10
    add ah,30h
    mov [si+bx-1],ah
    mov ah,0
    dec bx
    jnz cicle
    mov ah,09
    mov dx,offset rezult
    int 21h
    ret
write   endp

go:  mov ax,@data
     mov ds,ax
     mov ax,0
     int 33h
     cmp ax,0
     jne ner
     jmp er
ner:
     mov ax,10h
     int 10h
     mov ax,9
     xor bx,bx
     xor cx,cx
     push ds
     pop es
     lea dx,mask_m
     int 33h
     call speed
     call cursor_on
     call mouse_stat
     mov col,cx
     mov row,dx

    mov ah,02
    mov bh,00
    mov dx,0000h
    int 10h
    mov ah,9
        lea dx,mesx
    int 21h  
    mov ah,02
    mov bh,00
    mov dx,0009h
    int 10h
    mov ah,9
        lea dx,mesy
    int 21h  

next:
    mov ah,02
    mov bh,00
    mov dx,0004h
    int 10h
    mov ax,col
    lea si,rezult
    call write
    mov ah,02
    mov bh,0
    mov dx,000Dh
    int 10h
    mov ax,row
    call write
    mov dx,row
    mov cx,col

    mov ah,1
    int 16h
    jz nosym    
    mov ah,0
    int 16h
    cmp al,27
    je ex
    
    mov ah,1
    int 16h
    cmp al,32
    je nextc

nosym:
     mov flag,0
     call mouse_stat
     mov stat,bl
     cmp col,cx
     je  rov_lab
     mov col, cx
     mov flag ,1
rov_lab:
     cmp row,dx
     je pix
      mov row ,dx
      mov flag,1
pix:
    mov bl,stat
    and bl,2
    cmp bl,2
    ;je nextc is what was in the original program instead of 'je check'
    je check
       mov bl,stat
       and bl,1
       cmp bl,1
       jne next
       cmp flag,0
       je next
       call pixel
       jmp next
check: ; tried to write it myself but it doesn't work the way it needs to
    mov ax, 06h
    mov bx, 01h
    int 33h
    cmp ax, 02h
    je nextc
        
ex:
      call cursor_off
      lea dx,mes
       jmp exit
nextc:
    inc color
    jmp next
er:
     lea dx,mes_er
exit:
      mov ax,3
      int 10h
      mov ah,9
      int 21h
      mov ax,4c00h
      int 21h

End go

Upvotes: 1

Views: 110

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

and change colors on right mouse click. The problem is it skips over some colors if you hold the button.

When the program detects a right mouse click you could start by running a tight loop that stops as soon as the right mouse button is no longer pressed. Then rotate the colors and continue the main program loop.

mouseNextColor:
  call mouse_stat     ; -> BX CX DX
  test bl, 2
  jnz  mouseNextColor ; Right button is still down
  inc  color
  jmp  next

 mov ah,1
 int 16h
 jz nosym 
 mov ah,0
 int 16h       ; -> AX
 cmp al,27
 je ex
 mov ah,1        <<<< ???
 int 16h         <<<< ???
 cmp al,32
 je nextc
nosym:

Apparently you additionally want to be able to rotate colors on the user pressing the space bar. You should remove the 2 lines that I marked above. The keyboard function 00h already retrieved the key and because the program runs so fast it is improbable that a second key is available. That second use of the keyboard function 01h will have returned with ZF=1, but it might also have destroyed AL which would render comparing to 32 pointless.

  mov  ah, 01h
  int  16h            ; -> AX ZF
  jz   nosym    
  mov  ah, 00h
  int  16h            ; -> AX
  cmp  al, 27         ; <ESC>
  je   ex
  cmp  al, 32         ; <SPC>
  je   keybNextColor
nosym:

  ...

pix:
  test stat, 2        ; Right button ?
  jnz  mouseNextColor ; Yes
  test stat, 1        ; Left button ?
  jz   next           ; No
  cmp  flag, 0
  je   next
  call pixel
  jmp  next
ex:
  call cursor_off
  lea  dx,mes
  jmp  exit
mouseNextColor:
  call mouse_stat     ; -> BX CX DX
  test bl, 2
  jnz  mouseNextColor ; Right button is still down
keybNextColor:
  inc  color
  jmp  next

Upvotes: 1

Related Questions