Hi,

I am new in assembly and i nave some problems.
I am working on some project for school i should read text from .txt file. The problem is that i don't now how to get the number of chars contained in .txt file that i need to store in CX register.

;Oren .txt file
	mov ah,3dh
	mov al,00h
	lea dx,NameOfFile ;(for example test.txt)
	int 021h
	
	mov bx, ax
	
;Hire i should store in cx numbers of chars to read from opened file but because it is not same for all .txt files i am reading from i get stacked 

;read from .txt file
        mov cx,??????
	mov ah,3fh
	lea dx,TextArray
	int 021h

I hope you get my point, and sorry for my lame English.

Thanks in advance

Recommended Answers

All 3 Replies

You could get the size of the text file using 21/42, that is,
the LSEEK function.

XOR CX, CX
XOR DX, DX ; CX:DX=+0
MOV BX, filehandle
MOV AL, 2 ; Move from end of file
MOV AH, 42h
INT 21h
; a.e. DX:AX contains length of file

To have a 32-bit counter use:

sub word [lowword], 1
sbb word [highword], 0
; I think this is the right way to do it

Or if your TXT file is at most 65,535 bytes, do this:

mov cx, 0ffffh ; read in 64KB, will stop at end of file
mov dx, buffer
mov bx, filehandle
mov ah, 3fh
int 21h

Hi i obviously didn't get point of earlier posted code :(
this is what i ned to do:

I want to write for example "Hello World", from keyboard.
and press ENTER key for end.
Then i will write this chars into .txt file and at the end i will read thous shars from same .txt file and print them on scrin.

.386
.model small, c

;----------------------------------------------------------------
; Stak segment

stack_seg SEGMENT stack

	DB 100 DUP(?)

stack_seg ENDS

;----------------------------------------------------------------
; Data segment

data_seg SEGMENT USE16 'DATA'
 	
	Msg1 db 0ah,0dh,0ah,0dh,"*******************Write Some Text*******************$"
	reader db 1024 dup(?)
	datText db 1024 dup(?)
        filename	db "dat.txt", 0h

data_seg ENDS

;----------------------------------------------------------------
; CODE segment

code_seg SEGMENT USE16 'CODE'
ASSUME cs:code_seg, ds:data_seg
	
start:	

	mov ax, data_seg
	mov ds, ax	
	mov ax,0
	mov cx,0
;----------------------------------------------------------------

main PROC
;Write on scrin TITLE
	lea dx,Msg1
	mov ah,09h
	int 021h
	
;New Line
	mov ah,02h
	mov dl,0ah
	int 021h
	mov ah,02h
	mov dl,0dh
	int 021h


         lea si,reader
         mov cx,0
Here:
        	
	mov ah,08h
	int 021h
        cmp al,0dh
	je exit; if user press ENTER key exit 
	mov [si],al
	inc si
	inc cx
	jmp Here
exit:
        mov di,cx; counter

        mov ah, 03ch				
	mov cx, 0	
	lea dx, filename
	int 21h

	mov ah, 03Dh
	mov al, 01h ; 
	lea dx, filename
	int 21h

	mov bx, ax	; file-handle
	
	mov cx,di
	; WRITE in .txt file
	mov ah, 040h	
	lea dx, reader
	int 21h			
					
	; Close .txt file
	mov ah, 03eh
	int 21h
	

;Open .txt file
	mov ah,3dh
	mov al,02h
	lea dx,filename
	int 021h
         
MOV CX,??????

         mov ah,3fh
	lea dx,datText 
	int 021h

	lea si,datText
         
        ;Here i ned to now count of chars readed from .txt file and mov that number in CX reg (in this case 11 from "Hello world") for example
 	
print:
         cmp CX,0
         je endX
	 
	 mov ah,02h
	 mov dl,[si]
	 int 021h
	
         inc si
	 dec CX
         
         jmp print

EndX:	
;Close .txt file
	mov ah, 03eh
	int 21h
	


mov ax, 04c00h
int 021h
		
;*************************************************************************************		
main ENDP
code_seg ENDS

END start

Thanks in advance

Function 21/3F returns with zero in AX when end-of-file is reached.
If you read in one byte at a time from the file, you can count the
number of bytes you've read in.
Here's an example:

mov cx, 1
mov dx, offset byte_buf
mov bx, [file_handle]
mov ah, 3fh
int 21h
cmp ax, 0
jz done_reading
inc word [len]

Or if you place 65,535 in CX and the file is less in size or equal AX will return
with the number of bytes actually read by 21/3F

mov dx, offset buf
mov bx, [file_handle]
mov cx, 0ffffh
mov ah, 3fh
int 21h
; AX will contain (a.e.) the number of bytes actually read

Also note that 21/3C Create File With Handle returns a file-handle in AX,
and that if the file already exists 21/3C will truncate the file to
zero bytes. Both 21/3D and 21/3C return with CF set on error.

mov ah, 3ch
mov cx, 0 ; attributes
mov dx, offset asciz_name
int 21h
jc err_handler ; CF set on error
mov [file_handle], ax
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.