1

I need to declare a dword array of an undetermined size, how do I do this in x86 assembly?

typically to declare a dword array you would use:

iNumsArray dword 10 dup(?)

But if I need to continuously prompt the user for an infinite amount of integers, until the user types -1, I will need a dword array of an indeterminate size. I suppose one way of accomplishing this would be to push all of the dwords into the stack, keep a count on bytes(+4 each time), and then pop that amount of bytes off the stack and store into array, but that's terribly complicated.

Perhaps you can allocate the mutable array with iNumsArray dword ? dup(?) ? Where the first question mark is though you typically put the number of dwords, aka the length of the array, how do I allocate this without specifying a length?

Chizx
  • 351
  • 2
  • 5
  • 16
  • I'm not sure I understand why it would make any difference whether you declare your array as a number of bytes, or a number of dwords. Allocate the memory dynamically with `malloc`/`realloc` if you need the array to be able to grow to any size. – Michael Mar 20 '15 at 06:24
  • 1
    Usually in assembly you just reserve a large enough buffer for the maximum array size you'd possibly have and have an extra `size` field. – Bregalad Mar 20 '15 at 08:23
  • First, you cannot store infinite count of numbers in finite memory. Secondly, what's the lifetime of your user input values? If you pass entered value as an input to some function and then never use it again, then you don't need aa array at all - only space to hold one integer. Otherwise it'll be a cycling buffer, which starts over and oevrwrites previously entered data when overflowed – Alexander Zhak Mar 20 '15 at 08:36
  • You need dynamic memory, like classic pointers. Every time you add data to the array, you have to reserve some memory, and release it when you remove data. – Jose Manuel Abarca Rodríguez Mar 20 '15 at 15:20
  • I somewhat understand what you're getting at by saying that i must have a finite size, but I think I may just be able to declare like this: `iNumsArray dword ?` – Chizx Mar 20 '15 at 16:45
  • `iNumsArray dword ?` would reserve space for _a single_ `dword` with an undetermined (probably zero) value. – Michael Mar 20 '15 at 17:01
  • Okay you're right, so how do I create a dynamic/mutable array? – Chizx Mar 20 '15 at 17:04
  • Using stack space might be a good way to go, like pushing onto the stack for every input. [Manipulating the Runtime Stack within a procedure](https://stackoverflow.com/q/55171120) may be relevant. – Peter Cordes Apr 04 '21 at 18:19

0 Answers0