flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to create an "array" of structures?

Author
Thread Post new topic Reply to topic
chriscurl



Joined: 20 May 2020
Posts: 9
chriscurl 11 Aug 2026, 23:08
I want to create an "array" of structures like this

DWORD xt
BYTE flags, len
BYTE name[26]

Such that each entry is 32-bytes long

How would I set up a macro to do that?

Here is what I have curently, I feel like it just needs a tweak (and to get rid of the link 'last_de'')

; Macro to create a dictionary entry
last_de equ 0
macro dictEntry nameId, name, len, flags {
dict_##nameId:
dd last_de
dd XT_##nameId
db flags, len, name, 0
last_de equ dict_##nameId
XT_##nameId:
}
Post 11 Aug 2026, 23:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21107
Location: In your JS exploiting you and your system
revolution 12 Aug 2026, 02:10
The code doesn't match the description. There is an extra field at the beginning "dd last_de".

Is "dd XT_##nameId" intended to be a link to the next structure? It looks like a doubly linked list, is that the intention?

BTW: struc might be a better choice.
Code:
last_de equ 0
struc dictEntry name, len, flags {
    dict_#.:
        dd      last_de
        dd      XT_#.
        db      flags, len, name
        times   31 - ($ - dict_#.) db 0
        db      0
        last_de equ dict_#.
    XT_#.:
}

name1 dictEntry 'test',1,2
name2 dictEntry '123456789012345678901',1,2     ; maximum length
name3 dictEntry '1234567890123456789012',1,2    ; too long    


Last edited by revolution on 12 Aug 2026, 02:12; edited 1 time in total
Post 12 Aug 2026, 02:10
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1237
Location: Russia
macomics 12 Aug 2026, 02:11
Code:
xt rd 1
flags rb 1
len rb 1
name rb 26

; Macro to create a dictionary entry
last_de equ 0
struc dictEntry name, len, flags {
  local next
  dict_#.: dd last_de
  .next dd next
  .flags db flags
  .len db len
  .name db name
  times 27 - ($ - .name) db 0
  next:
  last_de equ dict_#nameId
}

virtual at ebx
  NextNameId dictEntry ?, ?, ?
end virtual

NewNameId dictEntry "abracadabra", 11, 0
TestData dd 1234, 5678
SomeNameId dictEntry "abracadabra", 11, 0

lea eax, [dict_NewNameId.name]
movzx ecx, [dict_NewNameId.len]
mov dl, [dict_NewNameId.flags]
mov ebp, [dict_NewNameId.next] ; TestData
mov ebx, [dict_NewNameId] ; SomeNameId
mov edi, [dict_NextNameId]
lea esi, [dict_NextNameId.name]
mov dh, [dict_NextNameId.flags]
mov ch, [dict_NextNameId.len]    


revolution wrote:
Is "dd XT_##nameId" intended to be a link to the next structure? It looks like a doubly linked list, is that the intention?

"dd XT_##nameId" just points to the end of the structure. If they go in a row, then it duplicates the first value. What if there are gaps between these structures?


Last edited by macomics on 12 Aug 2026, 02:33; edited 2 times in total
Post 12 Aug 2026, 02:11
View user's profile Send private message Reply with quote
chriscurl



Joined: 20 May 2020
Posts: 9
chriscurl 12 Aug 2026, 02:27
revolution wrote:
The code doesn't match the description. There is an extra field at the beginning "dd last_de".

Is "dd XT_##nameId" intended to be a link to the next structure? It looks like a doubly linked list, is that the intention?

BTW: struc might be a better choice.
Code:
last_de equ 0
struc dictEntry name, len, flags {
    dict_#.:
        dd      last_de
        dd      XT_#.
        db      flags, len, name
        times   31 - ($ - dict_#.) db 0
        db      0
        last_de equ dict_#.
    XT_#.:
}

name1 dictEntry 'test',1,2
name2 dictEntry '123456789012345678901',1,2     ; maximum length
name3 dictEntry '1234567890123456789012',1,2    ; too long    

yes, it is currently a linked list of variable-length dictionary items for a Forth dictionary. The XT_# is the pointer to the code, currently right after the (null-terminated) name. I want to remove the link (the 'last_de') and make each entry the same size, 32 bytes. This will also now be placed in a data area with only the entries, and the XTs will point to addresses in the code area. That will make it exceedingly easy to navigate through the dictionary.

It looks like the 'times 31-($-dict_#)' is the trick I need, thanks!
Post 12 Aug 2026, 02:27
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1237
Location: Russia
macomics 12 Aug 2026, 02:33
chriscurl wrote:
I want to remove the link (the 'last_de') and make each entry the same size, 32 bytes.
If all the structures are the same size and go only in order, then you don't need pointers to the next one. It will just be an array and can be addressed by indexes multiplied by the length of the structure.

Then the pointer to the next one can be found by simply adding the length of the structure to the base pointer. Just like the previous one.

ADD:
Code:
struc dictEntrySameLen [name, len, flags] {
  .flags db flags
  .len db len
  .name db name
  times 30 - ($ - .name) db 0
}

struc dictEntryVarLen [name, len, flags] { .:
  local .next
  .next dd next
  .flags db flags
  .len db len
  .name db name, 0
  next:
}

dictArraySameLen dictEntrySameLen \
    "abracadabra", 11, 0,\
    "banana", 6, 0

dictArrayVariableLen dictEntryVarLen \
    "abracadabra", 11, 0,\
    "banana", 6, 0

lea esi, [dictArraySameLen.name] ; "abracadabra"
lea ebx, [dictArraySameLen.name + 32 * 1] ; "banana"

lea esi, [dictArrayVariableLen] ; "abracadabra"
mov ebx, [dictArrayVariableLen.next] ; "banana"    
Post 12 Aug 2026, 02:33
View user's profile Send private message 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-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.