Author Topic: QBasic Mouse Sub Procedure  (Read 4059 times)

0 Members and 1 Guest are viewing this topic.

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
QBasic Mouse Sub Procedure
« on: February 29, 2020, 02:34:52 am »
I found this good example of a classic QBasic Mouse Sub Procedure.

What makes it good is that the mnemonics for the machine code opcodes are given as comments so you can understand what is being done.
Also, the code below will compile and run in QB64.

This should be helpful to those who are examining older QBasic code containing mouse routines.

Code: QB64: [Select]
  1. ' QBasic Mouse Sub Procedure
  2. ' Source:  https://web.archive.org/web/20171227233136/http://staticchaos.awardspace.com/basic/qbmouse.php
  3. '
  4. ' If you just want a quick mouse routine for QBasic, then copy & paste the (below) code into a .BAS file
  5. ' and call the mouse SUB routine, passing it the number of the interrupt sub-function:
  6. '
  7. '    0 = initialize mouse
  8. '    1 = show mouse cursor
  9. '    2 = hide mouse cursor
  10. '    3 = get mouse co-ordinates and button status
  11. '
  12. ' Notes:
  13. ' 1. This works for graphics & text modes.
  14. ' 2. Typically, the mouse is initialized (sub-function 0) early in the program.
  15. ' 3. The mouse cursor should *always* be hidden (sub-function 2) when drawing any graphics to the screen
  16. '    to avoid distortion or erasure of the mouse cursor. The mouse cursor should be restored (sub-function 1)
  17. '    right after the graphic drawing statement(s).
  18. '    The mouse status (sub-function 3) procedure will return the cursor's column in cx and its row in dx.
  19. ' 4. The button states are returned in bx:
  20. '   1 = left mouse button is pressed down
  21. '   2 = right mouse button is pressed down
  22. '   3 = left+right mouse buttons are pressed down
  23. '   4 = middle mouse button is pressed down
  24. '
  25. '
  26. DEFINT A-Z
  27. DECLARE SUB mouse (ax AS INTEGER)
  28. DIM SHARED ax AS INTEGER: ax = 0
  29. DIM SHARED bx AS INTEGER: bx = 0
  30. DIM SHARED cx AS INTEGER: cx = 0
  31. DIM SHARED dx AS INTEGER: dx = 0
  32.  
  33. ' Your program code goes here...
  34. '===============================
  35. ' Test Driver
  36. CALL mouse(0)
  37. CALL mouse(1)
  38.     CALL mouse(3)
  39.     PRINT ax, bx, cx, dx
  40.  
  41. '===============================
  42. SUB mouse (ax AS INTEGER)
  43.     ml$ = "" ' -=<( Mouse Code )>=-
  44.     ml$ = ml$ + CHR$(&H55) ' push bp               ; preserve BP register
  45.     ml$ = ml$ + CHR$(&H89) + CHR$(&HE5) ' mov  bp, sp           ; copy SP to BP
  46.     ml$ = ml$ + CHR$(&HB8) + CHR$(ax) + CHR$(&H0) '   mov  ax, #          ;   copy SUBFUNCTION to AX
  47.     ml$ = ml$ + CHR$(&HCD) + CHR$(&H33) '   int  33             ;   call mouse interrupt
  48.     ml$ = ml$ + CHR$(&H53) '   push bx             ;   preserve BX (again)
  49.     ml$ = ml$ + CHR$(&H8B) + CHR$(&H5E) + CHR$(&H6) '   mov  bx, [bp+6]     ;   copy location of dx (last variable) to BX
  50.     ml$ = ml$ + CHR$(&H89) + CHR$(&H17) '   mov  [bx], dx       ;   copy DX to dx location in BX
  51.     ml$ = ml$ + CHR$(&H8B) + CHR$(&H5E) + CHR$(&H8) '   mov  bx, [bp+8]     ;   copy location of cx to BX
  52.     ml$ = ml$ + CHR$(&H89) + CHR$(&HF) '   mov  [bx], cx       ;   copy CX to cx location in BX
  53.     ml$ = ml$ + CHR$(&H8B) + CHR$(&H5E) + CHR$(&HC) '   mov  bx, [bp+C]     ;   copy location of ax to BX
  54.     ml$ = ml$ + CHR$(&H89) + CHR$(&HF7) '   mov  [bx], ax       ;   copy AX to ax location in BX
  55.     ml$ = ml$ + CHR$(&H8B) + CHR$(&H5E) + CHR$(&HA) '   mov  bx, [bp+A]     ;   copy location of bx to BX
  56.     ml$ = ml$ + CHR$(&H58) '   pop  ax             ;   restore int 33's BX value to AX
  57.     ml$ = ml$ + CHR$(&H89) + CHR$(&H7) '   mov  [bx], ax       ;   copy AX to bx location in BX
  58.     ml$ = ml$ + CHR$(&H5D) ' pop  bp               ; restore BP
  59.     ml$ = ml$ + CHR$(&HCA) + CHR$(&H8) + CHR$(&H0) ' retf 8                ; Return Far and skip 8 bytes of variables
  60.     DEF SEG = VARSEG(ml$) ' Set current segment this machine code segment
  61.     offset% = SADD(ml$) ' Set offset to this machine code location
  62.     CALL ABSOLUTE(ax, bx, cx, dx, offset%) ' The actual call to this machine code
  63.     DEF SEG ' Restore the default segment
  64.  

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: QBasic Mouse Sub Procedure
« Reply #1 on: February 29, 2020, 05:36:32 am »
This I have to share as it is the best write up of the INT 33 mouse support functions that I have found so far. Very clear and concise.

INT 33H: Mouse Support
http://www.techhelpmanual.com/832-int_33h__mouse_support.html


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: QBasic Mouse Sub Procedure
« Reply #2 on: March 04, 2020, 09:03:22 am »
Hi Eric
fine to see again Interrupt Table...

about this read here please https://www.qb64.org/wiki/INTERRUPT#Parameters
and here https://www.qb64.org/wiki/CALL_ABSOLUTE#Description


Thanks to share this example
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBasic Mouse Sub Procedure
« Reply #3 on: March 04, 2020, 11:21:32 am »
Quick question to save me from technical idiocy, do Interrupts work in a non Windows/DOS (Microsoft) OS.
« Last Edit: March 04, 2020, 11:23:08 am by bplus »

FellippeHeitor

  • Guest
Re: QBasic Mouse Sub Procedure
« Reply #4 on: March 04, 2020, 11:24:03 am »
QB64 emulates a few, so older programs can be loaded. For new programs, one should use _MOUSE* functions.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBasic Mouse Sub Procedure
« Reply #5 on: March 04, 2020, 11:25:29 am »
QB64 emulates a few, so older programs can be loaded. For new programs, one should use _MOUSE* functions.

Man you are fast! Thanks, that's what I was thinking.

Oh now I see it:
Quote
This should be helpful to those who are examining older QBasic code containing mouse routines.

OK
« Last Edit: March 04, 2020, 11:27:14 am by bplus »