2015-04-21 95 views
0

我有一个数组名为a和恒定的命名,我试图解决与以下行二维数组n:mov al, [a+ebx*n+esi] 的问题是,如果n为偶数(n equ 4),它完美,而是如果n是奇数(n equ 3),编译器会给出“错误:无效的有效地址”。我可以理解,如果它在两个情况下工作或失败,但我不明白他们为什么工作不同。NASM无效地址行为?

编译器:NASM
接头:GCC(MinGW的用于Windows)
IDE:SASM

程序:

%include "io.inc" 

section .data 
a db 1, 2, 3, 4 
    db 5, 6, 7, 9 
    db 9, 10, 11, 12 
    db 13, 14, 15, 16 
n equ 4 

section .text 
global CMAIN 
CMAIN: 
    mov ebp, esp; for correct debugging 
    ; write your code here 
    ; 
    ; get upper limit 
    xor eax, eax 
    mov edi, n 
    ; 
    ; get last column 
    mov esi, n-1 
    xor ebx, ebx 
    xor edx, edx ; count in DL 
    xor ecx, ecx ; sum in CX 
    mov dh, 3 
cycle: 
    xor ah, ah 
    mov al, [a+ebx*n+esi] 
    div dh 
    cmp ah, 0 
    jne afteradd 
    add cl, [a+ebx*n+esi] 
    add dl, 1 
afteradd: 
    add ebx, 1 
    cmp ebx, edi 
    jl  cycle 
solve: 
    mov ax, cx 
    div dl  ; среднее арифметическое будет в AL 
aftercycle: 
    xor eax, eax 
    ret 
+0

使用3 * ebx =“push ebx”,“lea ebx,[ebx + ebx * 2]”,“mov al,[ebx + esi + a]”,.....“添加cl,[ebx + esi + a]“,.....,afteradd:”pop ebx“,”add ebx,1“... –

回答

2

偏移的地址的一部分的形式base + index*scale + displacement上给出。偏移量的scale部分仅允许使用某些值。这些是1(默认),2,48

Intel's Software Developers Manual第1卷中描述了(名为部分指定偏移):

The offset part of a memory address can be specified directly as a static value (called a displacement) or through an address computation made up of one or more of the following components:
Displacement — An 8-, 16-, or 32-bit value.
Base — The value in a general-purpose register.
Index — The value in a general-purpose register.
Scale factor — A value of 2, 4, or 8 that is multiplied by the index value.

(以上报价是用于32位模式,但在比例因子相同的限制适用于64位模式)

+0

(上面的引用对于16位模式是附加的使用80386+的操作数大小/地址大小前缀。) –