Re: opening and reading a character from a file with emu8086 assembler
- From: "Benjamin David Lunt" <spamtrap@xxxxxxxxxx>
- Date: Sat, 24 Sep 2005 19:19:33 +0000 (UTC)
<spamtrap@xxxxxxxxxx> wrote in message
news:1127560389.208499.175090@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi! I am new to assembly. I am currrently writing a program that opens
> a file, reads on character at a time and print each character on the
> screen. It prints the '$' character in a seemingly infinite loop. Here
> is my code:
>
> org 100h ;.com memory layout
>
> mov dx, offset file ;address of file to dx
> mov al,0 ;openfile (read-only)
> mov ah,3dh
> int 21h ;call the interupt
> jc terminate ;if error occurs, terminate program
> mov bx,ax ;put handler to file in bx
>
> mov cx,1 ;read one character at a time
> print:
> mov ah,3fh ;read from the opened file (its handler in bx)
> int 21h
> jc terminate ;end program if end of file is reached
> mov ax,dx ;char is in dl, send to ax for printing (char is in al)
This service does not return the character in DX. This service
reads CX bytes to the buffer pointed to by ds:dx, so the character
is at ds:[dx] which just so happens to be the first character of
FILE below.
As for printing '$', I think that dx just may be 0x0124, since the
offset of FILE may be 0x0124.
As for the infinite loop, the carry is not set at EOF. Check
for ax == cx after the service. AX is returned as the amount
of bytes actually read. If AX == CX, then EOF was not found
upon trying to read a number of bytes. This doesn't mean that
the current file posision isn't at the eof, it just means that
eof was not encountered when trying to read CX bytes.
> mov ah,0eh ;print character
> int 10h
> jmp print ;repeat if not end of file
>
> terminate:
> ret
>
> file db "c:\input.txt",0
> handle dw ?
Ben
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Forever Young Software
http://www.frontiernet.net/~fys/index.htm
To reply by email, please remove the zzzzzz's
Batteries not included,
Some assembly required.
.
- Follow-Ups:
- Prev by Date: Figuring out table based encryption in assembly
- Next by Date: Re: opening and reading a character from a file with emu8086 assembler
- Previous by thread: Re: opening and reading a character from a file with emu8086 assembler
- Next by thread: Re: opening and reading a character from a file with emu8086 assembler
- Index(es):
Relevant Pages
|