Author Topic: invalid effective address w/ sample simple code  (Read 10514 times)

Offline pprocacci

  • Jr. Member
  • *
  • Posts: 11
invalid effective address w/ sample simple code
« on: December 29, 2016, 04:47:37 AM »
I've come across the error (in the subject) that I can't simply get my head around.  I'm hoping someone is able to point me to the correct resolution.

Code: [Select]
STRUC something
.pos1:  RESQ 1
.pos2:  RESW 1
.pos3:  RESW 1
.pos4:  RESD 1
.pos5:  RESQ 1
.pos6:  RESQ 1
ENDSTRUC

BITS 64
DEFAULT REL

global _start:function

SECTION .bss

array: RESB something_size * 100
index: RESD 1

SECTION .text

_start:
        mov     eax, [index]            ; Load index into array
        mov     qword [array + rax * something_size + something.pos5], 1

        mov     eax, 1
        xor     edi, edi
        syscall

In short, this code allocates space for 100 something's named array in the bss segment.
It also allocates a double to store an index into that array.

The logic in start, attempts to store 1 in the array at array index 0 at offset pos5 ..... or so I thought.
What am I missing here?

Note: I also tried different registers given my reading on the net, but I simply cannot get anything to jive.

Thanks in advance!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: invalid effective address w/ sample simple code
« Reply #1 on: December 29, 2016, 06:52:18 AM »
An effective address consists of an (optional) displacement, an (optional) base register, and an (optional) index register multiplied by a scale of 1, 2, 4, or 8. You can't multiply a register by an arbitrary value (like "something_size").

(Your "syscall" doesn't look correct to me, either. 1 is the 32-bit system call number for sys_exit, but 64-bit system call number for sys_write - you want 3Ch for sys_exit I think... but check that!)

Best,
Frank


Offline pprocacci

  • Jr. Member
  • *
  • Posts: 11
Re: invalid effective address w/ sample simple code
« Reply #2 on: December 29, 2016, 07:38:38 AM »
Hey Frank,

Nice see'ing you're still around.

I understand I think.  Thanks for the follow up.

As for the syscall.  It's correct on 64-bit BSD.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: invalid effective address w/ sample simple code
« Reply #3 on: December 29, 2016, 07:58:52 AM »
Ah, BSD! Yeah, that's the same as the 32-bit numbers. A pox on Linux for changing them. I have no idea what they think they gained.

Best,
Frank