flat assembler
Message board for the users of flat assembler.

Index > Windows > label, local label and variable declaration

Author
Thread Post new topic Reply to topic
pstarczewski



Joined: 19 Jun 2009
Posts: 2
pstarczewski
Hi,

I'm new to FASM and the last time I've used asm was about 12 years ago, so please be gentle. I'm trying to learn a little about FASM, but due to last 12 years of C++ and OOP programming I've some difficulities to switch to "asm thinking" Embarassed

As a test app I wanted to display some string using "local" data from the structure. Unfortunatly as soon as I start my app it crushes. I think it has something to do with the way I've declared my variable in the code (look at .testString declaration). My question: is it possible to do such declaration or do I have to use global data section for that? I know that it is possible to use ebp/esp for proc calls and to create local memory area for data, but does it also apply to main section/code? I have also tried to use lea, but with no effect.

Here is my example:

Code:
format PE GUI 4.0
entry AppStart

include '/asm/wininc/win.asm'
include 'dr.inc'


struc MyStruc
 {
    .s      du      'fasm test app',0
 }


section '.data' data readable writeable
 g_hInstance                             dd      ?
   msg                                             MSG
 str01                                   MyStruc


section '.text' code readable executable

AppStart:
  ;  get instance handle
        push  NULL
        call    [GetModuleHandle]
   mov             [g_hInstance],eax

       call    MainWindow.Create

    ;  main loop
    .main_loop:
            .testString             MyStruc
               push  MB_OK
                 ;push str01.s
               push  .testString.s
                 ;lea  edx,[.testString.s]
           ;push edx
           push  0
             push  NULL
                call    [MessageBox]

    ;  exit app
   push  eax
 call    [ExitProcess]


;-----------------------------------------------------
;  Main window class definition
;-----------------------------------------------------
MainWindow:
     .Create:
                push    ebp
         mov             ebp,esp

         leave
               ret
    
Post 20 Jun 2009, 05:12
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Hallo,pstarczewski

on http://flatassembler.net/examples.php
or compile the examples in the EXAMPLE directory of your fasm dwonloaded package

First af all,
1) set up path for fasm compiler in a batch file/win environment,
or at the prompt as follows (i have fasm.exe on J:\fasm)

path=%path%;J:\fasm

2) set up include directory in a batch file/win environment,
or at the prompt as follows (i use %fasminc%" as veriable name )

set fasminc=J:\fasm\include


Now, i will try to do my best for a Console standard app:

3) include main macros and standard includes in your code
Code:
  
 format PE CONSOLE 4.0
 entry start
   include '%fasminc%\win32a.inc'
    


4) global data go in a data section
Code:
 section ".data" data readable writeable
   szGlobalString     db "Hello World", 13,10,0
   szPause          db "pause",0
   szMessage      db "In the subroutine local dword is=%x",13,10,0
    


5)your code goes in the .code section
Code:
  section ".code" code readable executable
   start:
    cinvoke printf,szGlobalString
    cinvoke system,szPause
   ret
    

6) if you have sub-routines declare it as follows in this model
Code:
proc myroutine _arg1dword
   local .onstack_dword:DWORD
  
   mov eax,[.onstack_dword]
   add eax,[_arg1dword]
   cinvoke printf,szMessage,eax
  ret
endp
    

7) all the function you use must be imported in an import section
called .idata
Code:
 section '.idata' import data readable writeable
    


8 ) enable importing function from MSVCRT.DLL
Code:
  library msvcrt,"MSVCRT.DLL"
    


9) import explicitly function one by one,not omitting alias
Code:
     ;NOTE this functions use cinvoke convention
 import msvcrt,\
    printf,"printf",\
        _getch,"_getch",\
        system,"system"
    


All together
Code:
 format PE CONSOLE 4.0
 entry start
 include '%fasminc%\win32a.inc'
  
  section ".data" data readable writeable
   szGlobalString      db "Hello World", 13,10,0
   szPause          db "pause",0
   szMessage      db "In the subroutine local dword is (hexadecimal) %x",13,10,0
   szMessage1    db "You have passed (hexadecimal) %x as argument",13,10,0
   szMessage2    db "In the subroutine sum of local+passed arg is (hexadecimal) %x",13,10,0

 section ".code" code readable executable
        
 start:
   cinvoke printf,szGlobalString

   push 12345h  ; this goes in _arg1dword 
   call myroutine

   cinvoke system,szPause
  ret

 proc myroutine _arg1dword
   local .onstack_dword:DWORD

   cinvoke printf,szMessage1,[_arg1dword]
        
   ; assign an arbitary  value to .onstack_dword
   mov [.onstack_dword],56789h
   cinvoke printf,szMessage,[.onstack_dword]
      
   mov eax,[.onstack_dword]
   add eax,[_arg1dword]
   cinvoke printf,szMessage2,eax
  ret
 endp

  section '.idata' import data readable writeable
   library msvcrt,"MSVCRT.DLL"

   import msvcrt,\
  printf,"printf",\
        _getch,"_getch",\
        system,"system"
    


NOTE: for Windowing Example you need tutorials from Iczelion (MASM, but i cannot find at the moment the translation for Fasm, here on board ) or try
CreateWindowEx in search box on board.

NOTE2: study the use of "proc" (it is a macro) and the difference
of "struc" and "struct", searching them on FAQ or on searchbox

Ok, this is a PE console app (PE CONSOLE)
using only the printf/and system function
Try to compile it. Hope it is useful and...
happy fasming. Very Happy

Regards,
hopcode
Post 20 Jun 2009, 11:10
View user's profile Send private message Visit poster's website Reply with quote
pstarczewski



Joined: 19 Jun 2009
Posts: 2
pstarczewski
Thanks hopcode,

Looks very interesting. I did look at those examples and they look like a good start for me - total beginner at asm. Thanks for your help.
Post 20 Jun 2009, 23:21
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 YouTube, Twitter.

Website powered by rwasa.