Reputation: 11
I'm new to assembly and I've been trying to learn through random problems. Im using nasm 64 bit on a linux machine. 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. 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.
Upvotes: 0
Views: 1185
Reputation: 32727
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. Then, at the start of _fibLoop
, you write to it with
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.
(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.)
Upvotes: 3