LEA指令生成的地址不正确

时间:2015-11-26 01:52:09

标签: macos assembly nasm

在这个OS / X NASM代码中,我想计算 .data 部分中定义的数组中的正,负和零。我想相应地存储结果。

SECTION .data
    align 4
    numdata db 0x12, 0x88, 0x82, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x87, 0x89
    len equ $-numdata
    positive db 0
    negative db 0
    zeros db 0
    sum dw 0, 0 ; 32bits

SECTION .text
    align 16
    DEFAULT REL
    GLOBAL start
start:
    push rbp
    mov rbp, rsp
    lea rax, [numdata] ; INCORRECT ADDRESS
    mov rcx, len
    lea rsi, [positive] ; INCORRECT ADDRESS
.Lloop1
    xor rbx, rbx
    mov bl, [rax]
    add [rsi+3], rbx
    test bl, bl
    jnz .Lnotzero
    inc dword [rsi+2]; inc zeros
    jmp .Lendloop
.Lnotzero:
    jns .Lpos
    inc dword [rsi+1]; inc negative
.Lpos:
    inc dword [rsi]; inc positive
.Lendloop
    inc eax
    loop .Lloop1
    mov rax, 0x2000001
    mov rdi, 0
    syscall
    ret

我的操作系统是Mac OS X 10.11,我通过以下方式组装上面的代码:

nasm -f macho64 -g exp02.asm && ld -o exp02 exp02.o

但是当我检查可执行文件时:

otool -tdV exp02

我得到了

exp02:
(__TEXT,__text) section
start:
0000000000001fb0    pushq    %rbp
0000000000001fb1    movq    %rsp, %rbp
0000000000001fb4    xorl    %eax, %eax
0000000000001fb6    leaq    0x143(%rip), %rax <- NOTE ADDR
0000000000001fbd    movl    $0xd, %ecx
0000000000001fc2    leaq    0x144(%rip), %rsi <- NOTE ADDR
start.Lloop1:
0000000000001fc9    xorq    %rbx, %rbx
0000000000001fcc    movb    (%rax), %bl
0000000000001fce    addq    %rbx, 0x3(%rsi)
0000000000001fd2    testb    %bl, %bl
0000000000001fd4    jne    start.Lnotzero
0000000000001fd6    incl    0x2(%rsi)
0000000000001fd9    jmp    start.Lendloop
start.Lnotzero:
0000000000001fdb    jns    start.Lpos
0000000000001fdd    incl    0x1(%rsi)
0000000000001fe0    jmp    start.Lendloop
start.Lpos:
0000000000001fe2    incl    (%rsi)
start.Lendloop:
0000000000001fe4    incl    %eax
0000000000001fe6    loop    start.Lloop1
0000000000001fe8    movl    $0x2000001, %eax        ## imm = 0x2000001
0000000000001fed    movl    $0x0, %edi
0000000000001ff2    syscall
0000000000001ff4    retq
(__DATA,__data) section
0000000000002000    12 88 82 01 02 03 04 05 06 07 08 09 10 00 00 00 
0000000000002010    00 00 00 00 

当我用gdb调试这个程序时,加载到rax和rsi的地址也是不正确的(对于rax,在这种情况下应该是0x2000但是我得到0x2100)。

我的问题是如何在这种情况下将numdatapositive的正确地址加载到raxrsi中,我是否正确使用LEA?

0 个答案:

没有答案
相关问题