hi all....

i m just a beginner in using assembly but i have this big problem in which i hope u ll help....

i want to make a prog that opens a certain text file read it and then count the character in it and display them on screen....i dont know how to make the prog read the file till the end and count the chars....

can u plz help me??
Posted on 2005-06-12 18:49:37 by netghost23
I'd say count them one after another. :)
Posted on 2005-06-12 19:33:13 by JimmyClif
For large files, you might want to read it into a buffer first, in that case you will already have the number of characters read in the lpNumberOfBytesRead buffer. If you want to count them and you expect more than 512 characters you might think about using MMX (SSE) to do the counting:

CountChars FRAME pBuffer

mov eax,
nop ; fill in stack frame+mov to 8 bytes

pxor mm0,mm0
nop ; fill pxor to 4 bytes
pxor mm1,mm1
nop ; fill pxor to 4 bytes

: ; this is aligned to 16 bytes
movq mm0,
pcmpeqb mm0,mm1
add eax,8
pmovmskb ecx,mm0
or ecx,ecx
jz <

sub eax,

bsf ecx,ecx
sub eax,8
add eax,ecx




This one will count the number of lines (terminated by 0Ah). It has the advantage that for huge files it uses file mapping so you don't need a massive memory buffer to hold it. It is however a great deal slower because of this.

CountFileLines FRAME pszFile
uses ebx,edi,esi
LOCAL hMapFile :D
LOCAL pFilebuffer :D
LOCAL cbHigh :D
LOCAL cbFile :D
LOCAL hFile :D

xor ebx,ebx
dec ebx

invoke CreateFileA,,GENERIC_READ,0,0,OPEN_EXISTING,0,0
mov ,eax
or eax,eax
js >>E1

invoke GetFileSize,,offset cbHigh
mov esi,eax

invoke CreateFileMappingA,,0,PAGE_READONLY,0,0,0
mov ,eax
or eax,eax
jz >E2

invoke MapViewOfFile,,FILE_MAP_READ,0,0,0
mov ,eax
or eax,eax
jz >E3
; This routine just counts the lines in a file
; in reality it just counts character 0Ah

xor ebx,ebx
inc ebx ; there is always one line, this is a one based count
mov edi,
lea esi,

mov eax,
add edi,4
cmp edi,esi
ja >L2

xor eax,0a0a0a0ah
mov edx,eax
lea ecx,
not edx
and ecx,edx
and ecx,80808080h
jz <L1

test ecx,00000080h
jz >
inc ebx
test ecx,00008000h
jz >
inc ebx
test ecx,00800000h
jz >
inc ebx
test ecx,80000000h
jz <L1
inc ebx
jmp <L1


invoke UnmapViewOfFile,

invoke CloseHandle,

invoke CloseHandle,

mov eax,ebx
Posted on 2005-06-12 20:33:04 by donkey
for windows use the OpenFIle and GetFileSize API's
Posted on 2005-09-19 16:33:02 by Warsocket

OpenFile is obsolete and GetFileSize while it does give you the size of the file - it does not count the chars of a file. GetFileSize returns the size of the file and that includes spaces, chars, tabs, CRLF's, points, commas, exclamation signs (etc, etc) and zero terminator(s).
Posted on 2005-09-19 20:27:26 by JimmyClif
(...)GetFileSize returns the size of the file and that includes spaces, chars, tabs, CRLF's, points, commas, exclamation signs (etc, etc) and zero terminator(s).

...and UNICODE stuff if the text is UTF ;)

and also you should specify if your app is supposed to support the "full" unicode. if it is, then the easiest and fastet way to write it is to make a RichEdit control (you don't have to display it) and use it to tell you the number of chars. RichEdit controls support ANSIs and UNICODEs. Of course this approach can be hell-slow in terms of execution time. I wouldn't use it for anything larger than few KBs.
Posted on 2005-09-24 18:43:43 by ti_mo_n