3

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 ({,,})
stefansixx1
  • 256
  • 1
  • 2
  • 11

2 Answers2

3

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
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • 1
    @stefansixx1 `counter = 1` is an assembly time statement, it's only evaluated by the assembler. It sets a symbol to 1, it doesn't change any variables in your program. You need to use a `MOV` instruction to change a variable when the program is running. – Ross Ridge May 25 '15 at 23:16
  • That was what I was looking for! Just one more thing will REPT reserve memory in succession like DUP operator? – stefansixx1 May 25 '15 at 23:18
  • 2
    @stefansixx1 Nope, the REPT directive just repeats everything up until the ENDM line. The memory is reserved the multiple `NODE {counter,}` statements that it creates in succession. – Ross Ridge May 25 '15 at 23:22
2

While Masm is very flexible and poweful, I do not believe that precisely what you are asking is possible. However, you can absolutely do this:

 array  DB 3 DUP (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)

It's not what you're asking, but then I don't believe that you can do what you are asking to do without cutting and pasting or figuring out a way to tell DUP to make multiple copies of what's inside. What I have above will define three times twenty bytes. Each of those three will have the integer values 0 through 19 in the bytes.

You can also do stuff like this:

 array DB 3 DUP (4 DUP (1), 2 DUP (2), 4 DUP (8))

This defines a region of 3 * 10 bytes total that have 1111228888 repeated three times.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • Thanks for the reply. The project ask for 1..20 values I thought maybe DUP operator stores in some register how many times did it do the operation like ecx with loops, so I could use it like: array dword 20 DUP (ecx). Is it possible? I only have 20 values predefined so it would not be that much of a hassle I just don't want to look dumb. – stefansixx1 May 25 '15 at 21:15
  • As I said, I do not believe that Masm can do what you are asking. I have never seen or heard of an example of DUP being accessible somehow. Clearly it can't be in ECX since it's only part of the pre-processor. – David Hoelzer May 25 '15 at 21:25