I have a problem getting my assembly program designed to get the first 47 numbers of the fibonacci series to output to a file. I'm not getting any errors except for when opening command window. This is the exact question on my homework problem if this helps :
Using Programming Exercise 6 in Chapter 4 as a starting point, write a program that generates the first 47 values in the Fibonacci series, stores them in an array of doublewords, and writes the doubleword array to a disk file. You need not perform any error checking on the file inputoutput because conditional processing has not been covered yet. Your output file size should be 188 bytes because each doubleword is 4 bytes. Use debug.exe or Visual Studio to open and inspect the file contents, shown here in hexadecimal.

INCLUDE Irvine32.inc

.data
FIB_COUNT = 45 ; number of values to generate after first 2 1's
fileHandle DWORD ?
filename BYTE "a.bin",0
array DWORD FIB_COUNT DUP(?)
temp1 DWORD ?
temp2 DWORD ?
temp3 DWORD ?

.code
main PROC

mov esi,OFFSET array
mov array[esi],1
mov eax,[esi]
mov temp1,eax
add esi,4
mov array[esi],1
mov eax,[esi]
mov temp2,eax
add esi,4

mov ecx, FIB_COUNT
fibgenerator:

mov eax,0
mov ebx,0
mov eax,temp1
mov ebx,temp2
add eax,ebx
mov temp3,eax
mov array[esi],eax
mov temp1,ebx
mov temp2,eax
add esi,4

loop fibgenerator

mov edx,OFFSET filename
call CreateOutputFile

mov eax, fileHandle
mov edx,OFFSET filename
mov ecx,188
call WriteToFile
mov eax,filehandle
call CloseFile
exit
main ENDP
END main

Recommended Answers

All 4 Replies

Er, um, you are not writing what you want to write to the file!

;
; Writes a buffer to an output file.
; Receives: EAX = file handle, EDX = buffer offset,
; ECX = number of bytes to write
; Returns: EAX = number of bytes written to the file.

Line 44 eax, has the file handle from the return from CreateOutputFile BUT you are overwriting it with the contents of fileHandle which is 0. Change line 44 to:

mov    fileHandle, eax

Next, edx is supposed to be a pointer to the buffer to write to the file NOT the filename.

thank you very much man! cant believe i made that mistake on line 44 and i also didnt know that about edx so thats good to know. I made some changes and am able to get a file out of it now but it doesn't show the right answers. Any suggestions?

INCLUDE Irvine32.inc

.data
FIB_COUNT = 45 ; number of values to generate after first 2 1's
buffersize = 188
num1 = 1
num2 = 1
fileHandle DWORD ?
filename BYTE "D:\Users\Jose\Documents\Assembly Assignments\COSC_2425_CJ_Project1\a.bin",0
array DWORD 47 DUP(?)
pointarray DWORD array
temp1 DWORD ?
temp2 DWORD ?
temp3 DWORD ?

.code
main PROC

mov esi,0
mov array[esi],num1
mov eax,array[esi]
mov temp1,eax
add esi,4
mov eax,0
mov array[esi],num2
mov eax,array[esi]
mov temp2,eax
add esi,4
call dumpregs

mov ecx, FIB_COUNT
fibgenerator:

mov eax,0
mov ebx,0
mov eax,temp1
mov ebx,temp2
add eax,ebx
mov temp3,eax
mov array[esi],eax
mov temp1,ebx
mov temp2,eax
add esi,4

loop fibgenerator

mov edx,OFFSET filename
call CreateOutputFile
mov filehandle,eax
mov edx,OFFSET pointarray
mov ecx,buffersize
call WriteToFile
mov eax,filehandle
call CloseFile

exit
main ENDP
END main

Your Fibo algo works fine.

It prints wrong because you are passing the wrong thing to the write file function

mov edx,OFFSET pointarray

this is wrong, this is a pointer to a pointer. Remove offset and it will work. Better yet, cut down on uneeded globals and just do

mov edx,offset array

fix that and it will write the proper file. You cannot open it in notepad and get the correct results, you can open in a hex editor or what the homework says.

Man am I glad you helped me out on how to write properly to the file. I literally spent almost 8 hours on just this hw problem wondering what I had done wrong in the code. I had added that pointer to the array when reading through the book thinking it would help but now i just see it was redundant in my case. Again thank you very much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.