0

It is obviously a newbie question, but I am trying to perform a simple loop that simply copies values to rsp-i * 8 where 1<=i<=j, such that j is the values stored in some register:

mov r9, r8 
loop: 
    cmp r9, 0 
    je end_loop_push_list_args 
    mov rax, %SOME VALUE %
    mov qword [rsp - 8 * r9], rax 
    sub r9, 1 
end_loop:

Yet it seems like assembler is not happy with the minus, shouting that:

error: invalid effective address

Thoguh [rsp - 8 * r9] is the desired memory address I wish to override.

Thanks.

MorKadosh
  • 5,846
  • 3
  • 25
  • 37
  • 6
    In x86, you can only compute an effective address as `base + (1/2/4/8)*index + disp`. You're trying to use `-8` as the multiplier of the index; This is not permitted. Instead, convert your code so `r9` is negated and counts up, allowing you to use the (positive) `8` index multiplier. – Iwillnotexist Idonotexist Jan 08 '19 at 07:53
  • 2
    also you are trying to use the "red zone" of stack, which means you should be sure you go down only by so much (128 bytes IIRC in linux), and if you ever later would want to `call` anything from inside, you will have to rewrite the code, so you can maybe save yourself some trouble by simply explicitly adjusting stack at beginning of your function. If considerable chunk of data will be processed, those two extra `sub rsp,xxx` + `add rsp,xxx` shouldn't hurt performance. And then you can use `rsp + ...` addressing, without the need to modify how counters progresses. – Ped7g Jan 08 '19 at 08:21
  • Use `[rsp-128 + 8*r9]` if you want an array starting at the bottom of the red-zone. Or a classic trick is to make `r9` negative and increment it towards zero. But really you're often better off incrementing or decrementing a pointer and avoiding indexed addressing modes in a loop. – Peter Cordes Jan 10 '19 at 11:25

0 Answers0