Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Arrays In x86 Assembly

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.

like image 926
CMilby Avatar asked Apr 09 '15 17:04

CMilby


People also ask

How can arrays be declared?

Declaring ArraysArray variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array. Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.

What is x86 assembly used for?

x86 assembly language is the name for the family of assembly languages which provide some level of backward compatibility with CPUs back to the Intel 8008 microprocessor, which was launched in April 1972. It is used to produce object code for the x86 class of processors.


1 Answers

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
like image 74
lurker Avatar answered Sep 21 '22 23:09

lurker