Attilah
Attilah

Reputation: 17932

Fibonacci not working in GAS assembly language

after executing this code, I get an infinite loop just printing zeros :

( I have examined it closely but I still can't find out what's wrong with it )

.section .data
N:
 .int 35

output:
 .asciz "%f"

.section .text
.globl _start

_start:
 nop
 pushl $5
 call fibonacci

.type fibonacci, @function
fibonacci:
 pushl %ebp
 movl %esp, %ebp
 sub $8, %esp
 pushl $0
 pushl $1

.L2:
 cmpl $2, 8(%ebp)
 jg .L5
 jmp .L3

.L5:
 movl -4(%ebp), %ecx
 addl -8(%ebp), %ecx

 pushl %ecx
 pushl $output
 call printf
 movl -8(%ebp), %edx
 movl %edx,-4(%ebp)
 movl %ecx,-8(%ebp)
 jmp .L2

.L3:
 pushl $0
 call exit

Upvotes: 0

Views: 427

Answers (1)

ninjalj
ninjalj

Reputation: 43748

sub $8, %esp
pushl $0
pushl $1

Why the sub? push already decrements the stack pointer, so after that your local variables would be at -12(%ebp) and -16(%ebp), not at -4(%ebp) and -8(%ebp).

.L2:
 cmpl $2, 8(%ebp)
 jg .L5
 jmp .L3

You never decrement or do anything other than comparing against 2 your argument, so the function runs forever.

Upvotes: 1

Related Questions