2016-06-12 16 views
1

私のNASMアセンブラが私のコードに無効な実効アドレスを持っているエラーを与え続ける理由がわかりません。問題は次のコードにあります:mov eax, dword [lst + (bl * DOUBLE_WORD)]。私は定数と8ビットのBLレジスタに格納されているものの積をlstで表されるアドレス値に加算しようとしています。私はそれを許可されていませんか?まあ、私が読んでいる本の中で、これはまさに著者がやる方法です。エラー:無効な実効アドレス

; ************************************************************************ 
; 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

[メモリの場所の内容を参照する]の重複が考えられます。 (x86アドレス指定モード)](http://stackoverflow.com/questions/34058101/referencing-the-contents-of-a-memory-location-x86-addressing-modes)これは正式な答えを書く私の試みでしたアドレッシングモードの質問。 –

答えて

5

短い答え:ebxに変更bl

長い答え:x86の中で、あなたが使用しているアドレッシングモードがSIB(スケールインデックスベース)と呼ばれ、実効アドレスはbaseindexeaxebxecxのような一般的なレジスタでフォームbase + index * scale + displacement、であり、またはedxであり、scaleは1,2,4または8であり、displacementは即値です。これらの各コンポーネントはオプションです。

blは、インデックスに使用できるレジスタの1つではありません。