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: 764
Location: Massachusetts, USA
bitshifter
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: 166
Location: Maryland, USA
rCX
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: 764
Location: Massachusetts, USA
bitshifter
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: 166
Location: Maryland, USA
rCX
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: 764
Location: Massachusetts, USA
bitshifter
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: 166
Location: Maryland, USA
rCX
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: 764
Location: Massachusetts, USA
bitshifter
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: 17279
Location: In your JS exploiting you and your system
revolution
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: 764
Location: Massachusetts, USA
bitshifter
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: 17279
Location: In your JS exploiting you and your system
revolution
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
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: 4633
Location: Argentina
LocoDelAssembly
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: 764
Location: Massachusetts, USA
bitshifter
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
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
@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
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: 1901
DOS386
> 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: 764
Location: Massachusetts, USA
bitshifter
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
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
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.