flat assembler
Message board for the users of flat assembler.

Index > DOS > INT 33 mouse demo

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 02 May 2009, 04:32
Hello
I learned how to use mouse interrupt 33.
There was no information around here to help.
I thought i would share this knowledge in a small demo.
Right now its 98 bytes but im trying to fit it in a 64 byte demo.
I may have to make it a bit dirty to fit in such small size...
The mouse moves a pixel in which you can draw with.
By left clicking the mouse button you can paint pixel.
By right clicking the mouse botton you can erase pixel.
Its cool how its implemented because we draw where the
mouse is but keep what color should be underneath it.
The buttons simply change the color underneath the mouse.
And when the mouse is moved away, the pixel has its new color.
Ok, enough blabbering, if you have any improvments, id like to hear them...
Code:
;-- ----------------------------------------------
; interrupt 33 mouse demo by bitshifter
;------------------------------------------------
use16
org 0x100

    start:
        ; set graphics video mode
        mov     ax,0x0013
        int     0x10
        ; setup mouse
        xor     ax,ax
        int     0x33
    mloop:
        ; get mouse state
        mov     ax,0x0003
        int     0x33
        shr     cx,0x01 ; adjust for mode 0x0013
        cmp     bx,0x01 ; is left button down?
        je      lbdown
        cmp     bx,0x02 ; is right button down?
        je      rbdown
        jmp     update
    lbdown:
        mov     [oldcolor],0x02 ; set color under mouse to green
        jmp     update
    rbdown:
        mov     [oldcolor],0x00 ; set color under mouse to black
    update:
        ; set pixel at old mouse position
        push    cx
        push    dx
        mov     cx,[oldposx]
        mov     dx,[oldposy]
        mov     al,[oldcolor]
        mov     ah,0x0C
        int     0x10
        pop     dx
        pop     cx
        ; save current mouse position
        mov     [oldposx],cx
        mov     [oldposy],dx
        ; save pixel at current mouse position
        mov     ah,0x0D
        int     0x10
        mov     [oldcolor],al
        ; paint pixel white at current mouse position
        mov     ax,0x0C0F ; HIBYTE(write pixel) LOBYTE(bk-fg color)
        int     0x10
        ; check for keypress
        mov     ah,0x01
        int     0x16
        jz      mloop
        ; set text video mode
        mov     ax,0x0003
        int     0x10
        ; return to operating system
        int     0x20
        ret

; These must be zero upon startup to ensure the pixel
; index is at top left and its color is set to black.
oldposx  dw 0x00
oldposy  dw 0x00
oldcolor db 0x00
    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.


Last edited by bitshifter on 05 May 2009, 13:04; edited 1 time in total
Post 02 May 2009, 04:32
View user's profile Send private message Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX 02 May 2009, 05:15
Hey, this is almost exactly what first asm program did.

Code:
        int     0x20
        ret
    


Last edited by rCX on 02 May 2009, 05:27; edited 2 times in total
Post 02 May 2009, 05:15
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 02 May 2009, 05:23
Sweet, now if only the next 32 bytes can be shaved just as easily... Razz

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 02 May 2009, 05:23
View user's profile Send private message Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX 02 May 2009, 05:28
If you are on a 386 or above this...
Code:
        push    cx
        push    dx
    

...can be replaced with "pusha" and this...

Code:
        pop     dx
        pop     cx 
    

... can be replaced with "popa" to trim off 2 more bytes.
Post 02 May 2009, 05:28
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 02 May 2009, 05:32
Wow, 4 bytes in under 4 minutes...
Do you have another 28 minutes?

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 02 May 2009, 05:32
View user's profile Send private message Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX 02 May 2009, 05:39
Hmm. I guess i'm out of any good ideas for now Cool
Post 02 May 2009, 05:39
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 02 May 2009, 05:48
Thanks for the help, you have been great.
(I cant expect to learn everything from one place, right...)

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 02 May 2009, 05:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20423
Location: In your JS exploiting you and your system
revolution 02 May 2009, 06:43
Well if you are serious about saving bytes then you have to be brutal:
Code:
...
        int     0x20
;        ret <--- not needed

; These might be zero upon startup to ensure the pixel
; index is at top left and its color is set to black.
; otherwise we get a random pixel somewhere with a random colour
oldposx  dw ? ;uninitialised random junk
oldposy  dw ? ;uninitialised random junk
oldcolor db ? ;uninitialised random junk    
6 more bytes, I think.
Post 02 May 2009, 06:43
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 02 May 2009, 09:16
Quote:
Well if you are serious about saving bytes then you have to be brutal:
So does this mean the general flow of the program is pretty good?
I think to shave this many bytes off it will have to be real dirty.
Maybe i leave it like it is and work on something a bit more usefull.
I just have a goal to try and use all of the bios interrupts in one way or another.

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 02 May 2009, 09:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20423
Location: In your JS exploiting you and your system
revolution 02 May 2009, 09:32
bitshifter wrote:
So does this mean the general flow of the program is pretty good?
Umm, well, no actually. You see, the flow is far too logical and sensible, and therefore uses too many bytes. To really get the full benefit of how to save bytes you have to make it all cryptic and really hard to follow and understand. Logical and sensible never saves bytes.
Post 02 May 2009, 09:32
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 02 May 2009, 10:25
1). Don't remove the final "ret", remove the "int 0x20", saves two bytes instead of one!
Post 02 May 2009, 10:25
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 02 May 2009, 15:52
Untested, not flexible and probably changes behavior a bit too:
Code:
    mloop:
        ; get mouse state
        mov     ax,0x0003
        int     0x33
        shr     cx,0x01 ; adjust for mode 0x0013

        and     bl, 0x03
        jz      update

        not     bl
        and     bl, 0x02

        mov     [oldcolor], bl

update:
    
Post 02 May 2009, 15:52
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 03 May 2009, 01:28
So to increase the mouse resolution i would have
to cheat by connecting the two points into a line?

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 03 May 2009, 01:28
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 03 May 2009, 19:58
Just for fun this is a DexOS ver
Code:
use32
org 400000h
jmp start
db 'DEX2'
start:
        ; setup call-table
  mov   edi,Functions
 mov   al,0             
     mov   ah,0x0a           
    int   40h
   ; setup mouse
       call  [ResetMouse]
  ; setup min-max XY
  xor   eax,eax
       mov   edx,eax
       mov   ax,319
        mov   dx,199
        call  [SetMouseMaxMinXY]
    ; set graphics video mode
   mov   ax,0x0013
     call  [RealModeInt10h]
    mloop:
    ; get mouse state
   call [GetMousePos]
  test BL,00000100b     ;left
 jnz   lbdown          ; 00000010b = center
  test BL,00000001b     ;right
        jnz   rbdown
        jmp  update
    lbdown:
      mov  [oldcolor],0x02 ; set color under mouse to green
       jmp  update
    rbdown:
      mov  [oldcolor],0x00 ; set color under mouse to black
    update:
    ; set pixel at old mouse position
   push  dx
    push  cx
    mov   cx,[oldposx]
  mov   dx,[oldposy]
  call  PutPix
        mov   al,[oldcolor]
 ;Note: fs the only zero based deseptor in DexOS.
        mov   byte[fs:edi],al
   pop   cx
    pop   dx
    ; save current mouse position
       mov   [oldposx],cx
  mov   [oldposy],dx
  ; save pixel at current mouse position
      call  PutPix
        mov   al, byte[fs:edi]
  mov   [oldcolor],al
 mov   byte[fs:edi],0x0f
 ; check for keypress
        call   [KeyPressedNoWait]
   cmp    al,0
 je     mloop
        ; set text video mode
       call   [SetDex4uFonts]
      ret

 ;----------------------------------------------------;
 ; PutPix                                             ;
 ;----------------------------------------------------;
PutPix:
     shl   ecx,16
        shr   ecx,16
        mov   edi,0xa0000   ;ScreenBuff
     mov   bx,dx
 shl   bx,8                  
        shl   dx,6                 
 add   bx,dx
 add   cx,bx                 
        add   edi,ecx   
    ret

; These must be zero upon startup to ensure the pixel
; index is at top left and its color is set to black.
oldcolor db 0x00
oldposx  dw 0x00
oldposy  dw 0x00

include "dex.inc"
    

I have made no attempt to optimizing the code, just convert.
Post 03 May 2009, 19:58
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 04 May 2009, 20:36
@bitshifter: Awesome! I was wondering how to make a mouse work, also I like
how you made the right and left clicks handle Smile

@Dex: what version of the assembler do you use, all of the DexOS apps I try
to compile and they never come out right Confused yes I included dex.inc

_________________
Want hosting for free for your asm project? You can PM me. (*.fasm4u.net)
Post 04 May 2009, 20:36
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 04 May 2009, 22:04
Coddy41 wrote:

@Dex: what version of the assembler do you use, all of the DexOS apps I try
to compile and they never come out right Confused yes I included dex.inc


That's too broad a statement for me to help you,
If for example you take the above code, make sure the Dex.inc is in the same dir
Then in window (you are using window ? )
Type in a command box
C:\fasm mouse.asm mouse.dex <enter>
Then put the file on a DexOS floppy and run the file from the cli by typing
mouse <enter>
Where is it you are having a problem ?

PS: I keep seing
start1:
in the header of your examples, that should not be there.
Post 04 May 2009, 22:04
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 05 May 2009, 12:31
> learned how to use mouse through bios.

NO you didn't.

> ------------------------------------------------
> ; bios driven mouse demo by bitshifter
> ;------------------------------------------------
> use16
> org 0x100

> ; return to operating system
> int 0x20
> ret

Does it boot into your PC ? Looks like a DOS COM executable Sad

> ; setup mouse
> xor ax,ax
> int 0x33

Here you talk to a mouse driver , NOT to the BIOS Sad
Post 05 May 2009, 12:31
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 05 May 2009, 13:02
Hey DOS386, your right about that.
(Stupid me thought it was bios int)
I change name to 'INT 33 mouse demo'

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 05 May 2009, 13:02
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 05 May 2009, 13:27
So it doesn't use bios? will this work without windows?
Post 05 May 2009, 13:27
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 05 May 2009, 17:00
Not unless you load a dos mouse driver.
Post 05 May 2009, 17:00
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.