flat assembler
Message board for the users of flat assembler.

Index > Windows > HardCoding request ??

Author
Thread Post new topic Reply to topic
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 19 Jul 2005, 01:24
Hello 2 all.
I was wondering if it was posible to code in FASM without using the includefiles.
What i mean is check code.
Code:
;------------------------------------------------
; Program Name: re.asm
; Version: 1.0
; Purpose: HardCoding in FASM
; Author: Spidark (C) 2005
; Date: 27-06-2005
; Tools: Created using RadASM IDE with FASM
; This program was tested on Windows XP SP2 
;------------------------------------------------

format PE GUI 4.0
entry START
;-------------------------------------------------
; CONSTANTS
;-------------------------------------------------
;-------------------------------------------------
; INCLUDES
;-------------------------------------------------
  
include "%fasminc%\win32a.inc"

;section '.const' data 
section '.data' data readable writeable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;include "re.inc"
;-------------------------------------------------
; NON INIT SECTION
;-------------------------------------------------
        hwnd            dd ?
        hInstance       dd ?
        hdc             dd ?
        tmphdc          dd ?



;-------------------------------------------------
; INIT SECTION
;-------------------------------------------------

        WND_WIDTH       dd 800
        WND_HEIGHT      dd 600
        mouseClick      dd 1    

;--------------------------------------------------
; STRUCTURES
;--------------------------------------------------
;--------------------------------------------------
 
        msg             MSG
        wc              WNDCLASSEX
        ps              PAINTSTRUCT
        rect            RECT
        hitPoint        POINT



;--------------------------------------------------
; TITLES MESSAGES ETC ETC
;--------------------------------------------------
        _wTitle         db "re v1.0 Spidark 2005",0 
        _wcName         db "commol_Class32",0 
        _errText        db "ERROR: Program re cannot run on this machine!",0
        _messCap        db "Message",0
        _helloMsg       db "HARDCODING in FASM ITS FUCKING GREAT",0

        
        

        
section '.code' code readable executable
START:
;------------------------------------------------
; FILL STRUCTURE
;------------------------------------------------
; Get Module handle
;------------------------------------------------
        invoke  GetModuleHandle,NULL
        
        
        mov     [hInstance],eax
        mov     [wc.hInstance],eax
        mov     [wc.cbSize],sizeof.WNDCLASSEX
        mov     [wc.style],CS_HREDRAW or CS_VREDRAW
        mov     [wc.lpfnWndProc],WND_PROC
        mov     [wc.cbClsExtra],0
        mov     [wc.cbWndExtra],0
        invoke  LoadIcon,NULL,IDI_APPLICATION
        mov     [wc.hIcon],eax
        
        invoke  LoadCursor,NULL,IDC_ARROW
        mov     [wc.hCursor],eax
        
        mov     [wc.hbrBackground],COLOR_WINDOW+1
        mov     [wc.lpszClassName],_wcName
        
        mov     [wc.hIconSm],0
        
;--------------------------------------------------
; REGISTER THE WINDOW
;-------------------------------------------------- 
        invoke  RegisterClassEx,wc
        cmp     eax,1
        jne     @F
        push    MB_ICONSTOP or MB_OK
        
        invoke  MessageBoxEx,[hwnd],_errText,_messCap,MB_OK or MB_ICONSTOP 
        invoke  ExitProcess,0
@@:     
        

 
;------------------------------------------------
; CREATE THE MAIN WINDOW
;------------------------------------------------
        invoke  CreateWindowEx,0,\
                _wcName,\
                _wTitle,\
                WS_OVERLAPPEDWINDOW,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                [WND_WIDTH],\
                [WND_HEIGHT],\
                NULL,\
                NULL,\
                [hInstance],\
                NULL
        
        mov     [hwnd],eax
;-----------------------------------------------
; CENTER THE WINDOW
;-----------------------------------------------            
        push    [hwnd]
        call    CenterWindow
       
;------------------------------------------------
; SHOW THE WINDOW
;------------------------------------------------
        invoke  ShowWindow,[hwnd],SW_SHOW
        
;------------------------------------------------
; MESSAGE LOOP
;------------------------------------------------

MSG_LOOP:
        
        invoke  GetMessage,msg,0,0,0
        
        or   eax,eax
        jz   END_LOOP
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp  MSG_LOOP
END_LOOP:
        invoke  MessageBoxEx,[hwnd],_errText,_messCap,MB_ICONSTOP or MB_OK ,0
        call    [ExitProcess]
        invoke  ExitProcess,0
        
 
        
;-------------------------------------------------
; WINDOW CALLBACK PROC
;-------------------------------------------------
; EQUATES
;-------------------------------------------------
hWnd    equ     ebp+8
uMsg    equ     ebp+12
wParam  equ     ebp+16
lParam  equ     ebp+20
        
WND_PROC:
        ;==========================================================
        ; First create a stack frame and save register
        ;==========================================================
        push    ebp                       ; Create stack frame
        mov     ebp,esp
        push    ebx esi edi               ; save the API destructive registers
        ;==========================================================
        ; WM_DESTROY
        ;==========================================================
        cmp     dword [uMsg],WM_DESTROY   ; DO WE HAVE WM_DESTROY
        jne     L1                        ; NO SO jump to next mess
                                          ; Yes we do so post a quitmessage             
        invoke  PostQuitMessage,0
        jmp     EX_IT                     ; EXIT
        ;==========================================================
        ; WM_NCHITTEST
        ;==========================================================
L1:     
        cmp     dword [uMsg],WM_NCHITTEST
        jne     L2
        invoke  DefWindowProc,dword [hWnd],\
                              dword [uMsg],\
                              dword [wParam],\
                              dword [lParam]
                                
                             
        cmp eax,1                       ; is it A 1 ??
        jnz @F                          ; 
        inc eax                         ; YES so increment eax
@@:
        jmp     EX_IT
        ;==========================================================
        ; WM_CREATE
        ;==========================================================
L2:
        cmp     dword [uMsg],WM_CREATE
        jne     L3
        invoke  MessageBoxEx,dword[hWnd],_helloMsg,_messCap,MB_OK,0
        jmp     EX_IT
        ;==========================================================
        ; WM_PAINT
        ;==========================================================
L3:
        cmp     dword [uMsg],WM_PAINT
        jne     DEFW
        invoke  BeginPaint,dword [hWnd],ps
        mov     [hdc],eax
        
        ;calclulate the string lenght
        invoke  lstrlen,_helloMsg
        
        ;show the screen
        invoke  TextOut,[hdc],30,10,_helloMsg,eax
        
        ;- Stop paiting 
        push    [hdc]
        call    [EndPaint]
        invoke  EndPaint,dword [hWnd],ps
        jmp     EX_IT   
        
        ;==========================================================
        ; DEFWINPROC
        ;==========================================================
DEFW:   
        invoke  DefWindowProc,dword [hWnd],\
                              dword [uMsg],\
                              dword [wParam],\
                              dword [lParam]
        
EX_IT:  
        ;==========================================================
        ; RESTORE THE STACKFRAME VERY IMPORTANT
        ;==========================================================
        pop     edi esi ebx
        mov     esp,ebp
        pop     ebp
        retn    16
     


        
lphWnd  equ     ebp+8
lpX     equ     ebp-4
lpY     equ     ebp-8
rct     equ     ebp-24                  ; 24 because the sizeof RECT struct is 24 Bytes long

CenterWindow:
        push    ebp                     ; create a  stack frame
        mov     ebp,esp
        sub     esp,24                  ; make room for local variables
        push    ebx esi edi             ; save API destructive registers

        lea     edi,dword [rct]         ; get adress of rect in edi
        invoke  GetWindowRect,dword[hWnd],edi

        invoke  GetSystemMetrics,SM_CXSCREEN
        mov     ecx,[edi+RECT.right]
        sub     ecx,[edi+RECT.left]
        sub     eax,ecx
        shr     eax,1
        mov     dword [lpX],eax
        
        invoke  GetSystemMetrics,SM_CYSCREEN
        mov     ecx,[edi+RECT.bottom]   
        sub     ecx,[edi+RECT.top]      
        sub     eax,ecx                 
        shr     eax,1                   
        mov     dword [lpY],eax
        ;==========================================================
        ; Set The Window position with this API call [7 Parameters]
        ;==========================================================
        ; FROM RIGTH TO LEFT:
        ; 1-> window-positioning flags
        ; 2-> height
        ; 3-> width
        ; 4-> vertical position
        ; 5-> handle of window
        ; 6-> placement-order handle
        ; 7-> horizontal position
        ; Then call the API
        ;--------------------------------------------
        invoke  SetWindowPos,dword [lphWnd],NULL,dword [lpX], dword [lpY],NULL,NULL,SWP_NOSIZE or SWP_NOZORDER
        ;==========================================================
        ; RESTORE THE STACKFRAME VERY IMPORTANT
        ;==========================================================
        add     esp,24                  ; restore local space
        mov     esp,ebp                 
        pop     ebp                      
        retn    4                       
        
        
        


;-------------------------------------------------
; IMPORTS AND INCLUDES
;-------------------------------------------------
section '.idata' import data readable writeable
    library kernel32,'KERNEL32.DLL',\
            user32,    'USER32.DLL',\
            gdi32,     'GDI32.DLL'
  include '%fasminc%\apia\kernel32.inc'
  include '%fasminc%\apia\user32.inc'
  include '%fasminc%\apia\gdi32.inc'

                       
section '.rsrc' resource data readable

; resource directory

  directory RT_VERSION  ,versions,\
            24 ,manifest
; resource subdirectories

 
resource   versions,\
           1,LANG_NEUTRAL,version_info
resource   manifest,\
           1,LANG_NEUTRAL,man
  

version version_info,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH+SUBLANG_DEFAULT,0,\
          'FileDescription','A SPIDARK SOFTWARE',\
          'LegalCopyright','No rights reserved.',\
          'FileVersion','1.0',\
          'ProductVersion','1.0',\
          'OriginalFilename','re.EXE'
         
fileres   man,'re.xml' 
    

Ok let's say i have this nice piece of code wich is running smooth and nice, but for some crazy reason ( learning reason), i didn't want to use the
Code:
include "%fasminc%\win32a.inc"    

This would generate a list full of errors i know.
Let's say i wanted to use ( just to go deeper into asm programming so i would understand what was going on behind the macros) my own kinda structures wich i would declare into a seperate include file like this
Code:
;===============================
; MESSAGE STRUCTURE
;===============================
; Message Structure Begins
        hWnd            dd      ?        
        message dd      ? 
        wParam          dd      ? 
        lParam          dd      ?        
        time            dd      ? 
;--------------------------------
;THIS IS A POINT STRUCTURE
;--------------------------------
        MPx             dd      ? 
        MPy             dd      ?
;================================
; END MESSAGE STRUCTURE
;================================

;=================================
; WNDCLASSEX
;=================================
        cbSize          dd      ? 
        style           dd      ? 
        lpfnWndProc     dd      ? 
        cbClsExtra      dd      ? 
        cbWndExtra      dd      ? 
        hInstance       dd      ?        
        hIcon           dd      ? 
        hCursor dd      ? 
        hbrBackground   dd      ? 
        lpszMenuName    dd      ? 
        lpszClassName   dd      ? 
        hIconSm dd      ?        
;--------------------------------
; END   WNDCLASSEX
;================================
 
;================================
; BEGIN PAINTSTRCUT
;================================
        hdc             dd      ? 
        fErase          dd      ? 
        rcPaint dd      ? 
        fRestore        dd      ? 
        fIncUpdate      dd      ?        
        rgbReserved     rb      32 
;---------------------------------
; END PAINTSTRUCT
;=================================

;=================================
; BEGIN RECT
;================================= 
        left            dd      ? 
        top             dd      ?        
        right           dd      ? 
        bottom          dd      ? 
;---------------------------------
; END RECT 
;=================================

;=================================
;BEGIN POINT
;=================================
        Px              dd      ?
        Py              dd      ?        
;---------------------------------
;END POINT
;================================= 

         


    

Can someone tel me how this would look like ( please if can give me a working example ).
I'm guessing that i wil have to know the total size of my Data 2 know where the structures begins !!!
Hey i'm just guessing...

I know what your thinking Oh My he's gone Crazy...!!!!!
Why reinvent the wheel ????
The answer is simple ...
To learn.
Gurus out there don't leave me in the dark here ...
And one last question ???
Can some one give me and example of a program that auto run itself everytime Windows Start up, i believe that it's has to write to the regristry ???
I hope that i didn't ask to much... Surprised.
Post 19 Jul 2005, 01:24
View user's profile Send private message Reply with quote
Ancient One



Joined: 28 Feb 2005
Posts: 55
Ancient One 19 Jul 2005, 02:57
included with every fasm package is a good manual. please read them first. there u will find out meanings and usage of every fasm's preprocessor/assembler directives. for example, fasm's 'struc' preprocessor directive is usually used to define a structure :
Code:
struc MSG {
  .hWnd            dd      ?
  .message         dd      ?
  .wParam          dd      ?
  .lParam          dd      ?
  .time            dd      ?
}
    

thats just a basic structure definition and u can use it to declare an instance of MSG structure like :
Code:
msg MSG
    


this is a very primitive structure definition. The Win32 headers included in fasm for Win32 package has more useful struct macro. read also the documentation section on flatassembler.net regarding the win32 headers.

u certainly don't want to built any data directories (import, export, resources, etc) manually. fasm doesn't support them internally except for reloc info and resource.

Autorun can be done in several ways.
-Startup folder in start menu (location varies depending on Windows version).
-adding entry in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key. (HKCU for user specific).
Post 19 Jul 2005, 02:57
View user's profile Send private message MSN Messenger Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 19 Jul 2005, 03:02
you can manually create import section, see PEDEMO in examples folder
Post 19 Jul 2005, 03:02
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 19 Jul 2005, 06:52
Hi Spidark,
I hate macros, structures, ... other things as you. This is because I don't understand it. I'am lazy to learn it. Every has the advantage, but I have no energy to learn and study. I understand code only as can be see in disassembler and debugger. If I code something, this is done slow, hardcoded, without macros, structures... Sometimes when I code, I must compile other demo with macros, then disassemble and debug it for understand, what macro and structure mean. I'am not serious coder. My coding isn't clear and "poetic" as with advantage of macros, structures. But my code everytime work! I don't want to make money with coding, it's for my pleasure only. I have hard head, hard to improve my insufficiency.
I know this isn't good way, and I don't want to preach nobody.
I want only to tell solidarity with you and for you know, there exist enough peoples going against the stream and piss against the wind.
For other coders - please don't reply to my opinions. I'am not able to change or correct myself, because high age and hard head... I must live with my defects.
Feryno


Last edited by Feryno on 20 Jul 2005, 05:57; edited 1 time in total
Post 19 Jul 2005, 06:52
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 19 Jul 2005, 17:48
Feryno wrote:
Hi Spidark,
I hate macros, structures, ... other things as you. This is because I don't understand it. I'am lazy to learn it. Every has the advantage, but I have no energy to learn and study. I understand code only as can be see in disassembler and debugger. If I code something, this is done slow, hardcoded, without macros, structures... Sometimes when I code, I must compile other demo with macros, then disassemble and debug it for understand, what macro and structure mean. I'am not serious coder. My coding isn't clear and "poetic" as with advantage of macros, structures. But my code everytime work! I don't want to make money with coding, it's for my pleasure only. I have hard head, hard to improve my insufficiency.
I know this isn't good way, and I don't want to preach nobody.
I want only to tell solidarity with you and for you know, there exist enough peoples going against the stream and piss against the wind.
For other coders - please don't reply to my opinions. I'am not able to change or correct itself, because high age and hard head... I must live with my defects.
Feryno

Laughing Laughing Yes there are other Humans like me out there Laughing .
I totaly can identify myself in your post Feryno.
I myself has the same problems i'm not a profesional coder,i program just for pleasure and i'm not young anymore just a middle aged guy with a computer and interested in assembly programming, and at the moment only interested in assembly programming, despite the votes on other languages in one of my other posts.
I gues it's safe to say that i don't understand macros ( Laughing don't have the energy to learn it YET ) Yes i'm lazy i know, but i don't think that it's wrong to program theway we do it Feryno Wink . I personaly think that as a newbie ( i speak for myself ) it's a great way to learn assembly language.
Wink
Ancient One your right i wil have to check the manual from time to time.
But if it not to much to ask can you wip up a working autorun example ??
coconut Thanks i checked the demo!!!
Post 19 Jul 2005, 17:48
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Jul 2005, 18:07
Spidark: istad of "include ..." directive just paste whole file, it's same. Read something of my preprocessor tutorial - last chapters are obsolete but i believe you'll get the idea http://decard.net/?body=tajga&chapter=preproc

btw: you are on the right path! Wink
Post 19 Jul 2005, 18:07
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 19 Jul 2005, 18:42
vid wrote:
Spidark: istad of "include ..." directive just paste whole file, it's same. Read something of my preprocessor tutorial - last chapters are obsolete but i believe you'll get the idea http://decard.net/?body=tajga&chapter=preproc

Thanks vid for the fast reply, i missed this site totaly lots of info on FASM here...
vid wrote:
btw: you are on the right path! Wink

It's great to know vid Wink
Post 19 Jul 2005, 18:42
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 19 Jul 2005, 18:54
Yes: the hard way. A lot of us think the same way Wink

If we didn't want to know how the machine works, we won't be coding in assembly in the first place.
I use a macro only when I know fully how it works.

Give a look at this link (PE from scratch) -> http://board.flatassembler.net/topic.php?t=1309
Post 19 Jul 2005, 18:54
View user's profile Send private message Yahoo Messenger Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 19 Jul 2005, 19:18
... or if you want something simpler - here's DOS executable written from scrach:
Code:
macro .align value { rb (value-1)-($+value-1) mod value }

        db      'MZ'            ; +00 signature
        dw      exe_size        ; +02 number of bytes in last page
        dw      (exe_size/512)+1; +04 number of 512b pages
        dw      0x0000          ; +06 number of relocation entries
        dw      0x0002          ; +08 header size in 16-byte paragraphs
        dw      0x0010          ; +0a min. # of paragraphs of extra memory
        dw      0xffff          ; +0c max. # of paragraphs of extra memory
        dw      0x0000          ; +0e initial value of SS
        dw      0x0140          ; +10 initial value of SP
        dw      0x0000          ; +12 checksum (or 0) of executable
        dw      0x0000          ; +14 entry point (initial value of IP)
        dw      0x0000          ; +16 initial value of CS
        dw      0x0020          ; +18 offset in file of first reloc. item
        dw      0x0000          ; +1a overlay number

        .align 0x20

  start:
        org     0

        push    cs
        pop     ds
        mov     dx,message
        mov     ah,9
        int     0x21
        mov     ax,0x4c01
        int     0x21
message db 'Hello, world!', 0x0d, 0x0a,'$'

exe_size:    
Post 19 Jul 2005, 19:18
View user's profile Send private message Visit poster's website Reply with quote
Format



Joined: 20 Jul 2005
Posts: 3
Format 20 Jul 2005, 17:54
Hi


Neat windows program, re.asm. Have to test it out myself on WinXP SP2. That's one finicky platform, to say the least!


But, just wondering, in your main loop section, when control exits the main loop, you have and error meassage:

invoke MessageBoxEx,[hwnd],_errText,_messCap,MB_ICONSTOP or MB_OK ,0


Sorry for the silly question, but, not all exit conditons from the main loop are an error. Correct?

The loop can be terminated with a simple alt_F4 exit key combination and therefore not an error condition.. Probably, I'm not seeing the need for it there, more likely.


Thanks for sharing the example with the rest of us.


Format
Post 20 Jul 2005, 17:54
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 20 Jul 2005, 22:09
alt+f4 is sent to window as request to lose window (WM_CLOSE) and then window is destroyed (WM_DESTROY). And app can end anywhere, not only behind message loop. App ends when ExitProcess is invoked, and that can be done anywhere. (Like DOS's int 20h or int 21h/ah=4Ch). Check these error messages.
btw windows isn't good to learn assembly. You kill most of time by learning stupid windoze's interface (solving problems like this one) instead of assembly.
Post 20 Jul 2005, 22:09
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Format



Joined: 20 Jul 2005
Posts: 3
Format 21 Jul 2005, 00:11
Hi vid


Yeah, I'm just starting to learn assembly myself. Just had a few sessions in FASM.

Don't really know the Win32API either. So, I guess those two add up for a bad combination for me, at the moment.


So to recap what I think you are saying, that main loop does not check for a WM_QUIT message, therefore, if alt_F4 is pressed , control flow will not even go into the msg loop after that ?


PostQuitMessage,0 in destroy window, in the Wnd Proc Callback procedure is not even interpreted by the main msg loop?

...since the app ends up at the Wnd Proc Callback destroy window section, then, where else is "exit process" being invoked from on an non_error event ?


But, if after the alt_F4 key exit, control flow goes right straight to what follows the msg loop, won´t it "have" to invoke that error mesg box needlessly on a non error messaage event ?


Yep, I got it wrong again, for sure.


Format
Post 21 Jul 2005, 00:11
View user's profile Send private message Reply with quote
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 22 Jul 2005, 00:25
Format wrote:

invoke MessageBoxEx,[hwnd],_errText,_messCap,MB_ICONSTOP or MB_OK ,0
Sorry for the silly question, but, not all exit conditons from the main loop are an error. Correct?


Well Format i'm pretty new to this myself so i wil tell you what i know , and your right on... not all exit condition is an error.
This is wat i know. and i qoute!!!
If the function retrieves a message other than WM_QUIT, the return value is TRUE.
If the function retrieves the WM_QUIT, the return value is FALSE.
If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle.
It was put there as an experiment ( the error message i mean )
I wanted to simulate the invalid handle to see how it works.. Laughing
Code:
MSG_LOOP:
        
        push    NULL                            ;last message
        push    NULL                            ;first message
        push    NULL                            ;handle of window
        push    MSG_STR                         ;address of structure with message
        call    [GetMessage]
                
        
        cmp     eax,0                           ; If message is WM_QUIT GetMessage returns 0
        je      END_LOOP                        ; Exit loop if we have zero
        cmp     eax,-1                          ; Check for Invalid handle error or somekind
        je      END_ERROR                       ; EXIT LOOP WITH ERROR
        push    MSG_STR                         ;address of structure with message
        call    [TranslateMessage]
        
        push    MSG_STR                         ;address of structure with message
        call    [DispatchMessage]
        jmp  MSG_LOOP                           ;Do it over and over again
END_ERROR:
        ; experimental error handling
END_LOOP:
        push    0                               ;Wat was the last Message parameter 
        call    [ExitProcess]
    


Format wrote:

So to recap what I think you are saying, that main loop does not check for a WM_QUIT message, therefore, if alt_F4 is pressed , control flow will not even go into the msg loop after that ?

Not sure on this one Format ?? maybe one of the gurus could explain this in detail
But i know that after pressing the X on the window postquitmessage wil execute wich wil put a WM_QUIT in the message queue.
Post 22 Jul 2005, 00:25
View user's profile Send private message Reply with quote
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 22 Jul 2005, 00:33
Ok I have managed to create a new re2.
Just a stupid program, but i got it working, but pointers are ALL welcome of course.
Code:
;------------------------------------------------
; Program Name: re2.asm
; Version: 1.0
; Purpose: HardCoding in FASM
; Author: Spidark (C) 2005
; Date: 27-06-2005
; Tools: Created using RadASM IDE with FASM
; This program was tested on Windows XP SP2 
;------------------------------------------------

format PE GUI 4.0
entry START

;-------------------------------------------------
; CONSTANTS
;-------------------------------------------------
;-------------------------------------------------
; INCLUDES
;-------------------------------------------------
include "re2.inc"
                        ; herés 
;section '.const' data 
section '.data' data readable writeable
;-------------------------------------------------
; NON INIT SECTION
;-------------------------------------------------
        hwnd            dd ?
        hInstance       dd ?
        hdc             dd ?
        tmphdc          dd ?



;-------------------------------------------------
; INIT SECTION
;-------------------------------------------------

        WND_WIDTH       dd 800
        WND_HEIGHT      dd 600
        mouseClick      dd 1    

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

;===== MESSAGE STRUCT   
MSG_STR:
        msg_hwnd                dd      ?        
        msg_message             dd      ?
        msg_wParam              dd      ?
        msg_lParam              dd      ?
        msg_time                dd      ?
        msg_mse_pntX            dd      ?
        msg_mse_pntY            dd      ?

;=======WNDCLASSEX STRUCT
WNDCLASSEX_STR:
        wc_cbSize               dd      ?
        wc_style                dd      ?
        wc_lpfnWndProc          dd      ?
        wc_cbClsExtra           dd      ?
        wc_cbWndExtra           dd      ?
        wc_hInstance            dd      ?
        wc_hIcon                dd      ?
        wc_hCursor              dd      ?
        wc_hbrBackground        dd      ?
        wc_lpszMenuName         dd      ?
        wc_lpszClassName        dd      ?
        wc_hIconSm              dd      ?
WNDCLASSEX_SIZE = $ - WNDCLASSEX_STR
        
;========PAINTSTRUCT
PAINTSTRUCT_STR:
        ps_hdc                  dd      ? 
        ps_fErase               dd      ? 
        ps_rcPaint              dd      ? 
        ps_fRestore             dd      ? 
        ps_fIncUpdate           dd      ? 
        ps_rgbReserved          db      32 

;=========RECT
RECT_STR:
        rect_left               dd      ? 
        rect_top                dd      ?
        rect_right              dd      ?
        rect_bottom             dd      ?




;--------------------------------------------------
; TITLES MESSAGES ETC ETC
;--------------------------------------------------
        _wTitle         db "re2 v1.0 Spidark 2005",0 
        _wcName         db "commol_Class32",0 
        _errText        db "ERROR: Program re2 cannot run on this machine!",0
        _messCap        db "Message",0
        _helloMsg       db "HARDCODING in FASM ITS FUCKING GREAT",0

        
        

        
section '.code' code readable executable
START:
        ;------------------------------------------------
        ; FILL STRUCTURE
        ;------------------------------------------------
        push    NULL
        call    [GetModuleHandle]
        
        mov     [hInstance],eax
        mov     dword [wc_hInstance],eax
        mov     [wc_cbSize],WNDCLASSEX_SIZE     
        mov     [wc_style],CS_HREDRAW or CS_VREDRAW
        mov     dword [wc_lpfnWndProc],WND_PROC
        mov     [wc_cbClsExtra],0
        mov     [wc_cbWndExtra],0
        
        push    IDI_APPLICATION
        push    dword [hInstance]
        call    [LoadIcon]
        mov     dword [wc_hIcon],eax
        
        push    IDC_ARROW
        push    dword [hInstance]
        call    [LoadCursor]
        mov     dword [wc_hCursor],eax
        
        mov     [wc_hbrBackground],COLOR_WINDOW+1
        mov     dword[wc_lpszClassName],_wcName
        
        mov     [wc_hIconSm],0
        
        ;--------------------------------------------------
        ; REGISTER THE WINDOW
        ;-------------------------------------------------- 
        lea     edx,[wc_cbSize]                 ; Get adress of the WNDCLASSEX structure
        push    edx                             ; It's needed for the Nex API Call
        call    [RegisterClassEx]               ; If the function fails, the return value is zero. 
        cmp     eax,1                           ; So Are we Error Free here ??
        jne     @F                              ; Its not a NULL so Out of here
                                                ; SHIT WE GOT a error so Bork
                                                
        push    0                               ; language identifier
        push    MB_OK or MB_ICONSTOP            ; style of message box
        push    _messCap                        ; address of title of message box
        push    _errText                        ; address of text in message box 
        push    [hwnd]                          ; handle of owner window
        call    [MessageBoxEx]                  ; Show the error Message
        
        push    0 
        call    [ExitProcess]                   ; Terminate program
        
@@:     
        

 
        ;------------------------------------------------
        ; CREATE THE MAIN WINDOW
        ;------------------------------------------------
        

        push    NULL                            ; address of window-creation data
        push    [hInstance]                     ; handle of application instance
        push    NULL                            ; handle of menu, or child-window identifier
        push    NULL                            ; handle of parent or owner window
        push    [WND_HEIGHT]                    ; window height
        push    [WND_WIDTH]                     ; window width
        push    CW_USEDEFAULT                   ; vertical position of window
        push    CW_USEDEFAULT                   ; horizontal position of window
        push    WS_OVERLAPPEDWINDOW             ; window style
        push    _wTitle                         ; address of window name
        push    _wcName                         ; address of registered class name
        push    NULL                            ; extended window style
        call    [CreateWindowEx]
        
        mov     [hwnd],eax
        ;-----------------------------------------------
        ; CENTER THE WINDOW
        ;-----------------------------------------------            
        push    dword [hwnd]                    ; handle of window
        call    CenterWindow
       
        ;------------------------------------------------
        ; SHOW THE WINDOW
        ;------------------------------------------------
        push    SW_SHOW                         ;show state of window
        push    [hwnd]                          ;handle of window
        call    [ShowWindow]    
        ;------------------------------------------------
        ; MESSAGE LOOP
        ;------------------------------------------------
MSG_LOOP:
        
        push    NULL                            ;last message
        push    NULL                            ;first message
        push    NULL                            ;handle of window
        push    MSG_STR                         ;address of structure with message
        call    [GetMessage]
                
        
        cmp     eax,0                           ; If message is WM_QUIT GetMessage returns 0
        je      END_LOOP                        ; Exit loop if we have zero
        cmp     eax,-1                          ; Check for Invalid handle error or somekind
        je      END_ERROR                       ; EXIT LOOP WITH ERROR
        push    MSG_STR                         ;address of structure with message
        call    [TranslateMessage]
        
        push    MSG_STR                         ;address of structure with message
        call    [DispatchMessage]
        jmp  MSG_LOOP                           ;Do it over and over again
END_ERROR:
        ; experimental error handling
END_LOOP:
        push    0                               ;Wat was the last Message parameter 
        call    [ExitProcess]
        
 
        
        ;-------------------------------------------------
        ; WINDOW CALLBACK PROC
        ;-------------------------------------------------

        hWnd    equ     ebp+8
        uMsg    equ     ebp+12
        wParam  equ     ebp+16
        lParam  equ     ebp+20
        
WND_PROC:
        ;==========================================================
        ; First create a stack frame and save register
        ;==========================================================
        push    ebp                       
        mov     ebp,esp
        push    ebx esi edi              
        cmp     dword [uMsg],WM_DESTROY   
        je      ON_DESTROY
        cmp     dword [uMsg],WM_NCHITTEST
        je      ON_NCHITTEST
        cmp     dword [uMsg],WM_PAINT
        je      ON_PAINT
DEFW:   
        push    dword [lParam]
        push    dword [wParam]
        push    dword [uMsg]
        push    dword [hWnd]
        call    [DefWindowProc]
        jmp     EX_IT                                   

ON_DESTROY:
        push    0
        call    [PostQuitMessage]
        
        jmp     EX_IT                     
ON_NCHITTEST:   
        push    dword [lParam]
        push    dword [wParam]
        push    dword [uMsg]
        push    dword [hWnd]
        call    [DefWindowProc]
        cmp eax,1                       
        jnz @F                           
        inc eax                         
@@:
        jmp     EX_IT

ON_PAINT:
        push    ps_hdc
        push    dword [hWnd]
        call    [BeginPaint]
        mov     [hdc],eax
        
        ;calclulate the string lenght
        push    _helloMsg
        call    [lstrlen]
        
        
        push    eax                     ; eax holds string lenght
        push    _helloMsg
        push    10
        push    30
        push    [hdc]
        call    [TextOut]
        
        
        ;- Stop paiting 
        push    [hdc]
        call    [EndPaint]
        jmp     EX_IT   
        
        
EX_IT:  
        ;==========================================================
        ; RESTORE THE STACKFRAME VERY IMPORTANT
        ;==========================================================
        pop     edi esi ebx
        mov     esp,ebp
        pop     ebp
        retn    16
     


lphWnd  equ     ebp+8
lpX     equ     ebp-4
lpY     equ     ebp-8
; Its uses a global rect structure because the local one can't get it 2 work    
CenterWindow:
        push    ebp                                     ; create a  stack frame
        mov     ebp,esp
        sub     esp,8                                   ; make room for local variables
        push    ebx esi edi                             ; save API destructive registers

        lea     edi,dword [RECT_STR]                    ; get adress of rect in edi
        push    edi
        push    dword [lphWnd]
        call    [GetWindowRect]
        
        push    SM_CXSCREEN
        call    [GetSystemMetrics]
        mov     ecx,[rect_right]                        ; Right RECT coordinate
        sub     ecx,[rect_left]                         ; Left  RECT coordinate
        sub     eax,ecx
        shr     eax,1
        mov     dword [lpX],eax
        
        push    SM_CYSCREEN
        call    [GetSystemMetrics]
        mov     ecx,[rect_bottom]                       ; Bottom        
        sub     ecx,[rect_top]  ; TOP   
        sub     eax,ecx                 
        shr     eax,1                   
        mov     dword [lpY],eax
        ;==========================================================
        ; Set The Window position with this API call 
        ;==========================================================
        push    SWP_NOSIZE or SWP_NOZORDER              ; window-positioning flags              
        push    NULL                                    ; height                                
        push    NULL                                    ; width                 
        push    dword [lpY]                             ; vertical position
        push    dword [lpX]                             ; horizontal position                   
        push    NULL                                    ; placement-order handle                                
        push    dword [lphWnd]                          ; handle of window
        call    [SetWindowPos]
        ;==========================================================
        ; RESTORE THE STACKFRAME VERY IMPORTANT
        ;==========================================================
        add     esp,8                   ; restore local space
        mov     esp,ebp                 
        pop     ebp                      
        retn    4                       
section '.idata' import data readable writeable

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

kernel_table:
        ExitProcess             dd RVA _ExitProcess
        GetModuleHandle         dd RVA _GetModuleHandleA 
        lstrlen                 dd RVA _lstrlenA
                                dd 0
user_table:
        MessageBox              dd RVA _MessageBoxA
        LoadIcon                dd RVA _LoadIconA
        LoadCursor              dd RVA _LoadCursorA
        RegisterClassEx         dd RVA _RegisterClassExA
        MessageBoxEx            dd RVA _MessageBoxExA
        CreateWindowEx          dd RVA _CreateWindowExA
        ShowWindow              dd RVA _ShowWindow
        GetMessage              dd RVA _GetMessageA
        TranslateMessage        dd RVA _TranslateMessage
        DispatchMessage         dd RVA _DispatchMessageA
        PostQuitMessage         dd RVA _PostQuitMessage
        DefWindowProc           dd RVA _DefWindowProcA
        BeginPaint              dd RVA _BeginPaint
        EndPaint                dd RVA _EndPaint
        GetWindowRect           dd RVA _GetWindowRect
        GetSystemMetrics        dd RVA _GetSystemMetrics
        SetWindowPos            dd RVA _SetWindowPos
        GetWindow               dd RVA _GetWindow
        MoveWindow              dd RVA _MoveWindow
                                dd 0
gdi_table:
        TextOut                 dd RVA _TextOutA
                                dd 0
                                        
kernel_name                     db 'KERNEL32.DLL',0
user_name                       db 'USER32.DLL',0
gdi_name                        db 'GDI32.DLL',0

_ExitProcess            dw 0            
db      'ExitProcess',0
_MessageBoxA            dw 0            
db      'MessageBoxA',0
_GetModuleHandleA       dw 0            
db      'GetModuleHandleA',0
_LoadIconA              dw 0
db      'LoadIconA',0
_LoadCursorA            dw 0
db      'LoadCursorA',0
_RegisterClassExA       dw 0
db      'RegisterClassExA',0    
_MessageBoxExA          dw 0
db      'MessageBoxExA',0
_CreateWindowExA        dw 0
db      'CreateWindowExA',0
_ShowWindow             dw 0
db      'ShowWindow',0
_GetMessageA            dw 0
db      'GetMessageA',0
_TranslateMessage       dw 0
db      'TranslateMessage',0
_DispatchMessageA       dw 0
db      'DispatchMessageA',0
_PostQuitMessage        dw 0
db      'PostQuitMessage',0
_DefWindowProcA         dw 0
db      'DefWindowProcA',0
_BeginPaint             dw 0
db      'BeginPaint',0
_lstrlenA               dw 0
db      'lstrlen',0
_TextOutA               dw 0
db      'TextOutA',0
_EndPaint               dw 0
db      'EndPaint',0
_GetWindowRect          dw 0
db      'GetWindowRect',0
_GetSystemMetrics       dw 0
db      'GetSystemMetrics',0
_SetWindowPos           dw 0
db      'SetWindowPos',0
_GetWindow              dw 0
db      'GetWindow',0
_MoveWindow             dw 0
db      'MoveWindow',0


section '.reloc' fixups data readable discardable
        
        
        



                       
    

And the re2.inc file.
Code:
NULL                    = 0h
FALSE                   = 0
TRUE                    = 1
CS_HREDRAW              = 2h 
CS_VREDRAW              = 1h
IDI_APPLICATION         = 32512
IDC_ARROW               = 32515
COLOR_WINDOW            = 5h
MB_OK                   = 0
MB_ICONSTOP             = 10h   
GW_OWNER                = 4
WS_OVERLAPPED           = 0h 
WS_CAPTION              = 0C00000h
WS_SYSMENU              = 80000h
WS_THICKFRAME           = 40000h

WS_MINIMIZEBOX          = 20000h
WS_MAXIMIZEBOX          = 10000h
WS_OVERLAPPEDWINDOW     = WS_OVERLAPPED OR WS_CAPTION OR WS_SYSMENU OR WS_THICKFRAME OR WS_MINIMIZEBOX OR WS_MAXIMIZEBOX
CW_USEDEFAULT           = 80000000h
SW_SHOW                 = 5h
WM_DESTROY              = 0002h
WM_NCHITTEST            = 0084h
WM_CREATE               = 0001h
WM_PAINT                = 000Fh
SM_CXSCREEN             = 0
SM_CYSCREEN             = 1
SWP_NOSIZE              = 0001h
SWP_NOZORDER            = 0004h
    

There could be some errors here and there our maybe you have a better idea, just drop a note, and a working example of autorunning a program using registry is welcome Laughing
Post 22 Jul 2005, 00:33
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Jul 2005, 07:46
Format wrote:

Don't really know the Win32API either. So, I guess those two add up for a bad combination for me, at the moment.

yep, you are learning two things at a time, and it will probably and with
learning winapi and not assembly. You wouldn't learn any deeper assembly
under windoze anyway. But you will discover youself when to move back to "real mode" (DOS etc.)

Quote:

So to recap what I think you are saying, that main loop does not check for a WM_QUIT message, therefore, if alt_F4 is pressed , control flow will not even go into the msg loop after that ?

Message loop (not main loop, well it is main in your app, but this is more exact term) take messages from Windows that are meant for windows of your application, and dispatches them to proper window procedures. Windows could do this by himself, but this way you can control dispatching. Message loop usually doesn't look into contents of message, it only finds out for which window is it meant and dispatches it.


But there is one special message, which isn't really meant for any window, but for message loop. GetMessage returns 0 when it gets this message, which usually means to end message loop. and this message is sent by calling PostQuitMessage().

Quote:
...since the app ends up at the Wnd Proc Callback destroy window section, then, where else is "exit process" being invoked from on an non_error event ?

Nope, i only said thi is possibility, not that it really happens.
As I looked at the code, i saw problem might be that window procedure's return value isn't always properly set, which sometimes means for windows that something has gone wrong. All branches must set EAX to proper value (see each handled message description)
Post 22 Jul 2005, 07:46
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Format



Joined: 20 Jul 2005
Posts: 3
Format 23 Jul 2005, 19:09
Hi again


Okay thanks evrybody.


Will take a look at the new file.


Format
Post 23 Jul 2005, 19:09
View user's profile Send private message Reply with quote
Ancient One



Joined: 28 Feb 2005
Posts: 55
Ancient One 26 Jul 2005, 06:48
spidark, here's a sample of creating autorun entry in registry.. i want to make it simple as possible so no error handling..
Code:
format pe gui 4.0
entry start

autorun_key db 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run',0
autorun_val db 'TestAutorun',0

start :
  xor   ebx, ebx
  push  ebx
  call  [GetModuleHandle]

  mov   esi, app_name

  push  256
  push  esi
  push  eax
  call  [GetModuleFileName]

  mov   edi, hKey

  push  edi
  push  KEY_ALL_ACCESS
  push  ebx
  push  autorun_key
  push  HKEY_LOCAL_MACHINE
  call  [RegOpenKeyEx]

  push  esi
  call  [lstrlen]
  inc   eax

  push  eax
  push  esi
  push  REG_SZ
  push  ebx
  push  autorun_val
  push  dword [edi]
  call  [RegSetValueEx]

  push  dword [edi]
  call  [RegCloseKey]

  push  ebx
  call  [ExitProcess]

KEY_ALL_ACCESS=0xF003F
HKEY_LOCAL_MACHINE=0x80000002
REG_SZ=1

data import
  dd 0,0,0,RVA kernel_name,RVA kernel_table
  dd 0,0,0,RVA advapi_name,RVA advapi_table
  dd 0,0,0,0,0
end data
kernel_table:
        ExitProcess             dd RVA _ExitProcess
        GetModuleHandle         dd RVA _GetModuleHandleA
        GetModuleFileName       dd RVA _GetModuleFileNameA
        lstrlen                 dd RVA _lstrlenA
                                dd 0
advapi_table:
  RegOpenKeyEx  dd RVA _RegOpenKeyExA
  RegSetValueEx dd RVA _RegSetValueExA
  RegCloseKey   dd RVA _RegCloseKey
  dd 0

kernel_name                     db 'KERNEL32.DLL',0
advapi_name db 'ADVAPI32.DLL',0

_ExitProcess        dw 0
                    db 'ExitProcess',0
_GetModuleHandleA   dw 0
                    db 'GetModuleHandleA',0
_GetModuleFileNameA dw 0
                    db 'GetModuleFileNameA',0
_lstrlenA           dw 0
                    db 'lstrlenA',0
_RegOpenKeyExA      dw 0
                    db 'RegOpenKeyExA',0
_RegSetValueExA     dw 0
                    db 'RegSetValueExA',0
_RegCloseKey        dw 0
                    db 'RegCloseKey',0
hKey      dd ?
app_name  rb 256
    
Post 26 Jul 2005, 06:48
View user's profile Send private message MSN Messenger Reply with quote
Spidark



Joined: 11 May 2005
Posts: 39
Spidark 26 Jul 2005, 08:39
Ancient One wrote:
spidark, here's a sample of creating autorun entry in registry.. i want to make it simple as possible so no error handling..

Thank you verry much Ancient One.
Going to experiment with your code thanks Very Happy
Post 26 Jul 2005, 08:39
View user's profile Send private message Reply with quote
Raedwulf



Joined: 13 Jul 2005
Posts: 375
Location: United Kingdom
Raedwulf 31 Jul 2005, 07:15
Quote:
Yeah, I'm just starting to learn assembly myself. Just had a few sessions in FASM.

Don't really know the Win32API either. So, I guess those two add up for a bad combination for me, at the moment.


Hello format, .....what language were you using before assembly?

Winapi is essential for Windows Assembly programming, so you need to brush up on winapi first ......maybe it'll be useful if you read iczelion's tutorials and these other ones here if you've not read them already.
http://www.website.masmforum.com/tutorials/

A MMX tutorial for those who are alien to the 'newish' instruction set.
http://za.scene.org/files/1999/tuts/rawtut05.txt
Post 31 Jul 2005, 07:15
View user's profile Send private message MSN Messenger 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.