17

I have this code here, but I'm unfamiliar with the syntax.

STACK16_SIZE    =       100h
stack16         db      STACK16_SIZE dup (?)

I think dup means we declare a variable of type array, as this is a stack, but I'm not sure. So what does dup mean in TASM, exactly?

sashoalm
  • 75,001
  • 122
  • 434
  • 781

3 Answers3

24

STACK16_SIZE dup (?) means to duplicate the data in parenthesis by STACK16_SIZE times. It is equivalent to writing ?, ?, ?, ?, ... (100h times)

The data in parens is "uninitialized data". That is, memory is allocated, but not set to any particular value on load.

Assembly does not provide an array "type". If it does, it is only for debuggers for use when inspecting the data. However, in this code snippet, stack16 is a symbol with an address beginning a memory block of bytes—which is counter-intuitive and potentially a source of a subtle bug. For a CPU stack, it really ought to be defined as 16 bit words (dw) or 32 bit words (dd).

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • But what type is stack16? I'm coming from C background, so I was thinking in terms of arrays. Or is it like a placeholder for a memory address? I.e. all variables in TASM are like `void*` and can point to any kind of data. So here stack16 points to the first element of an array of 100 bytes. Is that correct? – sashoalm Apr 11 '13 at 18:34
  • 1
    Never mind, I found a good explanation at http://www.csi.ucd.ie/staff/jcarthy/home/alp/alp5.html – sashoalm Apr 11 '13 at 18:46
  • ARR1 DB 5 DUP(2) what do u think this expression ? what is value inside of array ? – muco Jan 25 '17 at 08:38
  • @muco: That `5 dup(2)` produces 5 elements, each one having value 2. Since it's an arg to `db`, the element size is 1 byte. – Peter Cordes Feb 15 '18 at 08:22
  • 1
    And BTW, "uninitialized" doesn't mean random / garbage value. Under OSes like Windows, memory in the BSS is zero-initialized, and MSVC will emit asm like `foo db 100 dup(?)` for C like `static foo[100] = {0};`, because `dup(?)` does guarantee zero values for all the elements. (But those bytes aren't stored inside the executable, just a start / length for the whole BSS segment. IDK if Windows even calls it a BSS; that's Linux / Unix terminology, but same idea). – Peter Cordes Feb 15 '18 at 08:25
11

Let's start with a different example. You can read 20 DUP (0) as "twenty duplicates of zero". The whole expression INPUTSTR DB 20 DUP (0) is equivalent to INPUTSTR DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.

Now, because ? means "uninitialized value", 20 DUP (?) would give you 20 uninitialized bytes. And, in this specific case, where you have STACK16_SIZE DUP (?), you would get STACK16_SIZE uninitialized bytes.

This syntax is not TASM-specific. MASM supports it as well; take a look into the official MASM reference by Microsoft.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
rkhb
  • 14,159
  • 7
  • 32
  • 60
10

? means no particular value, uninitialized. DUP means duplicate.

So you get 100h bytes that are uninitialized.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180