Need help w/ my code. I can't seem to save my array of #s to a file. Greatly appreciate any help...

Using masm 6.15


;===============================================
INCLUDE Irvine32.inc

;unsigned int a[65000];
;for(unsigned int counter=0;counter<65000;counter++)
; a[counter]=0;
;
; unsigned int a[65000];
; for (counter=2;counter<10;counter++){
; unsigned int rotation=2,position=0;
; while ((position=rotation*counter)<65000){
; a[position]=1;
; rotation++;
; }
; }
;
;for(counter=0;counter<100;counter++)
; if (a[counter]==0) cout<<endl<<counter<<" : "<<a[counter];
;
;return 0;
;
;
;
;
; al -> counter
; bl -> rotation
; ax receives al * bl


.data
buffer word 0ffffh dup (0); ;array of 65535 elements initialized with zero

filename db "PRIMES.TXT", 0 ;filename
fileHandle dd ? ;handle to output file
bytesWritten dd ? ;number of bytes written

errMsg db "Error Creating File!", 0dh, 0ah,0

bufSize = ($ - buffer) ;buffer size

.code
main PROC

lea esi, buffer ;Load Effective Address buffer

mov al, 2 ;counter, should go from 2 to 9
mov dx, 1



.REPEAT

mov bl, 2
mul bl

.WHILE (ax < lengthof buffer)
push eax
mov ebx,esi
add eax,ebx
mov esi,eax
add [esi], dx
pop eax
pop ebx
inc bl
mul bl
.ENDW

inc al

.UNTIL(al >= 10)


create_file:
INVOKE CreateFile, ;create file
ADDR filename, ;ptr to filename
GENERIC_WRITE, ;access mode
DO_NOT_SHARE, ;share mode
NULL, ;share mode
CREATE_ALWAYS, ;creation disposition
FILE_ATTRIBUTE_NORMAL, ;flags and attribute
0 ;handle to template file

mov fileHandle, eax

.IF eax == INVALID_HANDLE_VALUE ;if error occurs
lea edx, errMsg ;Load Effective Address errMsg
call WriteString ;write to console
jmp QuitNow ;Exit program
.ENDIF ;end if


write:
INVOKE WriteFile, ;write text to file
fileHandle, ;file handle
ADDR buffer, ;buffer pointer
bufSize, ;number of bytes to write
ADDR bytesWritten, ;number of bytes written
0 ;overlapped execution flag


finish_write:
INVOKE CloseHandle, fileHandle ;close file




QuitNow:
INVOKE ExitProcess, 0

main ENDP
END main
Posted on 2003-03-30 23:02:33 by locke
I'm not sure about your macros so I would check the value of bufSize to make sure that you are actually requesting to write the number of bytes that you want to. The output will be in DWORDs so it will not be readable in text mode, if that's what you are trying to do. The CreateFile looks fine as does the WriteFile so I would look at your buffer data and bufSize contents.

PS the Irvine32.inc kind of gives away the homework thing :)
Posted on 2003-03-30 23:15:05 by donkey
Well, thanx for your input. How does my algorithm look?

I wrote it in C++ first and compiled it and it worked fine. I had some trouble trying writing it to asm...
I'm wondering maybe if that is the problem.


I'm just trying to fill the array w/ 0s and 1s. All the 0s being the prime #s and the 1s being the non-prime #s.
Than I wanna somehow display this info, either by saving it to a file, or outputing the whole gigantic thing to the screen...
Posted on 2003-03-30 23:25:14 by locke

Well, thanx for your input. How does my algorithm look?


At nearly 1 am every routine looks fine :grin: My brain isn't running at 100% right now but it looks ok. You're obviously in California so it's not as late for you.
Posted on 2003-03-30 23:30:47 by donkey
hehe... :)

It's almost 1PM here as well... otherwise I woudn't be in such a rush to get this done.

I'm just clueless as to why this isn't saving the array into a file.
Posted on 2003-03-30 23:33:46 by locke
How big is the output file when you're done? I would seriously check the bufSize parameter but then I have a serious mistrust of macros especially when they're in somebody else's library. If it's value is zero it would explain the no output problem. Also like I said, check the data in your array to make sure that there is acutal data to write. Bed time soon so I probably wiil give up now...
Posted on 2003-03-30 23:38:42 by donkey
I can't give up! :)

the file size is indeed 0(zero). so what does that mean???
Posted on 2003-03-30 23:43:47 by locke
bufSize is probably zero so you are either not generating any data or have not properly incremented bufSize
Posted on 2003-03-30 23:45:43 by donkey
Maybe you could try calling SetFilePointer to FILE_BEGIN before attempting to write to it.
Posted on 2003-03-30 23:47:44 by Mecurius
what would you recommend???

i'm a novice in asm... I was having a hard time as it is trying to rewrite what I had in c++ to asm. :)
Posted on 2003-03-30 23:49:54 by locke
set bufSize to a fixed number say 32768 to guarantee output then check the data in the file with a hex editor if it's all 0's you'll have to rethink your algorithm
Posted on 2003-03-30 23:52:23 by donkey
INVOKE setFilePointer,
fileHandle,
0,
0,
FILE_BEGIN

I inputed that... still nothing
Posted on 2003-03-30 23:55:58 by locke

INVOKE setFilePointer,
fileHandle,
0,
0,
FILE_BEGIN

I inputed that... still nothing


When you create/open a file the pointer is automatically set to byte 0 so it would not effect the output. Try the bufSize thing, it will allow you to verify the data.
Posted on 2003-03-30 23:57:47 by donkey
well... the algorithm worked fine in C++... got all the primes saved to a file

I'm wondering if I converted it to asm correctly


and if so...


I wanna save it to a file
Posted on 2003-03-30 23:59:49 by locke
use this to verify :

INVOKE WriteFile, ;write text to file
fileHandle, ;file handle
ADDR buffer, ;buffer pointer
32768, ;number of bytes to write
ADDR bytesWritten, ;number of bytes written
0 ;overlapped execution flag
Posted on 2003-03-31 00:01:32 by donkey
I tried the bufSize = 32768 ...

still the file size is 0(zero) and file is blank
Posted on 2003-03-31 00:01:41 by locke
oooh pleeeease.... I need to get this done. What am I doing wrong????

:confused:
Posted on 2003-03-31 00:06:45 by locke
locke,

There is obviously a problem writing the buffer to the file. You have properly checked the handle to the file that is returned from CreateFile. It is creating a file and do not recieve the error message therefore that part is working properly. The only thing that is left is the WriteFile command, try to replace the bufSize variable completely with a numeric value :

INVOKE WriteFile, ;write text to file
fileHandle, ;file handle
ADDR buffer, ;buffer pointer
32768, ;number of bytes to write
ADDR bytesWritten, ;number of bytes written
0 ;overlapped execution flag

Check the value of bytesWritten on exiting the WriteFile Command.
Posted on 2003-03-31 00:11:27 by donkey
thats weird...

I move the bytesWritten into a register and call DumpRegs

and it doesn't display the registers


directly inputing a number in place of bufSize doesn't seem to help either
Posted on 2003-03-31 00:18:45 by locke


;===============================================
INCLUDE Irvine32.inc

;unsigned int a[65000];
;for(unsigned int counter=0;counter<65000;counter++)
; a[counter]=0;
;
; unsigned int a[65000];
; for (counter=2;counter<10;counter++){
; unsigned int rotation=2,position=0;
; while ((position=rotation*counter)<65000){
; a[position]=1;
; rotation++;
; }
; }
;
;for(counter=0;counter<100;counter++)
; if (a[counter]==0) cout<<endl<<counter<<" : "<<a[counter];
;
;return 0;
;
;
;
;
; al -> counter
; bl -> rotation
; ax receives al * bl


Comments: use ecx as counters. Using 16bit reg means 1 extra clock and an extra byte for the prefix 66h


.data?
buffer db 0ffffh dup (?)
.code
lea esi,buffer
mov ecx,2
mov eax,2
@@:
push eax
mul ecx
mov byte ptr [esi+eax],1
pop eax
inc eax
cmp eax, 65000
jng @B
inc ecx
cmp ecx,10
jnz @B

Just my wild guess. Hope it works.
Posted on 2003-03-31 00:29:12 by roticv