2016-06-12 198 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

簡短回答:將bl更改爲ebx

龍答:在x86上,您使用的尋址模式被稱爲SIB(規模指數的基礎),其中有效地址的形式爲base + index * scale + displacement,其中baseindex是通用寄存器像eaxebxecx,或edxscale是1,2,4或8,並且displacement是立即數。 (這些組件中的每一個都是可選的。)

bl不是您可以用作索引的寄存器之一。