5

I'm writing an assembly program which I want to be able to do the (basic) following:

x = 100;
y = int[x]

E.g. the size of y depends on the value of x.

NOTE: I am using NASM instruction set on a 64 bit Ubuntu system.

In assembly I know that the size of an array needs to be declared in the data section of the file e.g.

myvariable resq 1000

The problem is I won't know how big to make it till I have done a previous calculation. What I really want is something like:

mov rax, 100
myvariable resq rax

But that's not allowed right? Just having some confusion over array access/declarations in assembly.

Any pointers appreciated!

phuclv
  • 37,963
  • 15
  • 156
  • 475
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
  • http://stackoverflow.com/questions/8496633/assembly-reserve-array-space-during-program – n0p Dec 14 '11 at 12:37

1 Answers1

3

Your C example is only possible if you declare the array on the stack or if you pull the memory from the heap with malloc or similar. For small values it's perfectly fine (and faster) to use the stack:

mov rax, 100   # 100 elements
shl rax, 3     # multiply with 8, the size of an element
sub rsp, rax   # rsp now points to your array

# do something with the array
mov rbx, [rsp]    # load array[0] to rbx
mov [rsp+8], rbx  # store to array[1]

add rsp, rax   # rsp points to the return address again
TheBotlyNoob
  • 369
  • 5
  • 16
Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • why is the stack faster? do you mean the "allocation" or the "usage"? – n0p Dec 14 '11 at 16:27
  • 2
    The allocation. `malloc` is a system call, it has some overhead, which may be not neglectable (especially if multithreaded). Using the stack requires two arithmetic operations to a register, or even none if you make use of the _red zone_. – Gunther Piez Dec 14 '11 at 16:33
  • Ok, I thought you meant the usage. But however, if the allocation is happening only once then you might not feel a difference so it's better to use `malloc` because it's safer if you don't know the possible values of `rax` – n0p Dec 14 '11 at 16:46
  • Yes, as always it depends. Something like `int[x]` as in the question is only possible on the stack, and the OP gave a value of 100 as an example. – Gunther Piez Dec 14 '11 at 17:20
  • Could you clarify what the ```shl rax, 3```line does? – Pete Hamilton Dec 14 '11 at 18:10
  • It shifts rax left by three bits. Google _shl x86_ and use the first link. – Gunther Piez Dec 14 '11 at 19:51
  • Thanks. I need to be able to keep a record of multiple arrays which could potentially make my stack huge though right? I'm probably going to use malloc instead I think. – Pete Hamilton Dec 15 '11 at 11:15
  • Depends on your compiler, linker, operating system. Here on my box I have a default stack size of 8 MBytes. – Gunther Piez Dec 15 '11 at 12:29
  • 1
    Normally in functions that use alloca / VLA you'd set up RBP as a frame pointer, and restore the stack with `mov rsp, rbp` (or `leave`), instead of having to keep the VLA size around as well. – Peter Cordes Apr 04 '21 at 17:41