I am trying to print out every fibonacci number in assembly with a printf implementation. But I keep getting a segmentation fault everytime the Instruction Pointer reaches call printf
. I have already used printf in a few other files but for some reason it throws an error in this one. I have already debugged the file with GDB and it throws the error when call printf
is reached.
I have also tried removing anything that pushes anything onto the stack, but alas this also did not work.
Execution:
nasm -felf64 fib.asm && gcc -no-pie fib.o && ~/a.out
Architecture: Linux x86_64
Code:
global main
extern printf
section .data
formatStr:
dd "%i",0xA,0
section .text
main:
push rbx ; Push default RBX
push rbp ; Push Base Pointer
mov rbp,rsp
sub rsp,32 ; Reverse 32 Bytes for local variables
mov rax,0 ; A
mov rbx,1 ; B
mov rcx,1 ; Counter
fib:
mov rdx,rbx ; Temp = A
add rbx,rax ; A += B
mov rax,rdx ; B = Temp
mov QWORD[rbp-8],rax
mov QWORD[rbp-16],rbx
mov QWORD[rbp-24],rcx
mov rdi,formatStr
mov rsi,rbx
xor rax,rax
call printf
mov rax,QWORD[rbp-8]
mov rbx,QWORD[rbp-16]
mov rcx,QWORD[rbp-24]
dec rcx
jnz fib
; Exit Program
mov rsp,rbp ; Return to Base Pointer
pop rbp ; Get Ret Address
pop rbx ; Get Default RBX
ret
Debugger Image: