The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: joerbanno on January 29, 2007, 10:02:11 AM

Title: Array handling
Post by: joerbanno on January 29, 2007, 10:02:11 AM
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.

Title: Re: Array handling
Post by: 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+0], eax
invoke CreateEvent,NULL,FALSE,FALSE,NULL
mov DWORD PTR [hEventArr+4], eax
Title: Re: Array handling
Post by: joerbanno on January 29, 2007, 11:02:21 AM
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?
Title: Re: Array handling
Post by: 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
Title: Re: Array handling
Post by: joerbanno on January 29, 2007, 11:37:59 AM
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?
Title: Re: Array handling
Post by: joerbanno on January 29, 2007, 12:57:21 PM
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?

Title: Re: Array handling
Post by: ramguru on January 29, 2007, 01:06:34 PM
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
Title: Re: Array handling
Post by: joerbanno on January 29, 2007, 01:25:39 PM
Thnx for your help!
Title: Re: Array handling
Post by: Shantanu Gadgil on January 29, 2007, 01:51:53 PM
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
Title: Re: Array handling
Post by: joerbanno on January 29, 2007, 02:15:50 PM
thnx Shantanu

the INFOSTRUCT ptr in mov (INFOSTRUCT ptr [ebx]).first, 1 is necessary to be able to use [edx].first?
Title: Re: Array handling
Post by: Shantanu Gadgil on January 29, 2007, 06:47:41 PM
No...not necessary, you should be able to use assume too!

Regards,
Shantanu