Click to See Complete Forum and Search --> : Help read a text file


smokymo
December 3rd, 2008, 09:59 AM
I need to read a 16 characters(one per line) and store them in the array, than wait for a user to press "A" and read another 16 characters.(this is not my code and I am tring to modify it)

.model small
.stack
.data
Handle DW ? ;to store file handle
FileName DB "f:\ca225\text.txt",0 ;file to be opened

OpenError DB "An error has occured(opening)!$"
ReadError DB "An error has occured(reading)!$"
array db 46(?)
Buffer DB 16 dup (?) ;buffer to store data
.code

start:


mov ax,@data ;base address of data segment
mov ds,ax ;put this in ds
re:
mov dx,OFFSET FileName ;put address of filename in dx
mov al,0 ;access mode - read and write
mov ah,3Dh ;function 3Dh -open a file
int 21h ;call DOS service

mov Handle,ax ;save file handle for later
jc ErrorOpening ;jump if carry flag set - error!
mov dx,offset Buffer ;address of buffer in dx
mov bx,Handle ;handle in bx
mov cx,46 ;amount of bytes to be read
mov ah,3Fh ;function 3Fh - read from file
int 21h ;call dos service
jc ErrorReading ;jump if carry flag set - error!

mov bx,Handle ;put file handle in bx
mov ah,3Eh ;function 3Eh - close a file
int 21h ;call DOS service

mov cx,46 ;length of string
mov si,OFFSET Buffer ;DS:SI - address of string
xor bh,bh ;video page - 0
mov ah,0Eh ;function 0Eh - write character

sub si,si
NextChar:

lodsb ;AL = next character in string
mov [array+si],al
inc si
loop NextChar

;xor ah,ah ;function 00h - get character
;int 16h ;interrupt 16h
;cmp al,041h
;je re
mov ax,4C00h ;terminate program
int 21h


ErrorOpening:
mov dx,offset OpenError ;display an error
mov ah,09h ;using function 09h
int 21h ;call DOS service
mov ax,4C01h ;end program with an errorlevel =1
int 21h

ErrorReading:
mov dx,offset ReadError ;display an error
mov ah,09h ;using function 09h
int 21h ;call DOS service

mov ax,4C02h ;end program with an errorlevel =2
int 21h



END start
the text file will look like this but with more characters
1
_
3
4
5
2
B
8
D
9
6
C
E
A
F
7

I am using tasm,x86.

dnapcex
December 6th, 2008, 03:34 AM
Hi smokymo!
I find something useful to help you.

INT 21,3D - Open File Using Handle
AH = 3D
AL = open access mode
00 read only
01 write only
02 read/write
DS:DX = pointer to an ASCIIZ file name
on return:
AX = file handle if CF not set
= error code if CF set (see DOS ERROR CODES)
Access modes in AL:
|7|6|5|4|3|2|1|0| AL
| | | | | `-------- read/write/update access mode
| | | | `--------- reserved, always 0
| `-------------- sharing mode (see below) (DOS 3.1+)
`--------------- 1 = private, 0 = inheritable (DOS 3.1+)
Sharing mode bits (DOS 3.1+): Access mode bits:
654 210
000 compatibility mode (exclusive) 000 read access
001 deny others read/write access 001 write access
010 deny others write access 010 read/write access
011 deny others read access
100 full access permitted to all
- will open normal, hidden and system files
- file pointer is placed at beginning of file

INT 21,3F - Read From File or Device Using Handle
AH = 3F
BX = file handle
CX = number of bytes to read
DS:DX = pointer to read buffer
on return:
AX = number of bytes read is CF not set
= error code if CF set (see DOS ERROR CODES)
- read specified number of bytes from file into buffer DS:DX
- when AX is not equal to CX then a partial read occurred due
to end of file
- if AX is zero, no data was read, and EOF occurred before read

INT 21,3E - Close File Using Handle
AH = 3E
BX = file handle to close
on return:
AX = error code if CF set (see DOS ERROR CODES)
- if file is opened for update, file time and date stamp
as well as file size are updated in the directory
- handle is freed

INT 21,9 - Print String
AH = 09
DS:DX = pointer to string ending in "$"
returns nothing
- outputs character string to STDOUT up to "$"
- backspace is treated as non-destructive
- if Ctrl-Break is detected, INT 23 is executed

INT 21,2 - Display Output
AH = 02
DL = character to output
returns nothing
- outputs character to STDOUT
- backspace is treated as non-destructive cursor left
- if Ctrl-Break is detected, INT 23 is executed