for a class project we're asked to create a Fibonacci sequence program. I am using Visual Studio 2017 to run my program (32 bit). All goes well when I run the below program until I try to print to console the 6th number in the sequence, 5, (0,1,1,2,3,5). Nothing past the 5th number is being outputted to the console. I am using the WriteDec procedure from Irvine32 Library to accomplish to write decimals to the console.
I am bamboozled because eax
register holds the right values when I step through in debugger mode, but WriteDec won't print it out. The address of the array I use to store Fibonacci numbers has these values when computing the 8th term of the sequence :
0x00406000 00 00 00 00 01 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 05 00 00 00 08 00 00 00 0d 00 00 00
The output looks like:
0
1
123
;Fibionacci Sequence
; This program outputs a fibonacci sequence and sum
Include Irvine32.inc
.data
array DWORD 0,1
.code
main proc
mov ecx, 6 ; would be the 8th term in the sequence
mov esi, 4
mov eax, 0
call WriteDec
call crlf
mov eax, 1
call WriteDec
call crlf
L1:
mov edx, array[esi]
mov edi, array[esi-4]
add edx,edi
mov array[esi+4],edx
mov eax, edx
call WriteDec ; Writes an unsigned 32-bit decimal number to standard output in decimal format with no leading zeros.
add esi,4
loop L1
invoke ExitProcess,0
main endp
end main
When you write past the end of the two dwords you reserved for your array DWORD 0,1
, you're probably overwriting some data that WriteDec uses.
how could i test if writedec overwrites data?
You have it backwards. Your code is writing outside your array. The linker probably puts it right before some data that WriteDec
needs to read, e.g. maybe a file handle for the console or something. So after you step on WriteDec's constants, future calls to it stop printing anything.
Your debugger output confirms that WriteDec
didn't overwrite the Fibonacci sequence values you stored, so we can conclude that WriteDec
isn't writing to that memory, just reading it.
And also that it's not a pointer, or it would crashed when overwrote it with small integers.
Try something like
array DWORD 0,1
DWORD 10 DUP(?) ; or DUP(0) because that's what you'll really get
to make a larger array with explicit initializers for the first 2 values, then more space in the same section.