News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Array handling

Started by joerbanno, January 29, 2007, 10:02:11 AM

Previous topic - Next topic

joerbanno

Well risking the danger of being called a newB, I still want to post the following question
I have a question about array handling especially for arrays different from byte arrays.
Let me give you an example:

hEventArr  HANDLE 2 dup

when I subsequently want to fill the array
I use the following code

invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov hPendingCommandEv, eax

lea edx ,[hEventArr]
mov eax,hPendingCommandEv
mov  [edx], eax
invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov hConnectEv, eax
mov eax, sizeof HANDLE
lea edx, hEventArr[eax]
mov eax, hConnectEv
mov [edx], eax

I know that the indexing into the array is address of array + indexintoArr*sizeof ArrType + offset.
But my code seems to me is a bit clumsy, surely there must be a more elegant (faster?) way to write to this array.


ramguru

If I know number of array (global var) elements I usually do like this:

invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov DWORD PTR [hEventArr+0], eax
invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov DWORD PTR [hEventArr+4], eax

joerbanno

Quote from: ramguru on January 29, 2007, 10:16:24 AM
If I know number of array (global var) elements I usually do like this:

invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov DWORD PTR [hEventArr+4], eax


Yeah that seems to be a more elegant way, but it has one slight disadvantage.
If I dont use an array of HANDLE(s) wich is on win32 a Dword(?)  but instead use a struct

like this

stFTPCmd STRUCT 4
nFTPCmd DWORD 0
nFTPParam BYTE  MAX_CMDLEN dup(0) 
stFTPCmd ENDS

then the code doesn't work, can I use mov DWORD PTR [hEventArr+sizeof stFTPCMd], eax
or do I have to setup a register first?

ramguru

I see no disadvantage  :bg

.data?
stftp stFTPCmd <>

...

mov    DWORD PTR [stftp.nFTPCmd], eax
...
mov    BYTE PTR [stftp.nFTPParam+0], al
mov    BYTE PTR [stftp.nFTPParam+1], bl
mov    BYTE PTR [stftp.nFTPParam+2], cl
mov    BYTE PTR [stftp.nFTPParam+3], dl

joerbanno

Quote from: ramguru on January 29, 2007, 11:10:38 AM
I see no disadvantage  :bg

.data?
stftp stFTPCmd <>

...

mov    DWORD PTR [stftp.nFTPCmd], eax
...
mov    BYTE PTR [stftp.nFTPParam+0], al
mov    BYTE PTR [stftp.nFTPParam+1], bl
mov    BYTE PTR [stftp.nFTPParam+2], cl
mov    BYTE PTR [stftp.nFTPParam+3], dl


obviously U know something that I don't

But how do U reference the second array entry in this way?

joerbanno

But if I have

FTPQueue stFTPCmd MAX_CMDQUEUE dup (<>)

and

stFTPCmdQueue STRUCT 4
   FTPQueueItem stFTPCmd MAX_CMDQUEUE dup (<>)
stFTPCmdQueue ENDS

How do I reference the second entry in FTPQueue?


ramguru

Don't know if that is going to work (you might have to calc. (sizeof stFTPCmd+1) by yourself)

    mov    DWORD PTR [stqit.FTPQueueItem.nFTPCmd+sizeof stFTPCmd], eax
    mov    BYTE PTR [stqit.FTPQueueItem.nFTPParam+sizeof stFTPCmd+0], bl
    mov    BYTE PTR [stqit.FTPQueueItem.nFTPParam+sizeof stFTPCmd+1], cl

joerbanno


Shantanu Gadgil

Hi joerbanno,
i guess what you want to do is something similar to  :bg :bg:
struct data {
  int first;
  int second;
}mydata[MAX_NUM];
...
for(i=0; i<MAX_NUM; i++)
{
    mydata[i].first = 1;
    mydata[i].second = 2;
}


The code should be such that you just have to fiddle with the "populating loop" least, right? So how about this:

INFOSTRUCT struct
  DWORD first ?
  DWORD second ?
INFOSTRUCT ends

...
.data?
mydata INFOSTRUCT MAX_NUM dup {(?)}

.code
...
mov ebx, offset mydata
xor ecx, ecx
.while ecx < MAX_NUM
  push ebx
  push ecx

  ;trying to calculate 'offset' into the array
  xor edx, edx
  mov eax, sizeof INFOSTRUCT
  mul ecx

  ;now ebx + eax would point to the mydata[i]'th element
  add ebx, eax
  mov (INFOSTRUCT ptr [ebx]).first, 1 ;or whatever
  mov (INFOSTRUCT ptr [ebx]).second, 2 ;or whatever

  pop ecx
  inc ecx
  pop ebx
.endw


Of course if you has a char array as a member of the struct, then you would need to work with the 'addr' of the child variable. (say for lstrcpy or something)

Does this help?

Regards,
Shantanu

P.S. Code could have some obvious bugs/issues, not on a Windows' box, right now!!!  :bg :bg
To ret is human, to jmp divine!

joerbanno

thnx Shantanu

the INFOSTRUCT ptr in mov (INFOSTRUCT ptr [ebx]).first, 1 is necessary to be able to use [edx].first?

Shantanu Gadgil

No...not necessary, you should be able to use assume too!

Regards,
Shantanu
To ret is human, to jmp divine!