assemblystructx86masmmasm32

Array of custom Structs MASM Windows API


This is something I’ve been trying to figure out. I want to make a MASM program that has a custom struct and an array of those structs and then saves it to a file. I can not find any good information on how this should/could be done. I think resb semi-instruction might be helpful. Anyway, this is what I have worked out so far, but obliviously having trouble. Basically long term I want to make a simple DB-like MASM program, but that is a ways off, still in the learning stages.

.386
.model flat, stdcall
option casemap :none

include windows.inc
include user32.inc
include kernel32.inc

.data?
MyStruct STRUCT
    string db 4 dup(0)
    dw1 DWORD ?
    dw2 DWORD ?
    dw3 DWORD ?
MyStruct ENDS

;MyArray MyStruct 20 dup(?)
MyArray resb 10*sizeof MyStruct  ; not sure of how to go about creating an array of structs

.data
    fileName db "myfile.dat"
    properNouns db "John", "Mike", "Emily", "Mary", "David",0
    
.code
        
    ; open the file for writing
    invoke CreateFile,fileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
    mov handle,eax

    ; write the array to the file
    invoke WriteFile,handle,MyArray,sizeof MyArray,written,NULL

    ; close the file
    invoke CloseHandle,handle

Solution

  • You will need to change dup(0) to uninitialized dup(?) in MyStruct. It should look like this:

    .data?
    MyStruct STRUCT
        string db 4 dup(?)
        dw1 DWORD ?
        dw2 DWORD ?
        dw3 DWORD ?
    MyStruct ENDS
    

    In your uninitialized .data? segment you can then use:

    MyArray MyStruct 20 dup (<>)
    

    This should create space for an array of 20 MyStruct structures.


    Notes