flat assembler
Message board for the users of flat assembler.

Index > Windows > TUTs 'Test Department' style

Author
Thread Post new topic Reply to topic
asmgges



Joined: 17 Jun 2003
Posts: 86
Location: France
asmgges
Hi ! Very Happy
TUTs 'Test Department' style

I began to make tuts on Fasm in the style of "Test Department".
(I want moreover to honour him here for the remarkable work that it made for the programming in assembler low-level.)
My tuts are very detailed and a maximum of comments for a better understanding of the future programmer but also an assistant memory for the confirmed programmers.
Then I am going to translate them into french because very little of tuts in french for assembler and beginners demand is big.

your comments are welcome

here exemple and .exe
Code:
;===========================================================================================
; Win_2.asm  Fasm assembler 165.5
; Window in the middle of the screen
; Programmed by AsmGges France @2006
;===========================================================================================

format PE GUI 4.0
entry start

;===========================================================================================
; Inclusion of macros/equates/structures necessary for the program
;-- -----------------------------------------------------------------------------------------
  include '..\..\include\win32a.inc'

;-------------------------------------------------------------------------------------------
; Some symbolic constants
;-------------------------------------------------------------------------------------------
  ;for WindowProc CallBack Procedure
  wp1_hwnd   equ ebp+8                      ;handle of window who receives message
  wp1_wmsg   equ ebp+12                     ;the message number
  wp1_wparam equ ebp+16                     ;extra info about the message
  wp1_lparam equ ebp+20                     ;extra info about the message

  d equ dword                               ;more short... for my laziness

;===========================================================================================
; Definitions of strings / variables initialized or not initialized / structures
;-------------------------------------------------------------------------------------------
section '.data' data readable writeable

  Title     db ' AsmGges Fasm assembler: Window in the middle ',0
  ClassName db ' AsmGges Win32',0

  align 4                  ;after the strings on 8bits alignment of datas on 32bits

  WinX  dd 0               ;X coordinate left relative of the window
  WinY  dd 0               ;Y coordinate top relative of the window
  WinW  dd 512             ;X coordinate right relative of the window
  WinH  dd 352             ;Y coordinate bottom relative of the window

  hWnd  dd ?               ;handle of window

;-------------------------------------------------------------------------------------------
; For structures one can use notation:
; wc WNDCLASS
; I chose otherwise as more clear and understanding in the statements
;-------------------------------------------------------------------------------------------
wc:                        ;WNDCLASS structure (API=RegisterClass)
  wc.style          dd ?   ;window style
  wc.lpfnWndProc    dd ?   ;address of user proc function
  wc.cbClsExtra     dd ?   ;extra bytes to allocate set to 0
  wc.cbWndExtra     dd ?   ;extra bytes class directive, rc file
  wc.hInstance      dd ?   ;program handle
  wc.hIcon          dd ?   ;handle of icon
  wc.hCursor        dd ?   ;handle of cursor
  wc.hbrBackground  dd ?   ;background color
  wc.lpszMenuName   dd ?   ;name of menu class in resource file
  wc.lpszClassName  dd ?   ;name of windows this window class

msg:                       ;WMG structure (API=GetMessage)
  msg.hwnd          dd ?   ;handle of window who receives message
  msg.message       dd ?   ;the message number
  msg.wParam        dd ?   ;extra info about the message
  msg.lParam        dd ?   ;extra info about the message
  msg.time          dd ?   ;time the message was posted
  msg.pt_x          dd ?   ;cursor x-position, POINT structure
  msg.pt_y          dd ?   ;cursor y-position, POINT structure

;===========================================================================================
; The code area starts here
;-------------------------------------------------------------------------------------------
section '.code' code readable executable

start:
    ;---------------------------------------------------------------------------------------
    ; API GetModuleHandle give program handle
    ;---------------------------------------------------------------------------------------
    push 0                                  ;lpModuleHandle, 0=get program handle
    invoke GetModuleHandle                  ;- API Function -
    mov [wc.hInstance],eax                  ;eax = program handle

    ;---------------------------------------------------------------------------------------
    ; API 'LoadIconA' loads an icon defined in the resource file and store the
    ; handle in the 'WNDCLASSEX' structure
    ;---------------------------------------------------------------------------------------
    push IDC_FRANCE                         ;icon resource id
    push [wc.hInstance]                     ;program handle
    invoke LoadIcon                         ;- API Function -
    mov [wc.hIcon],eax                      ;eax = handle of newly loaded icon

    ;---------------------------------------------------------------------------------------
    ; API 'LoadCursorA' loads a default system cursor, in this case we must set
    ; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
    ; Then we store the cursor handle in the 'WNDCLASSEX' structure
    ;---------------------------------------------------------------------------------------
    push IDC_ARROW                          ;lpCursorName,default value in dezimal
    push 0                                  ;hInstance, 0=default system cursor
    invoke LoadCursor                       ;- API Function -
    mov [wc.hCursor],eax                    ;eax = handle of the cursor

    ;---------------------------------------------------------------------------------------
    ; API 'GetSystemMetrics' retrieves various system metrics and system configuration
    ; settings. Get Width of the screen.
    ;---------------------------------------------------------------------------------------
    push SM_CXSCREEN                        ;get width of the screen.
    invoke GetSystemMetrics                 ;- API Function -
    sub eax,[WinW]                          ;subtract window width from screen width
    shr eax,1                               ;to divide the rest by 2
    mov [WinX],eax                          ;result = coordinated X left of the window

    ;---------------------------------------------------------------------------------------
    ; API 'GetSystemMetrics' retrieves various system metrics and system configuration
    ; settings. Get Height of the screen.
    ;---------------------------------------------------------------------------------------
    push SM_CYSCREEN                        ;get height of the screen.
    invoke GetSystemMetrics                 ;- API Function -
    sub eax,[WinH]                          ;subtract window height from screen height
    shr eax,1                               ;to divide the rest by 2
    mov [WinY],eax                          ;result = coordinated Y up of the window

    ;---------------------------------------------------------------------------------------
    ; The API function 'RegisterClass' registers a window class
    ; This API needs a 'WNDCLASS' structure so we fill it with correct values
    ;---------------------------------------------------------------------------------------
    mov [wc.style],0                        ;window style
    mov [wc.lpfnWndProc],WindowProc         ;address of user lpfnWndProc function
    mov [wc.cbClsExtra],0                   ;extra bytes to allocate set to 0
    mov [wc.cbWndExtra],0                   ;class directive in rc file
    mov [wc.hbrBackground],2                ;background color
    mov d[wc.lpszMenuName],0                ;menu ID in resource file
    mov d[wc.lpszClassName],ClassName       ;name of windows class

    ;---------------------------------------------------------------------------------------
    ; Now, after filled the 'WNDCLASS' structure we call API 'RegisterClass'
    ;---------------------------------------------------------------------------------------
    push wc                                 ;pointer to WNDCLASS structure
    invoke RegisterClass                    ;- API Function -

    ;---------------------------------------------------------------------------------------
    ; API 'CreateWindowEx' creates an overlapped, pop-up, or child window.
    ; The return value in EAX is the handle of the new window.
    ;---------------------------------------------------------------------------------------
    push 0                                  ;lpParam, extra pointer data 0=no data
    push [wc.hInstance]                     ;program handle
    push 0                                  ;hMenu, handle window menu 0=class menu
    push 0                                  ;hWndParent, handle parent window 0=no
    push [WinH]                             ;intnHeight, window height pixel
    push [WinW]                             ;intnWidth, window width pixel
    push [WinY]                             ;inty, vertical position window
    push [WinX]                             ;intx, horizontal position window
    push WS_OVERLAPPEDWINDOW+WS_VISIBLE     ;dwStyle, 0=no sysmenu/close buttons
    push Title                              ;pointer to window name
    push ClassName                          ;pointer to class name
    push WS_EX_CLIENTEDGE                   ;dwExStyle, extra window style 0=no
    invoke CreateWindowEx                   ;- API Function -
    mov [hWnd],eax                          ;eax = handle of window

    ;---------------------------------------------------------------------------------------
    ; API 'ShowWindow' function sets the specified window's show state.
    ;---------------------------------------------------------------------------------------
    push SW_SHOWDEFAULT                     ;nCmdShow, show state 1=SW_SHOWNORMAL
    push [hWnd]                             ;handle of window
    invoke ShowWindow                       ;- API Function -

    ;---------------------------------------------------------------------------------------
    ; API 'UpdateWindow' updates the area of the specified window by sending a
    ; WM_PAINT message to the window if the window's update region is not empty.
    ;---------------------------------------------------------------------------------------
    push [hWnd]                             ;handle of window
    invoke UpdateWindow                     ;- API Function -

 MessageLoop:
    ;---------------------------------------------------------------------------------------
    ; API 'GetMessage' retrieves a message & places it in the specified structure.
    ;---------------------------------------------------------------------------------------
    push 0                                  ;wMsgFilterMax, highest message value
    push 0                                  ;wMsgFilterMin, lowest message value
    push 0                                  ;hWnd, handle of window who gets msg.
    push msg                                ;lpMsg, pointer to MSG structure
    invoke GetMessage                       ;- API Function -
    or eax,eax                              ;check if return value=0 (exit)
    jz ExitProgram                          ;if return value is 0 go to LABEL

    ;---------------------------------------------------------------------------------------
    ; API 'TranslateMessage' translates key code into ASCII character messages
    ;---------------------------------------------------------------------------------------
    push msg                                ;lpMSG, pointer to msg structure
    invoke TranslateMessage                 ;- API Function - keyboard code

    ;---------------------------------------------------------------------------------------
    ; API 'DispatchMessage' function dispatches a message to a window procedure.
    ;---------------------------------------------------------------------------------------
    push msg                                ;lpMSG, pointer to msg structure
    invoke DispatchMessage                  ;- API Function -

    jmp MessageLoop                         ;check for message again, go to LABEL

 ExitProgram:
    ;---------------------------------------------------------------------------------------
    ; API ExitProcess terminate the program
    ;---------------------------------------------------------------------------------------
    push [wc.hInstance]                     ;program handle to exit
    invoke ExitProcess                      ;- API Function -

;===========================================================================================
; Windows proc
;-------------------------------------------------------------------------------------------
; ebp+8  wp1_hwnd   = handle of window who receives message
; ebp+12 wp1_wmsg   = the message number
; ebp+16 wp1_wparam = extra info about the message
; ebp+20 wp1_lparam = extra info about the message
;-------------------------------------------------------------------------------------------
; Use of pushad/popad because freedom to use all registers in this window procedure.
;-------------------------------------------------------------------------------------------
WindowProc:
    push ebp                                ;create stack frame
    mov ebp,esp                             ;
    pushad                                  ;push all register to the stack

    cmp d[wp1_wmsg],WM_DESTROY              ;check if value=2 (WM_DESTROY)
    jne .defwindowproc                      ;if not 2 go to LABEL
    ;---------------------------------------------------------------------------------------
    ; API 'PostQuitMessage' indicates to Windows a request to terminate
    ;---------------------------------------------------------------------------------------
    push 0                                  ;nExitCode, exit code=msg.wparam
    invoke PostQuitMessage                  ;- API Function -
    jmp .finish                             ;go to LABEL

 .defwindowproc:
    ;--------------------------------------------------------------------------------------
    ; API 'DefWindowProcA' calls the window procedure to provide default processing
    ; for any window messages that an application does not process.
    ; This function ensures that every message is processed.
    ; It is called with the same parameters received by the window procedure.
    ;--------------------------------------------------------------------------------------
    popad                                   ;pop all register from stack
    push d[wp1_lparam]                      ;extra info about the message
    push d[wp1_wparam]                      ;extra info about the message
    push d[wp1_wmsg]                        ;the message number
    push d[wp1_hwnd]                        ;handle of window who receives message
    invoke DefWindowProc                    ;- API Function -

    mov esp,ebp                             ;delete stack frame
    pop ebp                                 ;
    ret 16                                  ;return and clear stack

 .finish:
    popad                                   ;pop all register from stack
    mov eax,0                               ;set eax to 0 to exit program
    mov esp,ebp                             ;delete stack frame
    pop ebp                                 ;
    ret 16                                  ;return and clear stack

;===========================================================================================
; Inclusion of files .dll and Windows Apis used
;-------------------------------------------------------------------------------------------
section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
            user32,'USER32.DLL'

  include '..\..\include\apia\kernel32.inc' ;apis Windows in dll
  include '..\..\include\apia\user32.inc'   ;apis Windows in dll

;===========================================================================================
; Inclusion of icons / cursors / bitmaps
;-------------------------------------------------------------------------------------------
section '.rsrc' resource data readable

  directory RT_ICON,icons,\
     RT_GROUP_ICON,group_icons

  resource icons,\
    1,LANG_NEUTRAL,icon_data

  IDC_FRANCE = 17                           ;id for icon

  resource group_icons,\
    IDC_FRANCE,LANG_NEUTRAL,main_icon

  icon main_icon,icon_data,'res\france.ico' ;icon filename

;===========================================================================================
;///////////////////////////////////////////////////////////////////////////////////////////
;===========================================================================================
    

Friendly...AsmGges


Description:
Download
Filename: TUT_GgesFasm.zip
Filesize: 5.07 KB
Downloaded: 72 Time(s)

Post 16 Jan 2006, 17:44
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
you could also teach some error handling. Newbies always forget that and then waste time looking for bugs
Post 16 Jan 2006, 18:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
asmgges



Joined: 17 Jun 2003
Posts: 86
Location: France
asmgges
Yes it is easy to add
Post 16 Jan 2006, 19:16
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.