Author Topic: Can't index to the stack with a register variable  (Read 9577 times)

Offline paml27

  • Jr. Member
  • *
  • Posts: 37
Can't index to the stack with a register variable
« on: August 18, 2020, 05:38:31 PM »
In the example below rbp is set to the stack pointer at program start.  It's used throughout the program to index to variables on the stack.  I can index to the stack with an immediate value (mov rax,[rbp-88]) but I can't index to the stack with a variable for the offset; the NASM compiler returns "invalid effective address." 

Code: [Select]
mov rbp,rsp
sub rsp,192

mov rax,[rbp-88] ; works

mov rbx,88
mov rax,[rbp-rbx] ; "invalid effective address"


I can get around this limitation by performing math on the pointer:

Code: [Select]
mov rax,rbp
sub rax,rcx
mov rdi,[rax]

but that involves extra instructions. 

There have been some earlier posts here on this question in the 32-bit context, but this is 64-bit NASM. 

How can I use a variable to index into the stack? 

Thanks. 


Offline ig

  • Jr. Member
  • *
  • Posts: 12
Re: Can't index to the stack with a register variable
« Reply #1 on: August 19, 2020, 09:21:17 AM »
It's not possible to subtract registers within the address computation, the instructions (their encoding) don't support it.
The maximum you can do within the address is [register1 + N*register2 + constant] - where N can only be 1, 2, 4, or 8 (or 0 if the second register isn't there at all). It doesn't matter whether the code is 64bit or 32bit, this hasn't really changed.

Sure, you can do [rbp-88] - because that's actually [rbp + (-88)].

If your algorithm allows that (it's often possible to do in loops with the counter variable), you can negate the register value (in your example, via "neg rcx" instruction) somewhere in the beginning, and then work with the negative value. So, you can use mov rdi, [rax+rcx] to access the memory, but of course you need to reverse the other operations (so instead of "inc rcx", you use "dec rcx", you change "add rcx, N" into "sub rcx, N" - etc.)

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Can't index to the stack with a register variable
« Reply #2 on: August 19, 2020, 02:30:12 PM »
Just a complement to ig's response (that is absolutely correct), in your (paml17) example you could do:
Code: [Select]
mov rbx,-88
mov rax,[rbp+rbx]