jewelthief
November 26th, 2006, 06:39 AM
I am novice in assembly.ca any body tell me how read character by character from the file?
I have used funtion 3Dh int 21h but it reads number specified bytes from the byte. Is there any funtion like C++ e.g. [get(ch)] which reads character by character from the file?
please help me.
Lars(NL)
December 6th, 2006, 12:27 PM
Well, this is a pretty vague question if you ask me since I don't have even the slightest clue how you are trying to read your file in the first place, so I'm gonna have to keep it very generic.
Whatever method you are using to read your file, in the process you'll most likely have a pointer of the location in the file you want to read from.
Let's say that this pointer is in the register "ecx".
Assuming that your file contents are in ANSI, as opposed to unicode, and thus each character takes up 1 byte you could read the file one character at a time in some way similar the following:
.repeat
mov al, [ecx]
;// Process "al" in some way.
add ecx, 1
.until some condition
You could even read 4 characters at a time into one register and then process those bytes one by one.
For instance, like such:
.repeat
xor eax, eax
mov al, [ecx]
add ecx, 1
mov ah, [ecx]
add ecx, 1
rol eax, 16
mov ah, [ecx]
add ecx, 1
mov al, [ecx]
add ecx, 1
rol eax, 16
;// Process the characters.
.until some condition
You could also move 4 bytes into eax at once (mov eax, [ecx]) and then read them out byte by byte again but then you have to keep a very close eye on which bytes you're reading.
It's faster and all but it's easier to make mistakes that way if you don't think properly for a second because the byte order is reversed between memory and registers.
iviggers
December 11th, 2006, 06:11 PM
Hi. Actually DOS 3dh function only opens your file, it returns a number (called file handler) but it doesn't read bytes. Did you mean DOS 3fh function?
I don't think there's a more simple substitute function for 3fh in DOS.
Assume you want to open an existing file for read-only operations (set ax to 3d00h for opening file). To read just one byte at a time until EOF is reached do:
read_loop:
mov ah,3fh
mov bx,file_handle ; as returned by function 3d00h
mov cx,1 ; just read 1 char
lea dx,buffer ; where you want to store the byte
int 21h
cmp al,1
jb EOF_or_error
... process new read byte
jmp read_loop
EOF_or_error:
... rest of code
Note that it is expensive to access so many times a file. Your program should instead read more bytes and process them using an offset variable. It all depends of what you want your code to do.