Reading the mouse

In my last project I needed to use some test-coordinates before I generated a sine curve. The easiest way to do such thing is to read the mouse and as many people know that is a fairly easy task in for example Basic or other high level languages.

If you want to read the mouse in assembler you need to read $dff00a, which is a word and has the structure of Y position in bytes + X Position in bytes.

Well… This seems simple, just read it in and see if there is some change and then get the delta (Old Coordinate – New Coordinate). This might work, but what do you do if the counter loops?


  • Code was made in AsmOne 1.48 / AsmPro 1.18
  • Activate LimitMouse to ScreenWidth/ScreenHeight but setting MouseLimit to 1
  • Read the X Mouse and Y Mouse from the MouseX and MouseY label

The code:

		; -----------------------------
ScreenWidth		equ	320
ScreenHeight		equ	256

	IFND MouseLimit
		; 0 = Mouse Limit InActive
		; 1 = Mouse Limit Active (Default)
MouseLimit			equ	1


		; **** SOF Read Mouse Routine ****
		movem.l	d0-d2/a0-a1,-(sp)
		lea.l	OldDeltaY,a0
		lea.l	$dff00a,a1

		moveq	#0,d0
		move.b	(a1),d0		; Get New Y Mouse
		move.w	(a0),d1		; Get Old Y Mouse
		move.w	d0,(a0)		; Save New Y Mouse
		sub.w	d1,d0		; Delta Y

		moveq	#0,d1
		move.b	1(a1),d1	; Get New X Mouse
		move.w	2(a0),d2	; Get Old X Mouse
		move.w	d1,2(a0)	; Save New X Mouse
		sub.w	d2,d1		; Delta X
		; **** Check Y Delta ****

		cmp.w	#-127,d0
		bge	.NoUnderFlowY
		move.w	#-255,d2
		sub.w	d0,d2		; Delta Y = -255 - Delta Y
		bpl	.noYPos
		moveq	#0,d2

.noYPos:	move.w	d2,d0
		bra	.rmSkipY

.noUnderFlowY:	cmp.w	#127,d0
		ble	.rmSkipY
		move.w	#255,d2
		sub.w	d0,d2		; Delta X = 255 - Delta Y
		bmi	.noYNeg
		moveq	#0,d2
.noYNeg:	move.w	d2,d0


		; **** Check X Delta ****

		cmp.w	#-127,d1
		bge	.NoUnderFlowX
		move.w	#-255,d2
		sub.w	d1,d2		; Delta X = -255 - Delta X
		bpl	.noXPos
		moveq	#0,d2

.noXPos:	move.w	d2,d1
		bra	.rmSkipX

.noUnderFlowX:	cmp.w	#127,d1
		ble	.rmSkipX
		move.w	#255,d2
		sub.w	d1,d2		; Delta X = 255 - Delta X
		bmi	.noXNeg
		moveq	#0,d2
.noXNeg:	move.w	d2,d1


		lea.l	MouseY,a0

		move.w	(a0),d2		; D2.W = Old Y Mouse 
		add.w	d0,d2		; Y Mouse = Y Mouse + Y Delta

	IF MouseLimit
		bpl	.yPositive

		moveq	#0,d2
.yPositive:	cmp.w	#ScreenHeight-1,d2	; Y Mouse > Screen Height?
		ble	.yBelow
		move.w	#ScreenHeight-1,d2
		move.w	d2,(a0)+	; Save Y Mouse

		move.w	(a0),d2		; D2.W = Old X Mouse
		add.w	d1,d2		; X Mouse = X Mouse + X Delta
	IF MouseLimit
		bpl	.xPositive

		moveq	#0,d2
.xPositive:	cmp.w	#ScreenWidth-1,d2	; X Mouse > Screen Width?
		ble	.xBelow
		move.w	#ScreenWidth-1,d2
		move.w	d2,(a0)+

		movem.l	(sp)+,d0-d2/a0-a1

		; **** EOF Read Mouse Routine ****

OldDeltaY:	dc.w	0
OldDeltaX:	dc.w	0
MouseY:		dc.w	0
MouseX:		dc.w	0

		; -----------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2024