WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Sponsors
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
 

Array of Structures in ASM, Declaring / Accessing / Manipulating

JimG
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 725
Member No.: 11
Joined: 16-May 04


For my next dumb question.....

I'm trying to set up an array of variables defined by a structure.
For this example I'll use the simple POINT structure.

CODE
.DATA?

TPos    POINT   10 dup (<>)
buffr   db  512 dup (?)

.CODE

   mov TPos[0].x,1
   mov TPos[0].y,2
   mov TPos[1].x,3
   mov TPos[1].y,4
   mov TPos[2].x,5
   mov TPos[2].y,6
   invoke wsprintf,addr buffr,SADD("%lx %lx"),TPos[0].x,TPos[0].y
   invoke MessageBox, 0, addr buffr, 0,0
   invoke wsprintf,addr buffr,SADD("%lx %lx"),TPos[1].x,TPos[1].y
   invoke MessageBox, 0, addr buffr, 0,0
   invoke wsprintf,addr buffr,SADD("%lx %lx"),TPos[2].x,TPos[2].y
   invoke MessageBox, 0, addr buffr, 0,0

These are the values printed out:
QUOTE
50301  60000  503  600  5  6

Clearly not what I had intended...

A portion of the Listing follows:
QUOTE
000001BB  C7 05 00000220 R         mov TPos[0].x,1
    00000001
000001C5  C7 05 00000224 R         mov TPos[0].y,2
    00000002
000001CF  C7 05 00000221 R         mov TPos[1].x,3
    00000003
000001D9  C7 05 00000225 R         mov TPos[1].y,4
    00000004
000001E3  C7 05 00000222 R         mov TPos[2].x,5
    00000005
000001ED  C7 05 00000226 R         mov TPos[2].y,6
    00000006

Looking at the listing, the addresses make no sense to me...
The values are being overlapped at 1 byte intervals.
The results are so ridiculously wrong, that I must have no idea what I am doing.
I tried "dword ptr" all over the place, and many other things.
Everything gives the same answers.

Clearly I've not defined TPos correctly, doesn't MASM know how big POINT is?

OK, now one more question...

This was just preparation for what I really wanted to do...
I want to allocate memory using GlobalAlloc, and then, overlay the definition of an array of structured items.

How does one reference an element of this kind of array?

Say I want to reference the 15th array element, the item called x.
My best guess is to put the starting address in EBX and reference
[EBX][15].x. Is this correct?

I have been unable to find any code in any book or tutorial or in any of the hundreds of example asm programs I have...

PMEmail Poster
Top
akyprian
Quote Post


Administrator
******

Group: Admins
Posts: 2307
Member No.: 1
Joined: 12-May 04


Hi,

Assume the following structure:
CODE
.DATA

STRUCTURENAME STRUCT
Element1 DWORD ?
Element2 DWORD ?
Element3 POINT ?
Element4 DWORD ?
STRUCTURENAME ENDS

.CODE

; Part of the code omitted...
Invoke GetProcessHeap
;Assuming you want 20 items
Invoke HeapAlloc,EAX,HEAP_ZERO_MEMORY,20*SizeOf STRUCTURENAME
;STRUCTURENAME.Element1[EAX] is the first element of the first item

ADD EAX,SizeOf STRUCTURENAME
;STRUCTURENAME.Element3.x[EAX] is x of the third element of the Second item
; Part of the code omitted...


Do not forget to call HeapFree when you are done

I hope this is what you are asking for...

Antonis
PMEmail PosterUsers Website
Top
JimG
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 725
Member No.: 11
Joined: 16-May 04


Thanks akyprian-
CODE
;STRUCTURENAME.Element1[EAX] is the first element of the first item

ADD EAX,SizeOf STRUCTURENAME
;STRUCTURENAME.Element3.x[EAX] is x of the third element of the Second item

I never would have guessed this, it seems the reverse of normal notation...

Also, any idea what was wrong with my declaration:
CODE
TPos POINT 10 dup (<>)

...that made MASM think each item was 1 byte, rather than the size of POINT?
PMEmail Poster
Top
akyprian
Quote Post


Administrator
******

Group: Admins
Posts: 2307
Member No.: 1
Joined: 12-May 04


Hi,
QUOTE
CODE
;STRUCTURENAME.Element3.x[EAX] is x of the third element of the Second item

I never would have guessed this, it seems the reverse of normal notation...

The above is exactly the same with:
CODE
[EAX].STRUCTURENAME.Element3.x

The answer to the other question is that you calculate item positions in bytes.
(I know you have in mind the way we deal with array items in HLL... Just forget about it...).
E.g.:
CODE
TPos[(ItemNo-1)*SizeOf POINT].x
...or...
CODE
TPos[(ItemNo-1)*SizeOf POINT].y

I hope it is OK now,

Antonis
PMEmail PosterUsers Website
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian
Google