【问题标题】:nasm segfault in simple programm简单程序中的 nasm 段错误
【发布时间】:2015-04-19 12:42:53
【问题描述】:

此代码应该输出 1 位的密钥长度,但它输出 segfault。

我不明白我做错了什么,问题一定出在清单的第一部分,因为我直接从书中取了flen函数:

    section .text
    global _start   ;must be declared for linker (ld)
_start:

    mov edi,key
        call flen
        mov ebx,1
        add ecx,38
        mov edx,1
        int 0x80

    flen:           ;length of string on es:edi
    xor eax,eax
    xor ecx,ecx
    dec ecx
    cld
    repne scasb
    neg ecx
    ret


    xor eax,eax
    inc eax
    int 0x80



section .data
key db '123456',0x0
keylen equ $ - key     ;length of our dear string
plaintext times 256 db 1

【问题讨论】:

    标签: assembly segmentation-fault nasm


    【解决方案1】:

    您应该将退出代码移到flen 之前。因为它是您的控制流在系统调用返回后再次落入flen。您还应该将系统调用号放入eax。此外,要打印的值必须在内存中,并且必须传递一个指针。 0 的 ascii 代码是 48 而不是 38,或者你可以直接使用 '0'。像这样的:

        section .text
        global _start   ;must be declared for linker (ld)
    _start:
    
        mov edi,key
            call flen
            add ecx, '0' ; convert to ascii
            push ecx     ; put text onto stack
            mov ecx, esp ; put address into ecx
            mov eax, 4   ; sys_write
            mov ebx,1
            mov edx,1
            int 0x80
    
        xor eax,eax
        inc eax
        int 0x80
    
        flen:           ;length of string on es:edi
        xor eax,eax
        xor ecx,ecx
        dec ecx
        cld
        repne scasb
        neg ecx
        ret
    
    section .data
    key db '123456',0x0
    keylen equ $ - key     ;length of our dear string
    plaintext times 256 db 1
    

    注意 flen 返回8。 PS:学习使用调试器。

    【讨论】:

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