flat assembler
Message board for the users of flat assembler.

Index > DOS > count characters in a text file

Author
Thread Post new topic Reply to topic
netghost



Joined: 12 Jun 2005
Posts: 3
netghost 12 Jun 2005, 23:56
can somebody tell me how to make a prog that counts the character in a text file and show them on screen???i use masm611 for 8086.i have managed to open the file,read but not count the chars ....
Post 12 Jun 2005, 23:56
View user's profile Send private message Reply with quote
netghost



Joined: 12 Jun 2005
Posts: 3
netghost 13 Jun 2005, 00:01
i m using this code but cannot understand this line "mov Buffer2,ax
add Buffer2,46 " can someone help plz???

Code:
DATA segment "data" 
File1 db 'one.txt',0 
Buffer db 256 DUP(?) 
Buffer2 dw 29 DUP(?),'$' 

DATA ends 


.model small 
.stack 1024 

CODE segment "code" 
Assume cs:CODE, ds:DATA 
ep: 
mov AH,3Dh 
mov AL,0 
lea dx,File1 
int 21h 

mov BX,AX 

mov AH,3Fh 
mov CX,200 
lea dx,Buffer 
int 21h 


mov Buffer2,ax 
add Buffer2,46 


mov ah,09h 
mov dx,offset Buffer2 
int 21h 

mov ah,3eh 
int 21h 

mov ax,4c00h 
int 21h 


CODE ends 
end ep    
Post 13 Jun 2005, 00:01
View user's profile Send private message Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 7997
Location: 22° 15' N | 114° 10' E
YONG 13 Jun 2005, 12:30
Your program needs to do two things:
- File I/O & Processing (i.e., counting the number of bytes read), and
- Integer Outputting (i.e., printing the count).


For the first thing, I suggest you read Section 13.3 "An Introduction to MS-DOS"
of "The Art of Assembly Language", in particular Section 13.3.10 "Blocked File I/O"
which gives some simple examples of how to do file i/o & processing. The links are:

http://webster.cs.ucr.edu/AoA/DOS/AoADosIndex.html
http://webster.cs.ucr.edu/AoA/DOS/ch13/CH13-8.html#HEADING8-1


For the second, check this link:

http://board.flatassembler.net/topic.php?t=42
- "i wrote a Sample code for Outputting integers" by Tommy


YONG
Post 13 Jun 2005, 12:30
View user's profile Send private message Visit poster's website Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 13 Jun 2005, 12:39
netghost wrote:
i m using this code but cannot understand this line "mov Buffer2,ax
add Buffer2,46 " can someone help plz???

DATA segment "data"
File1 db 'one.txt',0
Buffer db 256 DUP(?)


DATA ends


.model small
.stack 1024

CODE segment "code"
Assume cs:CODE, ds:DATA
ep:
mov AH,3Dh
mov AL,0
lea dx,File1
int 21h

mov BX,AX

mov AH,3Fh
mov CX,200
lea dx,Buffer
int 21h


mov ah,3eh
int 21h

mov ax,4c00h
int 21h


CODE ends
end ep



I don't see how that code could work properly since the return value in AX after the read is the count in bytes read. Plus how is this code going to take that value and convert it to a ASCII format to print on the screen?Another problem with the program is the '$' terminating character. What if there was another character of '$' in your text file? It wouldn't read after that point if you were printing out the characters read. Best thing to do is to use a null terminated buffer and write a routine to print the contents of that buffer. Also, the carrry flag should be checked after each file operation for failure.

Code:
DATA segment "data" 
File1 db 'one.txt',0 
Buffer db 256 DUP(0),0 ;zero terminated


DATA ends 


.model small 
.stack 1024 

CODE segment "code" 
Assume cs:CODE, ds:DATA 
ep: 
mov AH,3Dh 
mov AL,0 
lea dx,File1 
int 21h 

jc ERROR
mov BX,AX 

mov AH,3Fh 
mov CX,256 
lea dx,Buffer 
int 21h 

jc ERROR

mov si, Buffer
cld
mov ah, 2

PrintString:
cmp [si], 0
je Done
lodsb
mov dl, al
int 21h
jmp PrintString

Done:
mov ah,3eh 
int 21h 
jc ERROR

mov ax,4c00h 
int 21h 

ERROR:
put error routine here...

CODE ends 
end ep
    


I don't have MASM so I haven't tested the code. Last thing, this is a FASM board not MASM.
Post 13 Jun 2005, 12:39
View user's profile Send private message Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 7997
Location: 22° 15' N | 114° 10' E
YONG 13 Jun 2005, 13:20
One more point.

If you're simply interested in the file size in bytes (rather than processing of
the file content), you can use ah=4eh_int21h to do the job. Take a look at
these two sections of AoA:

- 13.3.8.7 "Set Disk Transfer Address (DTA)"
- 13.3.8.8 "Find First File"

YONG
Post 13 Jun 2005, 13:20
View user's profile Send private message Visit poster's website Reply with quote
netghost



Joined: 12 Jun 2005
Posts: 3
netghost 14 Jun 2005, 17:47
thanks a lot guys....but still i havent figured it out....and the most worrying is that i have to give this code till tomorrow at night!!!!!!
it should be simple and maybe i should use better the file pointer using the AH=42H....

i dont know...if someone has a solid answer i would be grateful and ready to buy him a coffee in athens,greece!!!!!!


p.s. i have search the net like crazy ..couldnt find something....
Post 14 Jun 2005, 17:47
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 15 Jun 2005, 12:09
netghost wrote:
thanks a lot guys....but still i havent figured it out....and the most worrying is that i have to give this code till tomorrow at night!!!!!!
it should be simple and maybe i should use better the file pointer using the AH=42H....

i dont know...if someone has a solid answer i would be grateful and ready to buy him a coffee in athens,greece!!!!!!


p.s. i have search the net like crazy ..couldnt find something....



We just gave you pretty much the answer. If you want to display the count, then just write a routine that converts an integer to ASCII format and then print that value to the screen.
Post 15 Jun 2005, 12:09
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 19 Jun 2005, 06:20
Hy,
well if you look around in dos thread you will find yourself complete snipplets presenting "integer to ASCII format and then print that value to the screen."

if it does not matter who wrote it.

netghost wrote:
thanks a lot guys....but still i havent figured it out....and the most worrying is that i have to give this code till tomorrow at night!!!!!!
it should be simple and maybe i should use better the file pointer using the AH=42H....

i dont know...if someone has a solid answer i would be grateful and ready to buy him a coffee in athens,greece!!!!!!

hmm it was probably a homework,
and its over by now,
but was it your question to read the whole file and count bytes in a variable:that can be done with the return value "AX" of read file function and then print it on screen,

or get filesize of textfile with dos function and print it onto screen?

the second is easier...
Post 19 Jun 2005, 06:20
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.