I am using SASM.
The x86-asm program may save the first to n-th Fibonacci-Number into an array (eg. n:5 -> [1,2,3,5,8]).
The actual algorithm works, but it doesn't save the output from the EAX
register into the array, I keep getting an Segfault error:
%include "io.inc"
section.data
size: DD 5
array: DD 0,0,0,0,0
section .text
global CMAIN
CMAIN:
mov ebp, esp; for correct debugging
MOV eax, 1 ;i
MOV ebx, 1 ;j
MOV edx, [size]
MOV ecx, 0 ; index
DEC edx ; max. Index
fib:
CMP ecx, edx
JE end
MOV edx, ebx ; safe j
MOV ebx, eax ; j <- i
ADD eax, edx ; i <- j+i (with j from edx)
MOV [array+ecx*4],eax ; Program received signal SIGSEGV, Segmentation fault.
INC ecx
JMP fib
end:
ret
When implemented without an array, just with the results in EAX
, it works:
%include "io.inc"
section .text
global CMAIN
CMAIN:
mov ebp, esp; for correct debugging
MOV eax,1 ; i
MOV ebx,1 ; j
MOV ecx,0 ; n: index
fib:
CMP ecx, 4
JE end
MOV edx, ebx ; safe j
MOV ebx, eax ; j <- i
ADD eax, edx ; i <- j+i (with j from edx)
INC ecx
JMP fib
end:
ret