Search code examples
assemblymasmmasm32

Where does DUP operator keeps counting number?


I need to initialize an array of structure in .DATA part of the program. Most are initialized with zero but I need to set up order number. Can I do that in .DATA part with using a register that stores DUP operator to initialize the order number of array elements. Or is there another way beside using loops in .CODE part of the program.

Here is the example program, during the initialization of three each NODEi_KEY must be set to 1..20. The project demands that it be set in .DATA part, if it's not possible it may be a typo.

.DATA

NODE STRUCT
NODEi_KEY DWORD ?
NODEi_VALUE DWORD 0
NODE ENDS

THREE NODE 20 DUP ({,,})

Solution

  • You can do what you want, but you can't do it with the DUP operator. You'll need to use the REPT (repeat) directive instead and create your own counter:

        .DATA
    
    NODE    STRUCT
        NODEi_KEY DWORD ?
        NODEi_VALUE DWORD 0
    NODE    ENDS
    
    THREE   LABEL   NODE
    counter = 1
        REPT    20
            NODE    {counter,}
        counter = counter + 1
        ENDM        
    

    This creates an array of 20 NODE structures with each NODEi_KEY member initialized with its one-based position in the array.

    The REPT directive simply repeats everything up to ENDM as many times given by the argument. So if you were to change the REPT directive so the argument is only 4, it would generate the following:

        NODE    {counter,}
    counter = counter + 1
        NODE    {counter,}
    counter = counter + 1
        NODE    {counter,}
    counter = counter + 1
        NODE    {counter,}
    counter = counter + 1