Jump to content

Programming (AL)


dragonfly

Recommended Posts

Ok, so I'm having trouble writing to a file, as well as getting my fib numbers to store correctly into an array.

 

INCLUDE Irvine32.inc

.data
INVALID_HANDLE_VALUE = 0
BUFFER_SIZE = 188	;amount of bytes to write to the file (47x4)
.data
num1		DWORD 00000001h;num1 always holds lowest value
num2		DWORD 00000001h;num2 always holds new value
fibArray	DWORD 47 DUP(?);used to store fib numbers in array
temp		DWORD ?

fileHandle DWORD ?
filename BYTE "Output.txt", 0

.code
main PROC

mov ecx, lengthof fibArray - 2;moves number into ecx, (loop count, -2 because already given fib 1 and 2)
mov esi, num1
mov fibArray[0], esi
mov esi, num2
mov fibArray[1], esi
mov esi, 2			;esi to point to array pos 2 (3rd spot) for first fib number

call GetFib			;puts the 47 fib numbers into an array called fibArray

mov ecx, lengthof fibArray - 1
mov esi, 0

L2:	mov eax, fibArray[esi]
call WriteHex
call crlf
inc esi
loop L2

COMMENT &


;cmp eax, INVALID_HANDLE_VALUE
;je  file_error
mov eax,12
mov fileHandle, eax
&

mov edx, OFFSET filename
call CreateOutputFile
mov edx, OFFSET fibArray
mov ecx, BUFFER_SIZE
call WriteToFile

call WaitMsg
exit
main ENDP

;-----------------------------------------------------
GetFib PROC USES eax ebx esi
;
;Procedure gets the first 47 fib numbers and
;stores them in an array called fibArray, including
;the first two numbers, 1 and 1.
;-----------------------------------------------------
L1:	mov eax, 0
add eax, num1	;add num1 and num2 to eax for next number
add eax, num2
mov ebx, num2	;mov num2 into num1
mov num1, ebx	;mov num2 into num1
mov num2, eax	;mov eax to num2
			;num1 is now second last value, num2 is latest value
mov fibArray[esi], eax;puts next fib number into fibArray

inc esi		;increment array index

;call WriteDec	;displays fib number from the eax register
;call Crlf		;next line
loop L1
ret
GetFib ENDP
;-----------------------------------------------------

END main

 

Any help'd be appreciated, or even a point to an information source.

 

Edit: output in file:

"7YÂéybÛ=UmÂ/ñ 1Bsµ(ÃâçÉ°y)¢Ëm8¥Â_á$±

Edited by DarkArchon
Link to comment
Share on other sites

Woops, sorry!

 

Irvine32.inc has libraries, including WriteHex, which writes the hexadecimal digit to the screen of the eax register. Taht line is just for the loop to display the hex digits in my fibArray.

 

The program is supposed to calculate the first 47 digits of the fib sequence, store the values into an array (fibArray) and output the array contents to a file (Output.txt).

 

I wouldnt mind if I could write even one line from the array to the output file, cause that'd be a start. Right now my array is getting garbled (the higher-value digits) and I'm not sure why. I'm generally a terrible programmer (just look above) and I dunno why it's not working. I forgot to include a description, sorry! :bang:

Link to comment
Share on other sites

I am guessing that you're just dumping the numbers to the file. If you look at your file with a hex editor it will probably look something like this:

0000 0001 0000 0001 0000 0002 0000 0003 0000 0005 0000 0008 0000 000D 0000 0015 0000 0022 0000 0037 0000 0059

 

For each of your numbers if you want to make ASCII you have to do it by hand. Yes that means divide by 10 over and over.

Link to comment
Share on other sites

Member

fibArray[47];

fibArray[0]=0;

fibArray[1]=1;

 

for(x=2, x < 48, x++) {

fibArray[x] = fibArray[x-2]+fibArray[x-1];

cat << fibArray[x] << output.txt;

}

 

Bet you can't wait to start using a high level language :)

Edited by NOFX
Link to comment
Share on other sites

fibArray[47];

fibArray[0]=0;

fibArray[1]=1;

 

for(x=2, x < 48, x++) {

fibArray[x] = fibArray[x-2]+fibArray[x-1];

cat << fibArray[x] << output.txt;

}

 

Bet you can't wait to start using a high level language :)

 

That's what I've been using the last several years. It's SO much easier!!! <_<

Link to comment
Share on other sites

Well, I got everything to work except for printing to the file correctly... and I give up :) Maybe it's just not viewing it right like I've been told. Here's the solution incase anyone's interested!

 

; This program calculates the first 47 fib numbers, then writes them to a file called "Output.txt".
; Created: 02/10/2008

INCLUDE Irvine32.inc

.data
BUFFER_SIZE = 188		;amount of bytes to write to the file (47x4)
.data
num1		DWORD 00000001h;num1 always holds lowest value
num2		DWORD 00000001h;num2 always holds new value
fibArray	DWORD 47 DUP(0);used to store fib numbers in array, preset to 0
testArray BYTE  "testing 1 2 3...", 0


fileHandle DWORD ?
filename BYTE "Output.txt", 0

.code
main PROC

mov ecx, lengthof fibArray - 2;moves number into ecx, (loop count, -2 because already given fib 1 and 2)
mov esi, num1
mov fibArray[0], esi		;first fib number moved into the fibArray
mov esi, num2
mov fibArray[TYPE fibArray], esi
mov esi, 2				;esi to point to array pos 2 (3rd spot) for first fib number

call GetFib				;puts the 47 fib numbers into an array called fibArray

COMMENT |below is setup for the loop to display contents of fibArray|
mov ecx, lengthof fibArray	;need the loop to run 47 times
mov esi, 0				;esi is the indexing key

L2:	mov eax, esi
call WriteDec				;display which fib number we're on
mov al, '-'				;setup: [ARRAY INDEX]-[FIB VALUE]
call WriteChar				;writes "-" after index value
mov eax, fibArray[TYPE fibArray*esi]
call WriteHex				;writes the fib number at the current index
call crlf
inc esi					;go to the next index
loop L2					;restart the loop

mov edx, OFFSET filename		;move "Output.txt" offset into edx
call CreateOutputFile		;create the file called "Output.txt"
mov edx, OFFSET fibArray		;put fibArray offset into edx
mov ecx, BUFFER_SIZE		;number of bytes to be written (188)
call WriteToFile			;write 188 bytes starting at fibArray offset to file "Output.txt"

COMMENT +
-------------------------------------------------------
USED TO TEST MY FILE OUTPUT CODE TO SEE IF IT'S CORRECT
-------------------------------------------------------
mov edx, OFFSET filename
call CreateOutputFile
mov edx, OFFSET testArray
mov ecx, SIZEOF testArray
call WriteToFile
------------------------------------------------
THIS CODE IS CORRECT, SO ABOVE SHOULD BE AS WELL
IT IS JUST BEING DISPLAYED INCORRECTLY IN THE 
TEXT EDITOR I USE TO VIEW THE FILE...
------------------------------------------------
+

call WaitMsg
exit
main ENDP


;-----------------------------------------------------
GetFib PROC
;
;Procedure gets the first 47 fib numbers and
;stores them in an array called fibArray, including
;the first two numbers, 1 and 1.
;-----------------------------------------------------
L1:	mov eax, 0
add eax, num1		;add num1 and num2 to eax for next number
add eax, num2
mov ebx, num2		;mov num2 into num1
mov num1, ebx		;mov num2 into num1
mov num2, eax		;mov eax to num2
				;num1 is now second last value, num2 is latest value
				;puts next fib number into fibArray
mov fibArray[TYPE fibArray * esi], eax	
inc esi			;increment array index

loop L1
ret
GetFib ENDP

END main

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...