【问题标题】:how to get mouse position in assembly (TASM)如何在装配中获得鼠标位置(TASM)
【发布时间】:2018-06-23 13:31:22
【问题描述】:

我尝试在汇编 (TASM) 中做一个计算器,为此我需要获取鼠标位置 (Location)。我在另一个问题中看到有人写道,水平位置(x)在 cx 中,垂直位置(y)在 dx 中,但是当我运行我的软件时,我看不到 2 个鼠标位置(在 cx 和 dx 中)之间的差异。 有人知道为什么吗? 还有另一个问题 - 我如何确定文本的位置? 谢谢

这是代码:

IDEAL
MODEL small
STACK 1000h
DATASEG

; --------------------------
; Your variables here

;black dw 0
color1 db 12
    ;1-blue ,2-green ,3-azure ,4-red ,5-pink ,6-orange ,7-white ,8-gray ,9-purple
filename db 'test.bmp',0
filehandle dw ?
Header db 54 dup (0)
Palette db 256*4 dup (0)
ScrLine db 320 dup (0)
ErrorMsg db 'Error', 13, 10,'$'

; --------------------------
CODESEG
proc OpenFile
; Open file
mov ah, 3Dh
xor al, al
mov dx, offset filename
int 21h
jc openerror
mov [filehandle], ax

ret
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
ret
endp OpenFile

proc ReadHeader
; Read  BMP file header, 54 bytes
mov ah,3fh
mov bx, [filehandle]
mov cx,54
mov dx,offset Header
int 21h
ret
endp ReadHeader

proc ReadPalette
; Read BMP file color palette, 256 colors * 4 bytes (400h)
mov ah,3fh
mov cx,400h
mov dx,offset Palette
int 21h
ret
endp ReadPalette

proc CopyPal
; Copy the colors palette to the video memory registers
; The number of the first color should be sent to port 3C8h
; The palette is sent to port 3C9h
mov si,offset Palette
mov cx,256
mov dx,3C8h
mov al,0
; Copy starting color to port 3C8h
out dx,al
; Copy palette itself to port 3C9h
inc dx
PalLoop:
; Note: Colors in a BMP file are saved as BGR values rather than RGB.
mov al,[si+2] ; Get red value.
shr al,2 ; Max. is 255, but video palette maximal
; value is 63. Therefore dividing by 4.
out dx,al ; Send it.
mov al,[si+1] ; Get green value.
shr al,2
out dx,al ; Send it.
mov al,[si] ; Get blue value.
shr al,2
out dx,al ; Send it.
add si,4 ; Point to next color.
; (There is a null chr. after every color.)

loop PalLoop
ret
endp CopyPal

proc CopyBitmap
; BMP graphics are saved upside-down.
; Read the graphic line by line (200 lines in VGA format),
; displaying the lines from bottom to top.
mov ax, 0A000h
mov es, ax
mov cx,200
PrintBMPLoop:
push cx
; di = cx*320, point to the correct screen line
mov di,cx
shl cx,6
shl di,8
add di,cx
; Read one line
mov ah,3fh
mov cx,320
mov dx,offset ScrLine
int 21h
; Copy one line into video memory
cld ; Clear direction flag, for movsb
mov cx,320
mov si,offset ScrLine

rep movsb ; Copy line to the screen
;rep movsb is same as the following code:
;mov es:di, ds:si
;inc si
;inc di
;dec cx
;loop until cx=0
pop cx
loop PrintBMPLoop
ret
endp CopyBitmap

;----------------------------------------
start:
    mov ax, @data
    mov ds, ax
; --------------------------
; Your code here
; -----------------------
; Graphic mode
mov ax, 13h
int 10h
; Process BMP file
call OpenFile
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap

;Show mouse
mov ax,1h
int 33h
;mov ax, 0003h
;int 33h
; Loop until mouse click
MouseLP:
mov ax,3h
int 33h
cmp bx, 01h ; check left mouse click
mov ax, 0003h
int 33h
jne MouseLP

; Press any key to continue
mov ah,00h
int 16h
; Text mode
mov ax,3h
int 10h
    ; return to text mode
    ;mov ah, 0
    ;mov al, 2
    ;int 10h


exit:

mov ax, 4c00h
int 21h
END start

【问题讨论】:

  • 问题:(1)你是在DOS还是DOS模拟器中运行?你没说。 (2)你说检查cxdx的鼠标代码在哪里?
  • 要定位文本,假设您使用的是 DOS 或仿真,请使用 int 10h with AH=02H。这方面的文档都是在线的。
  • 我以为这是代码:(mov ax, 0003h int 33h) 我错了吗?如果我错了 - 代码是什么?我使用 dosbox 0.74
  • 查找documentation for int 33h。具体来说,该命令查询 鼠标 位置和按钮。您说的是完全不同的定位文本(setting text 光标位置)。
  • 如果我理解正确 - 我想我需要鼠标位置而不是定位文本。我需要找出是否单击了鼠标并找出任何数字的位置是什么,因为我需要知道用户想要单击哪个按钮。在这段代码中,我读到鼠标的位置应该在 cx 和 dx 中,但它不存在。

标签: assembly x86-16 tasm


【解决方案1】:

您永远不应该假设鼠标可用!在模拟器下通常不是。

谨慎的程序员首先检查中断向量 33h 是否指向某个地方,然后使用函数 AX=0000h 重置鼠标驱动程序。

  ...
  mov  ax, 0000h  ; reset mouse
  int  33h        ; -> AX BX
  cmp  ax, FFFFh
  jne  NoMouse
  mov  ax, 0001h  ; show mouse
  int  33h
MouseLP:
  mov  ax, 0003h  ; get mouse position and buttonstatus
  int  33h        ; -> BX CX DX
  test bx, 1      ; check left mouse click
  jz   MouseLP    ; Loop until mouse click
  ...

在这里您可以开始使用CXDX 的X 和Y 位置。

注意:即使您在 320x200 屏幕上工作,鼠标坐标也会返回相对 640x200。你需要通过写shr cx, 1来调整X位置。

【讨论】:

  • 我写了这个然后我得到了 cx 和 dx 的位置?
猜你喜欢
  • 2023-04-04
  • 2019-02-25
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多