flat assembler
Message board for the users of flat assembler.

Index > Windows > SDL help

Author
Thread Post new topic Reply to topic
Alessio



Joined: 26 Sep 2003
Posts: 35
Location: Viterbo, Italy
Alessio
Hi,

I've written a stupid program to test sdl under fasm.
It works until it doesn't lost focus, then a stack overflow occurs on ntdll.dll
Why ?

This is my sdl.inc
Code:
; SDL.INC

SDL_INIT_TIMER =       0x00000001
SDL_INIT_AUDIO =       0x00000010
SDL_INIT_VIDEO =       0x00000020
SDL_INIT_CDROM =       0x00000100
SDL_INIT_JOYSTICK =    0x00000200
SDL_INIT_NOPARACHUTE = 0x00100000      ; Don't catch fatal signals
SDL_INIT_EVENTTHREAD = 0x01000000      ; Not supported on all OS's
SDL_INIT_EVERYTHING =  0x0000FFFF

SDL_SWSURFACE =  0x00000000    ;  /**< Surface is in system memory */
SDL_HWSURFACE =  0x00000001     ; /**< Surface is in video memory */
SDL_ASYNCBLIT =  0x00000004  ; /**< Use asynchronous blits if possible */

SDL_ANYFORMAT =  0x10000000      ; /**< Allow any video depth/pixel-format */
SDL_HWPALETTE =  0x20000000  ; /**< Surface has exclusive palette */
SDL_DOUBLEBUF =  0x40000000       ; /**< Set up double-buffered video mode */
SDL_FULLSCREEN = 0x80000000   ; /**< Surface is a full screen display */
SDL_OPENGL =    0x00000002     ; /**< Create an OpenGL rendering context */
SDL_OPENGLBLIT = 0x0000000A  ; /**< Create an OpenGL rendering context and use it for blitting */
SDL_RESIZABLE =  0x00000010          ;/**< This video mode may be resized */
SDL_NOFRAME =      0x00000020             ; No window caption or edge frame */

SDLK_0                      = 48
SDLK_1                  = 49
SDLK_2                  = 50
SDLK_3                  = 51

;------------------------------------------------------------------------------
;
; SDL_EVENTs STUFF
;
;------------------------------------------------------------------------------
SDL_RELEASED = 0
SDL_PRESSED =  1

;
SDL_NOEVENT = 0              ; Unused (do not remove)
SDL_ACTIVEEVENT = 1         ; Application loses/gains visibility
SDL_KEYDOWN = 2                 ; Keys pressed
SDL_KEYUP = 3                 ; Keys released
SDL_MOUSEMOTION = 4          ; Mouse moved
SDL_MOUSEBUTTONDOWN = 5        ; Mouse button pressed
SDL_MOUSEBUTTONUP = 6         ; Mouse button released
SDL_JOYAXISMOTION = 7                ; Joystick axis motion
SDL_JOYBALLMOTION = 8         ; Joystick trackball motion
SDL_JOYHATMOTION = 9             ; Joystick hat position change
SDL_JOYBUTTONDOWN = 10                ; Joystick button pressed
SDL_JOYBUTTONUP = 11               ; Joystick button released
SDL_QUIT = 12                     ; User-requested quit
SDL_SYSWMEVENT = 13            ; System specific event
SDL_EVENT_RESERVEDA = 14     ; Reserved for future use..
SDL_EVENT_RESERVEDB = 15 ; Reserved for future use..
SDL_VIDEORESIZE = 16             ; User resized video mode
SDL_VIDEOEXPOSE = 17               ; Screen needs to be redrawn
SDL_EVENT_RESERVED2 = 18        ; Reserved for future use..
SDL_EVENT_RESERVED3 = 19 ; Reserved for future use..
SDL_EVENT_RESERVED4 = 20 ; Reserved for future use..
SDL_EVENT_RESERVED5 = 21 ; Reserved for future use..
SDL_EVENT_RESERVED6 = 22 ; Reserved for future use..
SDL_EVENT_RESERVED7 = 23 ; Reserved for future use..

; Events SDL_USEREVENT through SDL_MAXEVENTS-1 are for your use
SDL_USEREVENT = 24

; This last event is only for bounding internal arrays
; It is the number of bits in the event mask datatype -- Uint32
SDL_NUMEVENTS = 32

SDL_ACTIVEEVENTMASK      = 1 shl SDL_ACTIVEEVENT
SDL_KEYDOWNMASK      = 1 shl SDL_KEYDOWN
SDL_KEYUPMASK            = 1 shl SDL_KEYUP
SDL_KEYEVENTMASK   = (1 shl SDL_KEYDOWN) or \
                   (1 shl SDL_KEYUP)
SDL_MOUSEMOTIONMASK      = 1 shl SDL_MOUSEMOTION
SDL_MOUSEBUTTONDOWNMASK = 1 shl SDL_MOUSEBUTTONDOWN
SDL_MOUSEBUTTONUPMASK = 1 shl SDL_MOUSEBUTTONUP
SDL_MOUSEEVENTMASK = (1 shl SDL_MOUSEMOTION) or \
                       (1 shl SDL_MOUSEBUTTONDOWN) or \
                   (1 shl SDL_MOUSEBUTTONUP)
SDL_JOYAXISMOTIONMASK    = 1 shl SDL_JOYAXISMOTION
SDL_JOYBALLMOTIONMASK      = 1 shl SDL_JOYBALLMOTION
SDL_JOYHATMOTIONMASK       = 1 shl SDL_JOYHATMOTION
SDL_JOYBUTTONDOWNMASK       = 1 shl SDL_JOYBUTTONDOWN
SDL_JOYBUTTONUPMASK        = 1 shl SDL_JOYBUTTONUP
SDL_JOYEVENTMASK     = (1 shl SDL_JOYAXISMOTION) or \
                     (1 shl SDL_JOYBALLMOTION) or \
                     (1 shl SDL_JOYHATMOTION) or \
                      (1 shl SDL_JOYBUTTONDOWN) or \
                     (1 shl SDL_JOYBUTTONUP)
SDL_VIDEORESIZEMASK        = 1 shl SDL_VIDEORESIZE
SDL_VIDEOEXPOSEMASK  = 1 shl SDL_VIDEOEXPOSE
SDL_QUITMASK         = 1 shl SDL_QUIT
SDL_SYSWMEVENTMASK  = 1 shl SDL_SYSWMEVENT

SDL_ALLEVENTS = 0xFFFFFFFF

;------------------------------------------------------------------------------
;
; STRUCTURES
;
;------------------------------------------------------------------------------

struct SDL_Rect
  x dw ?
  y dw ?
  w dw ?
  h dw ?
ends

;
struct SDL_Surface
  flags dd ?                        ; Read-only
  format dd ?                    ; Read-only
  w dd ?                         ; Read-only
  h dd ?                         ; Read-only
  pitch dw ?                     ; Read-only
  db 0,0
  pixels dd ?                        ; Read-write
  offset dd ?                   ; Private

  ; Hardware-specific surface info
  hwdata dd ?

  ; clipping information
  clip_rect SDL_Rect            ; Read-only
  unused1 dd ?                   ; for binary compatibility

  ; Allow recursive locks
  locked dd ?                    ; Private

  ; info for fast blit mapping to other surfaces */
  map dd ?                      ; Private

  ; format version, bumped at every change to invalidate blit maps
  format_version dd ?            ; Private

  ; Reference count -- used when freeing surface
  refcount dd ?            ; Read-mostly
ends

;
struct SDL_version
  major db ?
  minor db ?
  patch db ?
ends

;
struct SDL_keysym
  scancode db ?
  db 0,0,0
  sym dd ?
  mod dd ?
  unicode dw ?
  db 0,0
ends

; Application visibility event structure
struct SDL_ActiveEvent
  type db ?        ; SDL_ACTIVEEVENT
  gain db ?        ; Whether given states were gained or lost (1/0)
  state db ?        ; A mask of the focus states
ends

; Keyboard event structure
struct SDL_KeyboardEvent
  type db ?       ; SDL_KEYDOWN or SDL_KEYUP
  which db ?      ; The keyboard device index
  state db ?     ; SDL_PRESSED or SDL_RELEASED
  db 0
  keysym SDL_keysym
ends

; Mouse motion event structure
struct SDL_MouseMotionEvent
  type db ?      ; SDL_MOUSEMOTION
  which db ?       ; The mouse device index
  state db ?        ; The current button state
  db 0
  x dw ?        ; The X coordinates of the mouse
  y dw ?    ; The Y coordinates of the mouse
  xrel dw ? ; The relative motion in the X direction
  yrel dw ? ; The relative motion in the Y direction
ends

; Mouse button event structure
struct SDL_MouseButtonEvent
  type db ?    ; SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP
  which db ?      ; The mouse device index
  button db ?       ; The mouse button index
  state db ?        ; SDL_PRESSED or SDL_RELEASED
  x dw ?       ; The X coordinates of the mouse at press time
  y dw ?      ; The Y coordinates of the mouse at press time
ends

; Joystick axis motion event structure
struct SDL_JoyAxisEvent
  type db ?  ; SDL_JOYAXISMOTION
  which db ?     ; The joystick device index
  axis db ?      ; The joystick axis index
  db 0
  value dw ?     ; The axis value (range: -32768 to 32767)
ends

; Joystick trackball motion event structure
struct SDL_JoyBallEvent
  type db ?      ; SDL_JOYBALLMOTION
  which db ?     ; The joystick device index
  ball db ?      ; The joystick trackball index
  db 0
  xrel dw ? ; The relative motion in the X direction
  yrel dw ? ; The relative motion in the Y direction
ends

; Joystick hat position change event structure
struct SDL_JoyHatEvent
  type db ? ; SDL_JOYHATMOTION
  which db ?      ; The joystick device index
  hat db ?       ; The joystick hat index
  value db ?        ; The hat position value:
               ;   SDL_HAT_LEFTUP   SDL_HAT_UP       SDL_HAT_RIGHTUP
               ;   SDL_HAT_LEFT     SDL_HAT_CENTERED SDL_HAT_RIGHT
         ;   SDL_HAT_LEFTDOWN SDL_HAT_DOWN     SDL_HAT_RIGHTDOWN
             ; Note that zero means the POV is centered.

ends

; Joystick button event structure
struct SDL_JoyButtonEvent
  type db ?    ; SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP
  which db ?  ; The joystick device index
  button db ?    ; The joystick button index
  state db ?     ; SDL_PRESSED or SDL_RELEASED
ends

; The "window resized" event
; When you get this event, you are responsible for setting a new video
; mode with the new width and height.
struct SDL_ResizeEvent
  type db ? ; SDL_VIDEORESIZE
  db 0,0,0
  w dd ?     ; New width
  h dd ? ; New height
ends

; The "screen redraw" event
struct SDL_ExposeEvent
  type db ?      ; SDL_VIDEOEXPOSE
ends

; The "quit requested" event
struct SDL_QuitEvent
  type db ?  ; SDL_QUIT
ends

; A user-defined event type
struct SDL_UserEvent
  type db ?    ; SDL_USEREVENT through SDL_NUMEVENTS-1
  db 0,0,0
  code dd ?    ; User defined event code
  data1 dd ?       ; User defined data pointer
  data2 dd ?     ; User defined data pointer
ends

; If you want to use this event, you should include SDL_syswm.h
struct SDL_SysWMEvent
  type db ?
  db 0,0,0
  msg dd ?
ends

; General event structure
struct SDL_Event
  union
    type db ?
    active SDL_ActiveEvent
    key SDL_KeyboardEvent
    motion SDL_MouseMotionEvent
    button SDL_MouseButtonEvent
    jaxis SDL_JoyAxisEvent
    jball SDL_JoyBallEvent
    jhat SDL_JoyHatEvent
    jbutton SDL_JoyButtonEvent
    resize SDL_ResizeEvent
    expose SDL_ExposeEvent
    quit SDL_QuitEvent
    user SDL_UserEvent
    syswm SDL_SysWMEvent
  ends
ends
    


and this is my assembler code...

Code:
;------------------------------------------------------------------------------
;
; FORMAT
;
;------------------------------------------------------------------------------

format PE GUI 4.0

;------------------------------------------------------------------------------
;
; ENTRYPOINT
;
;------------------------------------------------------------------------------

entry start

;------------------------------------------------------------------------------
;
; INCLUDES
;
;------------------------------------------------------------------------------

include 'win32a.inc'
include 'sdl.inc'

;------------------------------------------------------------------------------
;
; CONSTANTS
;
;------------------------------------------------------------------------------

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
SCREEN_BPP = 8
SOLID_COLOR = 255

;------------------------------------------------------------------------------
;
; RDATA SECTION
;
;------------------------------------------------------------------------------

section '.rdata' data readable

  szProgramName db 'Mouse Pixel Test',0
  szDirectDrawLibrary db 'DDRAW.DLL',0
  szMouseMotion db 'mouse @ %d,%d',0
  szKeyPressed db 'keypressed is %s',0

;------------------------------------------------------------------------------
;
; UDATA SECTION
;
;------------------------------------------------------------------------------

section  '.udata' data readable writeable

  screen       dd ?
  pitch         dw ?
  pixels        dd ?
  event         SDL_Event
  buffer   rb 16

;------------------------------------------------------------------------------
;
; CODE SECTION
;
;------------------------------------------------------------------------------

section '.code' code readable executable

  start:
   invoke LoadLibrary,szDirectDrawLibrary
      or eax,eax
  jz @f
       invoke FreeLibrary,eax

  @@:
     invoke SDL_Init,SDL_INIT_NOPARACHUTE
        or eax,eax
  jnz exit
    invoke GetModuleHandle,NULL
 invoke SDL_SetModuleHandle,eax
      invoke SDL_Init,SDL_INIT_VIDEO
      or eax,eax
  jnz exit
    invoke SDL_WM_SetCaption,szProgramName,NULL
 invoke SDL_SetVideoMode,SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0
     or eax,eax
  jz finish
   mov [screen],eax
    mov ecx,[eax+SDL_Surface.pixels]
    movzx edx,[eax+SDL_Surface.pitch]
   mov [pitch],dx
      add edx,ecx
 mov [pixels],edx

  flip:
 invoke SDL_Flip,[screen]
    or eax,eax
  jnz finish

  idle:
       invoke SDL_PollEvent,event
  or eax,eax
  jz flip
     cmp [event.type],SDL_KEYDOWN
        je keydown
  cmp [event.type],SDL_MOUSEMOTION
    je mouse_motion
     cmp [event.type],SDL_MOUSEBUTTONDOWN
        je mouse_button_down
        cmp [event.type],SDL_QUIT
   je finish
   jmp idle

  keydown:
      movzx eax,word [event.key.keysym.sym]
       invoke SDL_GetKeyName,eax
   cinvoke sprintf,buffer,szKeyPressed,eax
     invoke SDL_WM_SetCaption,buffer,NULL
        jmp idle

  mouse_motion:
 movzx eax,word [event.motion.x]
     movzx ecx,word [event.motion.y]
     cinvoke sprintf,buffer,szMouseMotion,eax,ecx
        invoke SDL_WM_SetCaption,buffer,NULL
        jmp idle

  mouse_button_down:
    movzx eax,word [event.button.y]
     movzx edx,word [pitch]
      imul eax,edx
        movzx edx,word [event.button.x]
     add eax,edx
 mov edx,[pixels]
    mov byte[edx+eax],255
       jmp idle

  finish:
       invoke SDL_Quit,0

  exit:
        invoke ExitProcess,0

;------------------------------------------------------------------------------
;
; IDATA SECTION
;
;------------------------------------------------------------------------------

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
    msvcrt,'MSVCRT.dll',\
    sdl,'SDL.DLL'

  import kernel32,\
        GetModuleHandle,'GetModuleHandleA',\
     LoadLibrary,'LoadLibraryA',\
     FreeLibrary,'FreeLibrary',\
      ExitProcess,'ExitProcess'

  import msvcrt,\
       sprintf,'sprintf'

  import sdl,\
  SDL_Init,'SDL_Init',\
    SDL_SetModuleHandle, 'SDL_SetModuleHandle',\
     SDL_SetVideoMode,'SDL_SetVideoMode',\
    SDL_Flip,'SDL_Flip',\
    SDL_PollEvent,'SDL_PollEvent',\
  SDL_Quit,'SDL_Quit',\
    SDL_WM_SetCaption,'SDL_WM_SetCaption',\
  SDL_GetKeyName,'SDL_GetKeyName'
    


Thank you.
Post 21 Apr 2011, 11:47
View user's profile Send private message MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17447
Location: In your JS exploiting you and your system
revolution
If you are getting stack overflow then check for:

  • Calling CDECL functions with invoke
  • Incorrect number of arguments supplied to invoke or stdcall
Post 21 Apr 2011, 11:57
View user's profile Send private message Visit poster's website Reply with quote
Alessio



Joined: 26 Sep 2003
Posts: 35
Location: Viterbo, Italy
Alessio
...I've changed invoke to cinvoke for SDL calls and seems to works.
How to identify if functions are cdecl ?

Thank you.
Post 21 Apr 2011, 12:18
View user's profile Send private message MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17447
Location: In your JS exploiting you and your system
revolution
Alessio wrote:
How to identify if functions are cdecl ?
Read the instructions. And if the instructions are not available (or non-existent) then read the source. If no source, then disassemble and look for "retn" or "retn <value>".
Post 21 Apr 2011, 12:21
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.