简体   繁体   English

NASM:分段错误(核心已转储)

[英]NASM: segmentation fault(core dumped)

I'm new to assembly and I've been trying to learn through random problems. 我是组装的新手,我一直在尝试学习随机问题。 Im using nasm 64 bit on a linux machine. 我在Linux机器上使用nasm 64位。 I've been trying to generate the fibonacci sequence. 我一直在尝试生成斐波那契数列。 However I get a segmentation fault(core dumped) error when I run my executable. 但是,当我运行可执行文件时,出现段错误(核心转储)错误。

section .data

    input db 2

section .bss

    fib resb 128

section .text

_start:

    mov rax, 1
    mov rcx, 1
    mov rdx, fib
    mov rdx, 1
    inc rdx
    mov rbx, 0

    call _fibLoop
    call _fibPrint

    mov rax, 60
    mov rdi, 0
    syscall

_fibLoop:

    mov [rdx], rax
    inc rdx
    add rcx, rax
    push rcx
    mov rcx, [rax]
    pop rax
    inc rbx
    cmp rbx, [input]
    jne _fibLoop
    ret

I know the call to _fibPrint is not the issue cause it does virtually nothing. 我知道_fibPrint的调用不是问题,因为它实际上什么也不做。 I assume the way I am writing to my reserved memory is flawed. 我认为写入保留内存的方式有缺陷。 However Ive been able to do this similarly in the past so I'm lost on whats wrong. 但是,我过去也可以类似地执行此操作,因此我错失了什么。

As part of your initialization, you have this sequence: 作为初始化的一部分,您具有以下顺序:

mov rdx, fib
mov rdx, 1
inc rdx

This will leave rdx with the value 2 , and not the offset of a buffer to hold your numbers. 这将使rdx的值为2 ,而不是保留数字的缓冲区的偏移量。 Then, at the start of _fibLoop , you write to it with 然后,在_fibLoop ,使用写入

mov [rdx], rax

This will try to access memory that you cannot access, resulting in the segmentation fault. 这将尝试访问您无法访问的内存,从而导致分段错误。

I think removing the extra two lines after mov rdx,fib will fix that crash, letting you move on to debugging the other bugs using GDB or whatever other debugger you like. 我认为在mov rdx,fib之后删除多余的两行将解决该崩溃问题,使您可以继续使用GDB或所需的其他调试器调试其他错误。

(Like that inc rdx in _fibLoop only advances the pointer by 1 byte, but you're doing 8 byte stores. And that input is also only 1 byte, but you're doing an 8-byte load there, too.) (就像_fibLoop中的inc rdx仅将指针_fibLoop 1个字节,但是您要进行8个字节的存储。该input也只有1个字节,但是您也要在那里进行8字节的加载。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM