flat assembler
Message board for the users of flat assembler.

Index > Windows > For loop in FASM

Author
Thread Post new topic Reply to topic
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Hello all.
I need some help converting a C/C++ code to FASM

C/C++ code

Code:

int count;               //---> Ignore this

char itemBuff[100]; //---> Ignore this

count = SendDlgItemMessage( hDlg, IDR_LIST, LB_GETCOUNT,0,0);

for ( int index = 0; index < count; index++ ){  //---> this is it



SendDlgItemMessage (hDlg, IDR_LIST, LB_GETTEXT, index, (LPARAM)itemBuff );

/*
Do some stuff with buffer
*/


}


    


How can i do a 'For Loop' in FASM, if it is any safe.

If there's also another simpler and safer way please let me know.

I used the following,below ( in FASM) but the results were just not what i expected.

Code:
;After i have called SendDlgItemMessage. Message being : LB_GETCOUNT
; Store count so i dont lose it

MOV [count_var],EAX

CMP EAX, LB_ERR ;If an error occured or nothing in the list
JE .Error

JMP .GetItems

.GetItems:

CMP EAX,-1; Or LB_ERR
JE .Error

;Call SendDlgItemMessage Message Being : LB_GETTEXT, with Parameters index and pointer to multibye variable.

DEC EAX
    


Can you please help me on this one.
Thanks. Very Happy
Post 05 Aug 2010, 03:24
View user's profile Send private message Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
Please help me the same! Very Happy I'm sorry...

How To Get The Screen Resolution In Pixels?
http://cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/
Fasm?

---
And how to get $CmdLineRaw aka *% in batfiles in fasm/msdn valid mode?
What's the api?
Post 05 Aug 2010, 04:41
View user's profile Send private message ICQ Number Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Here


Code:

format PE CONSOLE

include 'win32ax.inc'

entry start

section '.text' data readable executable

start:

invoke GetHandle,0 ; Sorry i always use this

invoke GetDesktop

mov [hDesktop],eax

invoke GetRect,[hDesktop],lpRect

invoke printf,msg,[lpRect.right], [lpRect.bottom],[lpRect.top] ,[lpRect.left]

invoke system,'pause>null'

invoke Return,0

section '.data' data writable readable

lpRect RECT ?

msg DB 'Right %d, Bottom %d, Top %d, Left %d',0

hDesktop DD ?

section '.idata' import data readable



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

import user,\
          GetRect,'GetWindowRect',\
          GetDesktop,'GetDesktopWindow'
          

import msvcrt,\
         system,'system',\
         printf,'printf'
         
         
import kernel,\
          GetHandle,'GetModuleHandleA',\
          Return,'ExitProcess' 


    


wrote that ontop of my head....


For me Output is

Right 1152, Bottom 864, Top 0, Left 0

which is 1152 by 664 px
Post 05 Aug 2010, 05:10
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
semiono wrote:
Please help me the same! Very Happy I'm sorry...

How To Get The Screen Resolution In Pixels?
http://cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/
Fasm?


Hi semionio,

Code:
        format PE CONSOLE

        include 'win32ax.inc'
        
.data
        horizontal dd 0
        vertical dd 0
        buffer rb 256

.code
main:
        stdcall GetDesktopResolution, horizontal, vertical
        cinvoke wsprintf, buffer, 'Resolution:%dx%d', [horizontal], [vertical]
        invoke MessageBox, HWND_DESKTOP, buffer, '', MB_OK
        ret


proc GetDesktopResolution horizontal, vertical
        
        locals
                rc RECT
        endl

        pushad
        invoke GetDesktopWindow
        invoke GetWindowRect, eax, addr rc
        mov eax,[rc.right]
        mov ecx,[rc.bottom]
        mov esi,[horizontal]
        mov edi,[vertical]
        mov [esi], eax
        mov [edi], ecx
        popad
        ret
endp

.end main
    


typedef wrote:
Hello all.
How can i do a 'For Loop' in FASM, if it is any safe.


Hi,

Don't forget, calling an API function changes registers eax ecx edx contents.
Here's a simple FOR loop.
Code:
       mov ecx, [count]
@@:
 push ecx

           ; invoke SomeApiFunction

     pop ecx
     dec ecx
     jnz @B
    
Post 05 Aug 2010, 06:34
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
You can also save a byte by using loop instruction...
Also you can push all GPR at no extra cost (assuming use32)
Code:
mov ecx, [count]
@@: 
        pusha

           ; invoke SomeApiFunction 

        popa
        loop @b
    
Post 05 Aug 2010, 17:28
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Having this definition of for loop:
Code:
for (expr1; expr2; expr3){ body }    
Now the general pattern to write that in assembly is:
Code:
; Assembly code representing expr1 here (for instance mov [count], 0)
.for_loop_check:
; Assembly code representing expr2 that jumps to .exit_for_loop when false (for instance cmp [count], TOP_VALUE / jae .exit_for_loop when expr2 is "count < TOP_VALUE"). Note that if expr2 is absent then so is the jump.

; body code here

; Assembly code representing expr3 here (for instance inc [count])
jmp .for_loop_check

.exit_for_loop:    


Of course you are not forced to do this way always (in fact what the others suggested is OK, although I would use a non volatile register for count and the dec reg/jnz pair instead), but the pattern above should always work for arbitrary for loops (including those that are not behaving as Pascal-like for loops).
Post 05 Aug 2010, 20:02
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Code:

 push    [count]         ;count dd
           

@@: invoke SomeApiFunction,A1,A2,...,Ax

     dec     dword[esp]
  jnz     @B
  add     esp,4 
Smile
    

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 05 Aug 2010, 22:08
View user's profile Send private message Send e-mail Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
typedef, big thanks! Picnic, big thanks!

Picnic,
; format PE CONSOLE
...
; ret
invoke ExitProcess,0
Cool


Last edited by semiono on 09 Aug 2010, 13:40; edited 1 time in total
Post 06 Aug 2010, 09:48
View user's profile Send private message ICQ Number Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
Code:
invoke  GetSystemMetrics,SM_CXSCREEN
        invoke  wsprintf,lpOut,'%d',SM_CXSCREEN

        invoke RegCreateKeyEx,HKEY_LOCAL_MACHINE,'Software\azz',\
               NULL,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE or KEY_READ,NULL,phkResult,NULL

        invoke  RegSetValueEx,phkResult,'\lpValueName',NULL,'REG_SZ',lpOut,100
        invoke  RegCloseKey,phkResult
exit:
        invoke  ExitProcess,0

section '.data' data readable writable

phkResult dw ?
lpOut rb 1024    


Please, i need keep SM_CXSCREEN in registry value. How to convert SM_CXSCREEN ?

_________________
Memory=8388608
Post 09 Aug 2010, 13:36
View user's profile Send private message ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
cinvoke wsprintf,lpOut,'%d',eax
Post 09 Aug 2010, 17:41
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
semiono wrote:
typedef, big thanks! Picnic, big thanks!

Picnic,
; format PE CONSOLE
Cool


You're welcome, quite right i miss that Smile
Post 09 Aug 2010, 17:52
View user's profile Send private message Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
Code:
include '%fasm%\win32ax.inc'
section '.code' executable
start:
        invoke RegCreateKeyEx,HKEY_LOCAL_MACHINE,'Software\$$debug',\
               NULL,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE+KEY_READ,NULL,phkResult,NULL
        invoke RegSetValueEx,HKEY_LOCAL_MACHINE,NULL,NULL,NULL,addr lpData,cbData
        invoke RegCloseKey,phkResult
exit:
        invoke ExitProcess,NULL

section '.data' readable writable
        lpData db 'ntdll.dll',0
        cbData dw 1024
        phkResult dd ?

.end start    

Anybody, why RegSetValueEx not work? Sad
Post 10 Aug 2010, 14:51
View user's profile Send private message ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
At first glance i see this fix..
Code:
cbData dd 9
...
invoke RegSetValueEx,HKEY_LOCAL_MACHINE,NULL,NULL,REG_SZ
,lpData,[cbData]    

addr is only required when data is relative to local stack frame.
Since its global memory there is need for it.
Also cbData should be passed by value (not pointer)
Also you should specify a "type" field value.
There may be more problems but i have no time to try...
Post 10 Aug 2010, 17:39
View user's profile Send private message Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
Full work example! Smile

Code:
include '%fasm%\win32ax.inc'
section '.code' executable
start:
        invoke RegOpenKeyEx,HKEY_CURRENT_USER,\
               'Software\Microsoft\Windows\CurrentVersion\Applets\Regedit',\
               NULL,KEY_WRITE,phkResult

        invoke RegSetValueEx,[phkResult],'LastKey',NULL,REG_SZ,\
               'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run',76

        invoke RegCloseKey,[phkResult]


@@:     invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,\
               'Software\Microsoft\Windows\CurrentVersion\Run',\
               NULL,KEY_WRITE,phkResult

        invoke RegSetValueEx,[phkResult],'Explorer.exe',NULL,REG_SZ,\
               '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}',41

        invoke RegCloseKey,[phkResult]

exit:
        invoke WinExec,"regedit.exe",SW_SHOWNORMAL
        invoke ExitProcess,NULL

section '.data' readable writable
        phkResult dd ?

.end start

section '.rsrc' resource readable
        directory RT_ICON,icons,RT_GROUP_ICON,group_icons,RT_VERSION,versions

        resource icons,\
        1,LANG_NEUTRAL,icon_data1,\
        2,LANG_NEUTRAL,icon_data2,\
        3,LANG_NEUTRAL,icon_data3,\
        4,LANG_NEUTRAL,icon_data4
        resource group_icons,17,LANG_NEUTRAL,main_icon
        resource versions,1,LANG_NEUTRAL,version

        icon main_icon,\
        icon_data1,'%fasm%\exec1.ico',\
        icon_data2,'%fasm%\exec2.ico',\
        icon_data3,'%fasm%\exec3.ico',\
        icon_data4,'%fasm%\exec4.ico'

        versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH+SUBLANG_DEFAULT,0,\
                    'FileDescription','RegSetValueEx.exe',\
                    'LegalCopyright','2001-2005 GmbH',\
                    'FileVersion','1.0.0.0',\
                    'ProductVersion','1.0.0.0',\
                    'OriginalFilename','RegSetValueEx.exe',\
                    'Company','Semiono'
    
I love fasm!
Post 11 Aug 2010, 14:04
View user's profile Send private message ICQ Number 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.