Re: opening and reading a character from a file with emu8086 assembler




<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.



.



Relevant Pages

  • Re: User Input issue
    ... detect the character that terminated a numerical field. ... static int ignoreblks ... which may be \n or EOF ... Skipblks returns the char that getc will next return, ...
    (comp.lang.c)
  • Re: K&R 1.5.1 exercise
    ... Your while loop will eventually cause integer overflow, ... >> int main ... As EOF is a constant negative integer, you can start at INT_MIN and ... >>getcharcall will return the code of the first character you have typed ...
    (comp.lang.c)
  • Re: opening and reading a character from a file with emu8086 assembler
    ... reads on character at a time and print each character on the ... >> jc terminate;if error occurs, ... >> int 21h ... > As for the infinite loop, the carry is not set at EOF. ...
    (comp.lang.asm.x86)
  • Re: probably trivial: what is the eof character?
    ... >>> to terminate my input to it. ... >> There is no EOF character. ... > streams, it is a problematic statement. ...
    (comp.lang.lisp)
  • Re: detab utility challenge.
    ... it is possible that some valid character may ... I'd guess the answer is the same, use feof() to verify an eof ... no. Undefined Behaviour is a technical term from the standard. ... Any program written in any language can contain mistakes. ...
    (comp.lang.c)