I am using x86 assembly MASM to compute first 41 fibonacci numbers. I initially had an array set to size 41, with 0's. This causes correct output for first 10ish values, with remaining indices being garbage values or blank. If I declare the array as size 100, it gives 20ish correct numbers... If i declare the array to size 300 elements, all 41 numbers are correct. Why is this? I want to declare the array as 41 and get 41 correct results.
INCLUDE Irvine32.inc
.data
display BYTE "Fibonacci number ", 0
equal BYTE " = ", 0
array DWORD 500 DUP(0)
fib DWORD OFFSET array
.code
main PROC
mov fib, OFFSET array
mov [fib], 0
mov [fib + 4], 1
mov [fib + 8], 1
mov ecx, 3
mov esi, 0
L1:
mov edx, OFFSET display
call WriteString
mov eax, esi
call WriteDec
mov edx, OFFSET equal
call WriteString
mov eax, [fib + esi * 4]
call WriteDec
call Crlf
inc esi
loop L1
mov ecx, 38
mov esi, 3
Fibo:
mov edx, OFFSET display
call WriteString
mov eax, esi
call WriteDec
mov edx, OFFSET equal
call WriteString
mov eax, [fib + esi * 4 - 4]
add eax, [fib + esi * 4 - 8]
mov [fib + esi * 4], eax
mov eax, [fib + esi * 4]
call WriteDec
inc esi
call Crlf
loop Fibo
exit
main ENDP
end main
fibcontains the address ofarray, butmov [fib], 0stores infibitself, not inarray; andmov [fib+4], 0stores in whatever memory followsfib. x86 doesn't have a double-indirect addressing mode, in which the memory address to be accessed is itself fetched from some other address in memory. I think every instance offibsimply needs to be changed toarray. The address ofarrayis known at link time and can be used as an immediate; there's no point in storing it in memory and then loading it back later.fib, that is, except inmov fib, OFFSET arraywhich you should simply delete.mov [fib], 0overwrites the pointer with 0.mov [fib + 4], 1stores to memory you haven't allocated, past the end of that pointer. I don't know what you might be stepping on in the.datasection of Irvine32 code you link with, but it might depend on alignment relative to a 16-byte boundary. Anyway, if you just want your code to work and be simpler, usefib DWORD 41 dup(0)instead of the 2 lines you have now. (edit: wrote this comment before Nate's, but the site was temporarily read-only. Basically redundant now)mov esi, OFFSET fib(wherefibis the label attached to41 dup(0)).