2016-06-12 243 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不是您可以用作索引的寄存器之一。