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




<spamtrap@xxxxxxxxxx> wrote in message
news:1127764694.935366.135340@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> hi, yes but [dx] is not allowed in emu8086. so i figured out the
> solution:

(smile) but for sure it is not allowed. Am I to do all of your
work? :)

> copy dx to bp on which [] is allowed.. then copy [bp] to ax. this is
> working well.

Please note that [bp] will use the stack segment by default
instead of the data segment as you would expect. It just
so happens that in your .COM program, ss = ds.

You could also use direct addressing

mov al,[buffer]

and not have to use a register at all.

> thanks for the help!

You're welcome.
Ben

> Benjamin David Lunt wrote:
>> <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: 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)
  • Re: detab utility challenge (wrapping up).
    ... opens up UB is argvcontains over 500 character is unacceptable. ... Another problem is that putchar needs to be tested for EOF on every call, ... but it will only break on pathological implementations or (more ... Mission creep should be seen for what it is, ...
    (comp.lang.c)
  • Re: confused about behaviour of scanf
    ... Scanf also always leaves the terminating char in the input stream, so flushln (barring EOF or error) will always have a '\n' to terminate on. ... They allow overflow detection, ... detect the character that terminated a numerical field. ... Skipblks returns the char that getc will next return, ...
    (comp.lang.c)