0

I'm studying x86 assembly, and I've come across this declaration:

array1 DB 5 DUP(2 DUP('*'))

What does this declaration do?

  1. Allocates space for an array called array1, with size DB * 5 * 2 = 10, and 10 * elements.

  2. Allocates space for an array called ærray1, with size DB * 5 and 5 ** elements. This would mean that 5 * elements get discarded.

  3. Allocates a multi-dimensional array called array1, with size [5][2] and 5 {'*', '*'} elements.

So, is this declaration equivalent to

char array1[10] = {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}

or is it equivalent to

char array1[5] = {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}

?

Or maybe:

char array1[5][2] = {{'*', '*'}, {'*', '*'}, {'*', '*'}, {'*', '*'}, {'*', '*'}}

?

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • It almost certainly wouldn't be equivalent to any kind of `int` array, since `DB` means that each element is a byte. – Michael Feb 11 '14 at 15:03
  • @Michael: whoops, fixing the question – Vittorio Romeo Feb 11 '14 at 15:04
  • 1
    I'm fairly certain most assembly languages don't really have the concept of a multi-dimensional array. Also, generally, a `char array[10]` and a `char array[5][2]` are going to be laid out in memory identically - the type difference is an abstraction in the higher-level language. Quite possibly, the original syntax is meant to make sure the byte count is always even, to express the notion of an array of pairs of bytes or something - that way you could conveniently and easily scale your program up to, say 37 pairs... – twalberg Feb 11 '14 at 15:28
  • @twalberg: So in the end the allocated space is 10, correct? – Vittorio Romeo Feb 11 '14 at 15:39
  • 1
    @VittorioRomeo: You could check this by placing a label directly after the declaration of `array1` and then compute the difference between that label and `array1` (either in your code or by looking at the map file). – Michael Feb 11 '14 at 16:19
  • The example is a nested dup, but not a great example, You could have something like array1 db 5 dup (1 dup '1', 2 dup '2', 3 dup '3', 4 dup '4'), which would create 50 bytes of data. – rcgldr Feb 11 '14 at 18:35
  • Could you post your comment as an answer so that I can accept it? Thanks. – Vittorio Romeo Feb 14 '14 at 14:17

1 Answers1

1

According to the comments, the declaration allocates an array with 10 contiguous * character bytes in memory.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416