我目前正在开发一个装配项目,遇到了一个问题,我有一个名为“mouse_click”的过程,我需要在项目中调用几次。 第一次调用该过程时,它按应有的方式工作,但第二次在“int 33h”行之后调用它时,bx 始终等于 1,并且它会跳过我检查鼠标是否被单击的部分。 这是代码:
proc mouse_click
mov ax,0h
int 33h ;resets the mouse
mov ax,1h
int 33h ;shows the mouse
not_pressed:
mov bx,0
mov ax,3h
int 33h
cmp bx, 01h ;checks if left mouse is pressed
jne not_pressed
;if left mouse is not pressed, it jumps to not_pressed and checks again if left mouse is pressed
shr cx, 1 ;devide the x coordinates in 2 to get correct x coordinates
mov [left_pressed_x], cx
;moves the x cordinates of the mouse to left_pressed_x when left button is pressed
mov [left_pressed_y], dx
;moves the y cordinates of the mouse to left_pressed_y when left button is pressed
mov ax, 2h
int 33h
;hides the mouse because if not the coordinates of the color will be the one on the outline of the mouse so the color would never change
mov bh, 0 ;sets page to zero
mov ah, 0Dh ; checks pixel color in mouse coordinates(left_pressed_x, left_pressed_y)
int 010h
;al = color
mov [left_pressed_color], al
; moves the color to left_pressed_color
add [left_pressed_color], '0'
;turns left_pressed_color from unicode into ascii
mov ax, 1
int 33h ;shows the mouse agaim
ret
endp mouse_click
我尝试在“int 33h”行之前和“ret”行之前重置bx,但每次到达“int 33h”行时bx都等于1。
每次调用
mouse_click
鼠标都会重置,因为 proc 以 mov ah,0 / int 33h
开头,bx
里面是按钮的数量。我添加了一些打印此内容的代码。
接下来我们有循环
not_pressed
。因此,如果没有按下任何按钮,您将看到 0
,如果按下人民币,您将看到 2
。如果您按 LMB,您将看到 1
并且代码停止,因为我添加了等待关键服务,之后其余代码将被执行,bx = 1
,程序结束。每次您打电话mouse_click
都会发生这种情况。
如果您决定将
mov ah,0 / int 33h
移到过程之外,鼠标将仅初始化一次。首先,代码将打印 number of buttons
,因为这是 bx
内部的内容,稍后 bx
将仅包含 1
。
IDEAL
MODEL small
STACK 100h
assume ds:data, cs:code
segment data
left_pressed_x dw ?
left_pressed_y dw ?
left_pressed_color db ?
pos_x dw ?
pos_y dw ?
status_text db "Number of buttons: ",13,10
db " Pressed:$"
ends
segment code
Start:
proc main
mov ax, data
mov ds,ax
mov ax,13h ; mode 13h, 320x200x256
int 10h
mov ax,0a000h ; es = video mem address
mov es,ax
;-----------------------
mov ah,02h ; move cursor
mov dh,0
mov dl,2
int 10h
mov ah,09h ; show status text
lea dx, status_text
int 21h
; mov ax,0h
;int 33h ; resets the mouse
main_loop:
call mouse_click
mov ah,01h ; check keyboard buffer
int 16h
jz no_keys
mov ah,00h
int 16h
cmp al,27 ; if ESC pressed, exit app
je quit
no_keys:
jmp main_loop
quit:
MOV AX, 3h
int 10h
mov ax, 4c00h
int 21h
endp main
proc mouse_click
mov ax,0h
int 33h ; resets the mouse
; bx = number of buttons
mov ah,02h ; position
mov dh,0
mov dl,21
int 10h
mov ah,02h ; print number of buttons
mov dl,bl
add dl,30h
int 21h
mov ax,1h
int 33h ;shows the mouse
not_pressed:
;mov bx,0
mov ax,3h ; mouse status
int 33h ; bx = button number
mov ah,02h ; position of button number
mov dh,1
mov dl,13
int 10h
mov ah,02h ; inside loop: 0 - nothing was pressed
mov dl,bl ; 2 - RMB
add dl,30h ; 3 - LMB + RMB
int 21h ; 4 - CMB
cmp bx, 01h ; we go outside if bx = 1, checks if left mouse is pressed
jne not_pressed
;if left mouse is not pressed, it jumps to not_pressed and checks again if left mouse is pressed
mov ah,02h ; position
mov dh,1
mov dl,13
int 10h
mov ah,02h ; print 1 and stop (wait for key press)
mov dl,bl ; just to see that LMB was pressed
add dl,30h ; after that bx is changed on enter to mouse_click
int 21h ; bx = number of mouse buttons
mov ah,08h
int 21h
shr cx, 1 ;devide the x coordinates in 2 to get correct x coordinates
mov [left_pressed_x], cx
;moves the x cordinates of the mouse to left_pressed_x when left button is pressed
mov [left_pressed_y], dx
;moves the y cordinates of the mouse to left_pressed_y when left button is pressed
mov ax, 2h
int 33h
;hides the mouse because if not the coordinates of the color will be the one on the outline of the mouse so the color would never change
mov bh, 0 ;sets page to zero
mov ah, 0Dh ; checks pixel color in mouse coordinates(left_pressed_x, left_pressed_y)
int 010h
;al = color
mov [left_pressed_color], al
; moves the color to left_pressed_color
add [left_pressed_color], '0'
;turns left_pressed_color from unicode into ascii
mov ax, 1
int 33h ;shows the mouse agaim
ret
endp mouse_click
ends ;code
end Start