flat assembler
Message board for the users of flat assembler.

Index > Windows > How to optimize - Hello World!

Author
Thread Post new topic Reply to topic
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 30 Mar 2006, 13:08
I found such example of the Hello World, see below:
Code:
 
; fasm example of writing 32-bit PE program using Win32 API

format PE GUI
entry start

; MessageBox type flags

MB_OK                = 0000h
MB_OKCANCEL          = 0001h
MB_ABORTRETRYIGNORE  = 0002h
MB_YESNOCANCEL       = 0003h
MB_YESNO             = 0004h
MB_RETRYCANCEL       = 0005h
MB_ICONHAND          = 0010h
MB_ICONQUESTION      = 0020h
MB_ICONEXCLAMATION   = 0030h
MB_ICONASTERISK      = 0040h
MB_DEFBUTTON1        = 0000h
MB_DEFBUTTON2        = 0100h
MB_DEFBUTTON3        = 0200h
MB_APPLMODAL         = 0000h
MB_SYSTEMMODAL       = 1000h
MB_TASKMODAL         = 2000h
MB_NOFOCUS           = 8000h

; Conventional dialog box and message box command IDs

IDOK      = 1
IDCANCEL  = 2
IDABORT   = 3
IDRETRY   = 4
IDIGNORE  = 5
IDYES     = 6
IDNO      = 7

section '.code' code readable executable

CodeSectionOrigin:

  start:
        push    MB_OK + MB_ICONEXCLAMATION
        push    _caption

        push    _message
        push    0
        call    [MessageBox]
  .local1:
        push    0
  .local2:
        call    [ExitProcess]

        jmp     .local1

section '.data' data readable writeable

DataSectionOrigin:

  _caption db 'Win32 assembly program',0
  _message db 'Hello World!',0

section '.idata' import data readable writeable

ImportSectionOrigin:

  dd 0,0,0,RVA kernel_name,RVA kernel_table
  dd 0,0,0,RVA user_name,RVA user_table
  dd 0,0,0,0,0

  kernel_table:
    ExitProcess dd RVA _ExitProcess
    dd 0
  user_table:
    MessageBox dd RVA _MessageBoxA
    dd 0

  kernel_name db 'KERNEL32.DLL',0
  user_name db 'USER32.DLL',0

  _ExitProcess dw 0
    db 'ExitProcess',0
  _MessageBoxA dw 0
    db 'MessageBoxA',0

                                                                  


How this can be optimized?
Post 30 Mar 2006, 13:08
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 30 Mar 2006, 13:20
do you mean simplified/shortened? check the fasmw examples folder for a macro version. that example there is a raw way of building a pe executable, which is why it looks so complicated for such a simple task
Post 30 Mar 2006, 13:20
View user's profile Send private message Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 30 Mar 2006, 13:48
Of course I checked the fasmw examples before I wrote thread here. I was trying to compile that example but there is mistake or something wrong there. That is why I found this example and want to know how to simplified it.
Post 30 Mar 2006, 13:48
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 30 Mar 2006, 15:14
Urff wrote:
I found such example of the Hello World, see below:
Code:
 
; fasm example of writing 32-bit PE program using Win32 API

format PE GUI
entry start

; MessageBox type flags

MB_OK                = 0000h
MB_OKCANCEL          = 0001h
MB_ABORTRETRYIGNORE  = 0002h
MB_YESNOCANCEL       = 0003h
MB_YESNO             = 0004h
MB_RETRYCANCEL       = 0005h
MB_ICONHAND          = 0010h
MB_ICONQUESTION      = 0020h
MB_ICONEXCLAMATION   = 0030h
MB_ICONASTERISK      = 0040h
MB_DEFBUTTON1        = 0000h
MB_DEFBUTTON2        = 0100h
MB_DEFBUTTON3        = 0200h
MB_APPLMODAL         = 0000h
MB_SYSTEMMODAL       = 1000h
MB_TASKMODAL         = 2000h
MB_NOFOCUS           = 8000h

; Conventional dialog box and message box command IDs

IDOK      = 1
IDCANCEL  = 2
IDABORT   = 3
IDRETRY   = 4
IDIGNORE  = 5
IDYES     = 6
IDNO      = 7

section '.code' code readable executable

CodeSectionOrigin:

  start:
        push    MB_OK + MB_ICONEXCLAMATION
        push    _caption

        push    _message
        push    0
        call    [MessageBox]
  .local1:
        push    0
  .local2:
        call    [ExitProcess]

        jmp     .local1

section '.data' data readable writeable

DataSectionOrigin:

  _caption db 'Win32 assembly program',0
  _message db 'Hello World!',0

section '.idata' import data readable writeable

ImportSectionOrigin:

  dd 0,0,0,RVA kernel_name,RVA kernel_table
  dd 0,0,0,RVA user_name,RVA user_table
  dd 0,0,0,0,0

  kernel_table:
    ExitProcess dd RVA _ExitProcess
    dd 0
  user_table:
    MessageBox dd RVA _MessageBoxA
    dd 0

  kernel_name db 'KERNEL32.DLL',0
  user_name db 'USER32.DLL',0

  _ExitProcess dw 0
    db 'ExitProcess',0
  _MessageBoxA dw 0
    db 'MessageBoxA',0

                                                                  


How this can be optimized?


it's not really possible to optimize just two API calls, but if you want to make it smaller, remove the data section and make it all one giant section

Code:
section '.flat' code readable writeable executable    


and the imports can be merged aswell

Code:
section '.idata' import data readable writeable
    

to
Code:
data import
;;;
end data
    


you could also remove
Code:
jmp     .local1    


it is not needed

_________________
redghost.ca
Post 30 Mar 2006, 15:14
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 30 Mar 2006, 15:39
Ок, I downloaded zip-archive fasmw 1.65.17, open example Hello World and see next when compile:

Error: setting already Specified
Display:
Instruction: entry
Source: Hello.asm[12]
win32ax.inc[136]

What is wrong?
Post 30 Mar 2006, 15:39
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 30 Mar 2006, 16:49
Error in win32ax.inc[136] means that you either specified entry point with the "entry" directive and ".end" macro, or used ".end" macro twice.
Post 30 Mar 2006, 16:49
View user's profile Send private message Visit poster's website Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 30 Mar 2006, 19:02
I did nothing, only compile Hello.asm from the Examples folder of the fasmw 1.65.17 distributive. Please,see below:
Code:

format PE GUI 4.0
entry start

include 'win32ax.inc'

.code

  start:
        invoke  MessageBox,HWND_DESKTOP,"Hi! I'm the example program!","Win32 Assembly",MB_OK
        invoke  ExitProcess,0

.end start      
                                  


I still can't understand where is the mistake? I didn't do anything, only open file Hello.asm and push run only......
Post 30 Mar 2006, 19:02
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 30 Mar 2006, 20:57
Remove "entry start", ".end start" is enough to declare entrypoint here.
Post 30 Mar 2006, 20:57
View user's profile Send private message Visit poster's website Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 31 Mar 2006, 08:31
@Tomasz Grysztar thanks, but the other examples in the examples directory also have mistakes...
Post 31 Mar 2006, 08:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 31 Mar 2006, 09:33
In what examples directory?
Post 31 Mar 2006, 09:33
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 31 Mar 2006, 11:11
I checked - the ones that come with 1.65.17 - no mistakes found!
Post 31 Mar 2006, 11:11
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 31 Mar 2006, 14:17
Madis731 wrote:
I checked - the ones that come with 1.65.17 - no mistakes found!


as did i, and the hello example does not have 'entry start' as it makes use of the '.end label' macro

_________________
redghost.ca
Post 31 Mar 2006, 14:17
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 31 Mar 2006, 17:41
Fasmw 1.65.17 arhive - Example directory, I was trying only examples for Windows, and everytime to run the example, I need to copy this example to the Include directory to run it... And in most attempts difficulties in at the running
Code:
include 'win32ax.inc'    
.....
I understand what is the mistake of
Code:
 include 'win32ax.inc'    

The correct code must be
Code:
 include '%path to the Include directory%\win32ax.inc'    

I'm just starting to learn my fist programming language and I chose fasm, so now I have some difficulties and problems, I can't find any good fasm's source in the Internet on my native language that is why I'll will learn it with the help of this site and help of the members of this forum.
Post 31 Mar 2006, 17:41
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 01 Apr 2006, 01:52
Urff wrote:

I'm just starting to learn my fist programming language and I chose fasm, so now I have some difficulties and problems, I can't find any good fasm's source in the Internet on my native language that is why I'll will learn it with the help of this site and help of the members of this forum.


wise choice Smile, if you are having problems finding tutorials on fasm in your native language you can always search for 'MASM' or even 'NASM' sources and the conversion to fasm should be simple enough

_________________
redghost.ca
Post 01 Apr 2006, 01:52
View user's profile Send private message AIM Address MSN Messenger Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 01 Apr 2006, 02:29
Urff wrote:

The correct code must be
Code:
 include '%path to the Include directory%\win32ax.inc'    


Wrong. The newer versions of FASM (i think 1.63 and above, correct me if i am wrong) will automatically look for win32ax.inc (or anything else you include) first in the current directory, then relative to the %INCLUDE% environment variable (if it exists). Therefore, if you have the %INCLUDE% environment variable set to the correct directory, then
Code:
 include 'win32ax.inc'     
works.

_________________
This calls for... Ultra CRUNCHY Man!
Ta da!! *crunch*
Post 01 Apr 2006, 02:29
View user's profile Send private message Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 01 Apr 2006, 17:31
This may be works in the exe.install archive but not in the zip's one!...
Post 01 Apr 2006, 17:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 01 Apr 2006, 17:53
The section "1.1.1 System Requirements" of FASM.PDF contains the installation instructions.
Post 01 Apr 2006, 17:53
View user's profile Send private message Visit poster's website Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 01 Apr 2006, 18:25
There is no exe install archive. The idea is basically: you have to set the INCLUDE environment variable yourself.
Post 01 Apr 2006, 18:25
View user's profile Send private message Reply with quote
Urff



Joined: 29 Mar 2006
Posts: 22
Urff 01 Apr 2006, 18:34
Thanks again to Tomasz Grysztar.....
Post 01 Apr 2006, 18:34
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.