Home > Software engineering >  Array of custom Structs MASM Windows API
Array of custom Structs MASM Windows API

Time:01-17

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

CodePudding user response:

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

  • resb doesn't work as that is a NASM directive, not a MASM directive.
  • The .data? (BSS) segment in a Windows PE executable will be zeroed out by the Windows program loader.
  • In DOS the .Data? (BSS) segment would have to be set to zero by the programmer after the program is started. If you didn't zero out the .data? memory then it was possible for uninitialized data to contain garbage that may or may not contain zeroes.
  • Related