dragonfly February 10, 2008 Share dragonfly Member February 10, 2008 (edited) 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 February 10, 2008 by DarkArchon Link to comment Share on other sites More sharing options...
mookie February 10, 2008 Share mookie GC Alumni February 10, 2008 Call me old fashioned (or lazy), but it's easier to debug ASM code if you know what it's supposed to be doing. Link to comment Share on other sites More sharing options...
mookie February 10, 2008 Share mookie GC Alumni February 10, 2008 What is WriteHex, and are you expecting it to do something other than just dump the double word to a file? You may want to look at your output file with a hex editor. Link to comment Share on other sites More sharing options...
dragonfly February 10, 2008 Author Share dragonfly Member February 10, 2008 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! Link to comment Share on other sites More sharing options...
mookie February 10, 2008 Share mookie GC Alumni February 10, 2008 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 More sharing options...
NOFX February 11, 2008 Share NOFX Member February 11, 2008 (edited) 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 February 11, 2008 by NOFX Link to comment Share on other sites More sharing options...
dragonfly February 11, 2008 Author Share dragonfly Member February 11, 2008 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 More sharing options...
NOFX February 11, 2008 Share NOFX Member February 11, 2008 yep, sorry can't help you man, last ASM code I wrote was for Operating system class about 3-4 years ago. Link to comment Share on other sites More sharing options...
mookie February 11, 2008 Share mookie GC Alumni February 11, 2008 It's not that hard, you just add 48 (30h) to a number 0-9 to get the ASCII character. Link to comment Share on other sites More sharing options...
dragonfly February 11, 2008 Author Share dragonfly Member February 11, 2008 I understand that... I guess I'm having more trouble understanding how the output file is created and manipulated. The text doesnt really give us any examples, and the prof... same guy that gave us the sequence on our lab test. Link to comment Share on other sites More sharing options...
dragonfly February 13, 2008 Author Share dragonfly Member February 13, 2008 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now