1
votes

I don't know why my NASM assembler keeps giving me the error that I got an invalid effective address in my code. The problem lies in the following piece of code: mov eax, dword [lst + (bl * DOUBLE_WORD)]. I'm just trying to add the product of a constant and whatever is stored in the 8-bit BL register to the address value represented by lst. Am I not allowed to do that? Well, in the book I'm reading, this is exactly the way the author does it.

; ************************************************************************
; Assembler: NASM
;
; This program sums the values of all elements of a double word array.
;
; ************************************************************************

section .data
    EXIT_SUCCESS equ 0            ; The exit status code for success
    SYS_EXIT     equ 0x3C         ; The value for the exit system call
    DOUBLE_WORD  equ 4            ; A double word is 4 bytes

    lst          dd  10, 20, 2, 1 ; A 4-element array
    size         db  4            ; The size of the array
    sum          dd  0            ; This is where we're going to store the sum

section .text
global _start
_start:
    nop
    mov    bl, 0 ; The index to keep track of the element we're working with
_loop:
    ; error: invalid effective address
    mov    eax, dword [lst + (bl * DOUBLE_WORD)]
    add    dword [sum], eax
    inc    bl
    cmp    bl, byte [size] ; Compare the index to the size
    jne    _loop           ; If the index value is not equal to the size,
                           ; keep looping

    ; x/dw &sum

    ; exit
    mov    rax, SYS_EXIT
    mov    rdi, EXIT_SUCCESS
    syscall

; ************************************************************************

%if 0

Compile and run:

nasm -f elf64 -F dwarf -g -o demo.o demo.asm -l demo.lst && \
ld -g -o a.out demo.o && \
rm demo.o && \
./a.out

%endif
1
Possible duplicate of Referencing the contents of a memory location. (x86 addressing modes) That was my attempt at writing up a canonical answer to addressing-mode questions.Peter Cordes

1 Answers

6
votes

Short answer: change bl to ebx.

Long answer: in x86, the addressing mode you're using is called SIB (scale index base), where the effective address is of the form base + index * scale + displacement, where base and index are general registers like eax, ebx, ecx, or edx, and scale is 1, 2, 4, or 8, and displacement is an immediate number. (Each of these components is optional.)

bl isn't one of the registers you can use for the index.