如何在 Assembly 中使用鼠标?

How to use mouse in Assembly?

我需要编写一个汇编代码,如果你点击屏幕上的特定图标,就会激活一个声音。(例如:钢琴) 怎么做?

我正在使用 Assembly 86 和 dosbox 0.74-3 如果有帮助。

这是创建钢琴的代码(5 个白色矩形) 我需要当鼠标点击其中一个矩形时,它会发出声音。

我知道如何发出声音,我只需要在单击图标时听到声音即可。 备注是希伯来语因为是我的母语

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
color db 12
Player_CoordinateIn_X dw 5
Player_CoordinateIn_Y dw 10 
Player_CoordinateIn_X1 dw 40
Player_CoordinateIn_X2 dw 75
Player_CoordinateIn_X3 dw 110
Player_CoordinateIn_X4 dw 145
Player_CoordinateIn_X5 dw 180
Player_CoordinateIn_X6 dw 215
Player_CoordinateIn_X7 dw 250

include "iconbox.asm"


; --------------------------
CODESEG
; ---------------------------------------

proc printIcon
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon

proc drawIcon
;פרוצדורה מקבלת מיקום ותמונה
;פרוצדורה מדפיסה תמונה במיקום המבוקש
iconOffset EQU [BP+14] ;מספר האייקון
iconWidth EQU [BP+12] ;רוחב האייקון
iconHeight EQU [BP+10] ;גובה האייקון
iconColor EQU [BP+8] ;צבע האייקון
iconX EQU [BP+6] ;מיקום ציר X
iconY EQU [BP+4] ;מיקום ציר Y

 add [bp],10
 push bp
 mov bp,sp
 ;PushAll
 mov si,0
 mov di,0
 mov bx,iconOffset
 mov dx,iconY
bigLoop: 
 cmp [byte ptr bx],0
 je afterPixel
 MOV AX,iconColor
 MOV AH,0Ch
 MOV CX,iconX
 ADD CX,SI 
 INT 10H
afterPixel:
 inc bx
 inc si
 cmp si,iconWidth
 jne bigLoop
 mov si,0
 inc dx
 inc di
 cmp di,iconHeight
 jne bigLoop
 ;PopAll
 pop bp
 ret 12
endp drawIcon

proc open_grafic
    mov ax,13h
    int 10h
    ret 
endp open_grafic
;------------------------------------
proc printIcon2
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X1] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon2
;--------------------------------------
proc printIcon1
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X2] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon1
;--------------------------------------
proc printIcon3
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X3] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon3
;--------------------------------------
proc printIcon4
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X4] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon4
;--------------------------------------
proc printIcon5
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X5] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon5
;--------------------------------------
proc printIcon6
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X6] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon6
;--------------------------------------
proc printIcon7
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X7] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon7
;--------------------------------------
start:
    mov ax, @data
    mov ds, ax
; -------------------------
call open_grafic
call printIcon
call printIcon1
call printIcon2
call printIcon3
call printIcon4
call printIcon5
call printIcon6
call printIcon7
    ; --------------------------
    mov ax,0h
int 33h
; Show mouse
mov ax,1h
int 33h
; Loop until mouse click
MouseLP :
mov ax,3h
int 33h
cmp bx, 01h ; check left mouse click
jne MouseLP
; Print dot near mouse location
shr cx,1 ; adjust cx to range 0-319, to fit screen
sub dx, 1 ; move one pixel, so the pixel will not be hidden by mouse
mov bh,0h
mov al,[color]
mov ah,0Ch
int 10h
; Press any key to continue
mov ah,00h
 int 16h
; Text mode
mov ax,3h
int 10h



exit:

end start

您的代码已经在等待左键单击:

MouseLP:
    mov     ax, 0003h   ; MOUSE.GetMousePosition
    int     33h         ; -> BX CX DX
    test    bx, 1       ; Is left button down?
    jz      MouseLP     ; No
    shr     cx, 1       ; Adjust cx to range 0-319, to fit screen

一旦点击到达,您就开始将您在 CX (X) 和 DX (Y) 中获得的坐标与您感兴趣的矩形的坐标进行比较:

您所有的矩形都具有相同的宽度 (25) 和高度 (150)。
下一个片段将检查鼠标是否在您的第一个矩形上方单击,其左上角位于 (Player_CoordinateIn_X1, Player_CoordinateIn_Y).

    mov     ax, [Player_CoordinateIn_X1]  ; UpperLeftCornerX
    cmp     cx, ax
    jb      Outside
    add     ax, 25-1                      ; LowerRightCornerX
    cmp     cx, ax
    ja      Outside
    mov     ax, [Player_CoordinateIn_Y]   ; UpperLeftCornerY
    cmp     dx, ax
    jb      Outside
    add     ax, 150-1                     ; LowerRightCornerY
    cmp     dx, ax
    ja      Outside
Inside:
    ... Make the corresponding sound!
Outside:
    ... Go check for another mouse click!

proc drawIcon
 add [bp],10
 push bp
 mov bp,sp

您的 drawIcon 过程以这条奇怪的指令开始 add [bp],10。它应该做什么?