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



spamtrap@xxxxxxxxxx wrote:

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)

Your problem is that int 21h/3Fh doesn't read a character into dl, it reads a character (cx characters, actually) into a buffer pointed to by dx. You'll need to allocate a buffer, with your other data, "input_buffer db 0", then, before the read, "mov dx, offset input_buffer". Now the int 21h/3Fh will put a character in the buffer - you'll have to get it into al for the int 10h/0Eh - "mov al, [input_buffer]" (the "[]"s are optional - I like 'em for "clarity"). I think that'll solve your problem...


Mmmm, maybe not... It isn't an error to read past the end of a file, so the "jc terminate" will never happen. If there's no error, ax contains the number of bytes actually read. So leave the "jc terminate" where it is (we still want to exit on error), and follow it with:

or ax, ax  ; set flags according to ax
jz terminate

*That'll* solve your problem... or get you closer, anyway :)

Best,
Frank

.



Relevant Pages

  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • Re: [Bugme-new] [Bug 12562] New: High overhead while switching or synchronizing threads on diffe
    ... Perhaps of the big buffer size of 512KB? ... second producer and a second consumer reduces the performance to 33%. ... int CTHREADPAIRCOUNT; ... int terminate; ...
    (Linux-Kernel)
  • Re: To Richard Heathfield
    ... int main ... But it still suffers from one problem - what if the input stream ... themselves, buffer overrun attacks were unheard of, and programmers ... up to either the end of the stream or a newline character if that is ...
    (comp.lang.c)
  • Re: help with block i/o
    ... we know that character read ... the last page is quite linkely to have the EOF ... fp_on_read(state, buffer, read)) break; ...
    (comp.lang.c)
  • Re: Non-blocking gets ?
    ... If I was programming a Windows console app in C, ... to check if there was a character in the buffer. ... int c = FIX2INT; ...
    (comp.lang.ruby)