【问题标题】:error: invalid effective address错误:无效的有效地址
【发布时间】:2016-06-12 04:16:38
【问题描述】:

我不知道为什么我的 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

【问题讨论】:

标签: assembly nasm


【解决方案1】:

简答:将bl 更改为ebx

长答案:在 x86 中,您使用的寻址模式称为SIB(标度索引基),其中有效地址的形式为base + index * scale + displacement,其中baseindex 是通用寄存器比如eaxebxecxedxscale是1、2、4或8,displacement是一个立即数。 (这些组件中的每一个都是可选的。)

bl 不是可用于索引的寄存器之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多