flat assembler
Message board for the users of flat assembler.

Index > Windows > FASM "TEMPLATE.ASM" questions :?

Author
Thread Post new topic Reply to topic
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
hi,
i am back again with some questions regarding the example file, "TEMPLATE.ASM"
provided by FASM inside the FASMW version.

i created the lite version of it, below is the code
Code:
; template for program using standard Win32 headers
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable

     _title db 'Win32 program template',0
     _class db 'FASMWIN32',0

     mainhwnd dd ?
     hinstance dd ?

     msg MSG
     wc WNDCLASS

section '.code' code readable executable
     start:
          invoke    LoadCursor,0,IDC_ARROW
               mov  [wc.hCursor],eax    ;will see hourglass (busy)
                                        ;cursor if this line is clear
                                        ;some delay perhaps
               mov  [wc.lpfnWndProc],WindowProc
               mov  [wc.lpszClassName],_class
               mov  [wc.hbrBackground],COLOR_BTNFACE+1 ;will see transparent window
                                                       ;if this line is clear
          invoke    RegisterClass,wc
          invoke    CreateWindowEx,\
                    0,\
                    _class,\
                    _title,\
                    WS_VISIBLE+WS_DLGFRAME+WS_SYSMENU,\
                    128,128,\
                    192,192,\
                    NULL,\
                    NULL,\
                    [hinstance],\
                    NULL
               mov  [mainhwnd],eax

     msg_loop:
          invoke    GetMessage,msg,NULL,0,0
                    or   eax,eax
                    jz   end_loop
          invoke    TranslateMessage,msg
          invoke    DispatchMessage,msg
                    jmp  msg_loop

     end_loop:
          invoke    ExitProcess,[msg.wParam]

proc WindowProc, hwnd,wmsg,wparam,lparam
                    cmp  [wmsg],WM_DESTROY
                    je   wmdestroy
     defwndproc:
          invoke    DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
                    jmp  finish
     wmdestroy:
          invoke    PostQuitMessage,0
     finish:
          return
endp

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
       user,'USER32.DLL'

  import kernel,\
      ExitProcess,       'ExitProcess'

  import user,\
      RegisterClass,     'RegisterClassA',\
      CreateWindowEx,    'CreateWindowExA',\
      DefWindowProc,     'DefWindowProcA',\
      GetMessage,        'GetMessageA',\
      TranslateMessage,  'TranslateMessage',\
      DispatchMessage,   'DispatchMessageA',\
      LoadCursor,        'LoadCursorA',\
      PostQuitMessage,   'PostQuitMessage'
    


the questions are :
1. do we really need to INVOKE GetModuleHandle? coz lite version could run without it,
if we should INVOKE it, i wish somebody could please tell me why and why not to INVOKE it too?
Code:
;2. why we need to

        push    ebx esi edi

;in the actual "template.asm" -> proc WindowProc,hwnd,wmsg,wparam,lparam
;and pop them off in the finish:
    


3. if we use C language to create win32 application, we would run the WinMain function like below,
Code:
int PASCAL WinMain
       (HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,
  int nCmdShow)
    

how could we access to the hInstance, hPrevInstance, lpszCmdLine and nCmdShow in Win32 ASM mode?


sincerely,
vbVeryBeginner (:-|)>-|--<
http://sulaiman.thefreebizhost.com
Post 22 Aug 2004, 16:00
View user's profile Send private message Visit poster's website Reply with quote
proveren



Joined: 24 Jul 2004
Posts: 68
Location: Bulgaria
proveren
1. You call getModule handle only if you need a reference to the module, in most win applications you need it at least to create a window (the [hinstance] gets it value from the function call), if you dont know all win32API just like all C functions return the needed variable in eax.
2. You push the registers because they need (or you want them) to be saved after the function call.
3. WinMain is there to tell the C compiler that the program is a Windows one and starts from there, the compiler automatically calls the functions needed to get references to the four parameters before the actual code. Eg. GetModuleHandle for hInstance etc.
Post 22 Aug 2004, 17:20
View user's profile Send private message Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
Quote:

You call getModule handle only if you need a reference to the module, in most win applications you need it at least to create a window (the [hinstance] gets it value from the function call),


so, does it mean that, if i didn't invoke GetModule, my application would NOT run? or execute?

Quote:

You push the registers because they need (or you want them) to be saved after the function call.

what is the use of those values pushed into ebx,esi and edi there?
where are that values from?

Quote:

Eg. GetModuleHandle for hInstance etc.


if GetModuleHandle for hInstance, how about hPrevInstance, lpszCmdLine and nCmdShow?


sincerely,
vbVeryBeginner
Post 22 Aug 2004, 17:40
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
2. it's calling standard, caller awaits to have those values preserved. It is same when you call some API procedure, you await it won't change value of this registers.
Post 22 Aug 2004, 17:53
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
you call GetCommandLine to obtain lpszCmdLine, hPrevInstance is always NULL, and usually you don't have to care about nCmdShow. Open win32.hlp, and find "WinMain" in index, if you wan't more details.
Post 22 Aug 2004, 18:54
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: usa
coconut
thought window couldnt be created without [hinstance], but his "lite" example works Confused
Post 22 Aug 2004, 19:44
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo
GetModuleHandle will always return the same address value (0400000h) because is the start of the virtual address space assigned by windows loader for a running application.
As this could change in future versions of windows, it is a good practice to call GetModuleHandle however and use the returned value.
Post 22 Aug 2004, 22:04
View user's profile Send private message Yahoo Messenger Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
to:decard,
sorry for that trouble, i miss look those information in my PSDK ;(,
i did scroll that WinMain page before posting... but i guess, i might scroll it too fast :p

to: pelaillo,
Quote:

GetModuleHandle will always return the same address value (0400000h)

according to the PSDK :
Code:
HMODULE GetModuleHandle(
     LPCTSTR lpModuleName   // module name
);
    

If this parameter (lpModuleName) is NULL, GetModuleHandle returns a handle to the file used to create the calling process.

i wish if you could verify that do you mean, when the supplied value for lpModuleName is NULL, the return value will be always (0400000h)?

to : vid or anyone
does this mean, the proc WindowProc, hwnd,wmsg,wparam,lparam will modify the value of ebx esi edi?
if yes, i got a reason to push them there
if no, i really wonder why to push them there?

sincerely,
vbVeryBeginner d(0 >> ***)b
Post 23 Aug 2004, 03:11
View user's profile Send private message Visit poster's website Reply with quote
ShortCoder



Joined: 07 May 2004
Posts: 105
ShortCoder
You save ebx esi and edi because, whenever Windows gains control from your application, it can change the values in any of those without necessarily saving them and restoring them for you.

_________________
Boycott Symantec/Norton/PowerQuest whenever possible
Post 23 Aug 2004, 19:11
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Window procedure CAN'T modify (or more exact - must preserve) value of these 3 registers, so you have reson to push them if you are using it. If you aren't, all API calls will preserve them too, so they will stay preserved. But it is nice to preserve them always, you can prevent some ugly bug with this.
Post 23 Aug 2004, 19:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
ic,
anyway, vid, do you mean that "push ebx esi edi" and pop them off later in the end of WindowProc has becoming so common (a tradition for win32 asm guys) that now already emerges as a "Standard" when you wrote in message #4
Quote:

2. it's calling standard, caller awaits to have those values preserved. It is same when you call some API procedure, you await it won't change value of this registers.


i wish if you could verify my understanding, does this mean, in win32 ASM world, the WINAPI will ONLY modify the EAX register? and if it(winapi) need to uses the ebx,esi,or edi, it will push them first and pop them off later?

does the WINAPI or Windows use the "ebx esi edi"? and what are those registers function as in win32 operating system?

sincerely,
vbVeryBeginner d(..>)b
thanks in advance Smile
Post 24 Aug 2004, 04:39
View user's profile Send private message Visit poster's website Reply with quote
proveren



Joined: 24 Jul 2004
Posts: 68
Location: Bulgaria
proveren
vb, you really should read a decent ASM and Win32ASM tutorial. Sure you can learn from forums, but some questions are TOO basic. Besides ebx, edi, and esi there is not only eax. Yes, the push ebx esi edi is so common that some functions take it as granted so use this push and pop to avoid some bugs.
Post 24 Aug 2004, 09:52
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:

anyway, vid, do you mean that "push ebx esi edi" and pop them off later in the end of WindowProc has becoming so common (a tradition for win32 asm guys) that now already emerges as a "Standard" when you wrote in message #4

no it didn't become "standard" by usage. It is defined as standard by microsoft, and not doing this will almost surely cause bug. Window procedure or WINAPI procedure just must preserve them.
Quote:

i wish if you could verify my understanding, does this mean, in win32 ASM world, the WINAPI will ONLY modify the EAX register? and if it(winapi) need to uses the ebx,esi,or edi, it will push them first and pop them off later?

yes, and it also can modify ecx and edx freely.
Quote:
does the WINAPI or Windows use the "ebx esi edi"? and what are those registers function as in win32 operating system?

it uses them, and there is no special function of them in windows. Only special thing about them is that they have to stay preserved after calling procedure, so you can keep values in them between calls of winapi procedures.
Post 24 Aug 2004, 10:27
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
thanks vid,
that really helps Smile

Quote:

vb, you really should read a decent ASM and Win32ASM tutorial. Sure you can learn from forums, but some questions are TOO basic. Besides ebx, edi, and esi there is not only eax. Yes, the push ebx esi edi is so common that some functions take it as granted so use this push and pop to avoid some bugs.

if that is really what you could see from my post, i have nothing to say :<
Post 24 Aug 2004, 14:06
View user's profile Send private message Visit poster's website Reply with quote
proveren



Joined: 24 Jul 2004
Posts: 68
Location: Bulgaria
proveren
OK, sorry, vb!
(offtopic) You have quite and interesting web site.
Post 28 Aug 2004, 07:25
View user's profile Send private message Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
sorry too,
i didn't mean to offend :p

so, let us shake our hand Smile and yell, FASM horray! lol
Post 28 Aug 2004, 07:56
View user's profile Send private message Visit poster's website Reply with quote
proveren



Joined: 24 Jul 2004
Posts: 68
Location: Bulgaria
proveren
"FASM - make friends globally!"
Post 28 Aug 2004, 09:16
View user's profile Send private message 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 GitHub, YouTube, Twitter.

Website powered by rwasa.