7

I am learning Assembly and I need to make a large array. I have looked around at how to declare arrays and I have come across this.

array db 10 dup(?)

Where an array of 10 uninitialized bytes is declared. I tried this and tried to assemble it and get "error: comma expected after operand 1". I realized that the '?' is not supported in x86 so I made it a constant and got the same error. I ended up doing this.

array db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

This worked! But the problem is, I need large arrays (~100-400 integers) and their values will not always be known. I could write out 400 0's but I figured there must be an easier way. So is there a better way to declare large arrays?

I am using x86_64 Assembly on an Intel-Based Macbook Pro with AT&T syntax.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
CMilby
  • 624
  • 1
  • 6
  • 23
  • 2
    Based upon the `array TIMES...` answer working for you, I assume you are using `nasm` (Intel syntax) not AT&T syntax? – lurker Apr 09 '15 at 21:26
  • If you are using NASM assembler then "array db 10 dup(?)" should work as it is perfectly legal. – Kaustav Apr 25 '16 at 16:37
  • 1
    @Kaustav. No it isn't, maybe you're thinking of MASM. [This errors on NASM](https://stackoverflow.com/questions/29545696/declaring-arrays-in-x86-assembly#comment98017937_29561021). – Peter Cordes Apr 13 '19 at 12:49

3 Answers3

11

The AT&T syntax is used by the GNU assembler. The directive you're looking for is .fill <count>\[, <data-size>\[, <value>\]\]. In the specific case of 400 bytes:

array:  .fill  400

data-size defaults to 1 (byte). I believe the value that fills the 400 bytes defaults to zero.


If you are actually using the nasm assembler (which is Intel format, not AT&T), then the times directive will work, as avinash indicated, as long as you want to predefine the data in either the .text or .data section. However, if you need to reserve bytes in the .bss section (in nasm), you can use the resb (reserve byte) directive:
       setion .bss
       ...
arr1   resb  400             ; Reserve 400 bytes (uninitialized)
arr2   times 400 resb 1      ;  Same thing, using times
lurker
  • 56,987
  • 9
  • 69
  • 103
7

Did you try TIMES directive.Use this code for declaring an array of a given size.

array TIMES 8 DB 0

This will create an array of size 8

Refer to this link for more.

Jacques Ramsden
  • 801
  • 5
  • 20
avinash pandey
  • 1,321
  • 2
  • 11
  • 15
0

It actually depends on which assembler you use. I am used Netwide assembler and the following syntax is working perfectly for me

arr DD 100 DUP(?) ; declaring 100 uninitialised 4 byte words

router
  • 582
  • 5
  • 16
  • 1
    This is MASM syntax. Tested in NASM version 2.14.02, and as expected I get an error: `error: comma expected after operand 1`. – Peter Cordes Apr 13 '19 at 12:49
  • (NASM 2.15 and 2.16 added more MASM compatibility syntax features so this might work in more recent NASM. https://nasm.us/doc/nasmdoc6.html#section-6.5 / https://nasm.us/doc/nasmdoc3.html#section-3.2.1) – Peter Cordes Apr 02 '23 at 20:48