I'm having some trouble with a line of code, but I'm not sure what is wrong with it. This is for a simple program in my CSE class using assembly. For this part, I am supposed to calculate x^2 (where x is a value originally stored in %rdi). Here is the code segment that is giving me trouble:
movq %rdi, %rax
imulq %rdi # %rax = %rdi * %rax = x^2
movq %rax, %r10 # %r10 = x^2
movq (%r10,%r10,4), %r11 # SEG FAULT OCCURS HERE
# %r11 = x^2 + 4 * x^2 = 5x^2
As a note, I need x^2 to be in all of these registers for reasons outside of the code I am sharing.
I ran this code through the debugger, and %rdi, %rax, and %r10 all have x^2 stored in them. So why would the last line return a seg fault error?