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



spamtrap@xxxxxxxxxx schrieb:
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

You need a data-segment.

maybe:
mov ax, cs
mov ds, ax

For the beginning you need more stuff like:

Ralf Browns IRQ-List(RBIL)
http://www.pobox.com/~ralf
http://www.pobox.com/~ralf/files.html
ftp://ftp.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/

RBIL->Inter61b.zip->Interrup.f
INT 21 - DOS 2+ - "OPEN" - OPEN EXISTING FILE
	AH = 3Dh
	AL = access and sharing modes (see #01402)
	DS:DX -> ASCIZ filename

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 dx, OFFSET buffer

INT 21 - DOS 2+ - "READ" - READ FROM FILE OR DEVICE
	AH = 3Fh
	BX = file handle
	CX = number of bytes to read
	DS:DX -> buffer for data

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)

No, the byte is stored at ds:buffer.

mov dl, buffer

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 ?
    ^
You dont acess this location.

buffer DB ?
;-------------------

Dirk

.



Relevant Pages

  • Re: Converting code to work with Tasm :-)
    ... mov ax, 1112h ... AL = 00h, 10h: load user-specified patterns ... BH = number of bytes per character pattern ... the current block specifiers may be determined with INT 10/AH=1Bh, ...
    (alt.lang.asm)
  • Re: cpuid
    ... Each bytes represents one character of text, using some character encoding, usually ASCII. ... So to print a 32 bit register in binary, all you have to do is take the bits one by one, check whether each bit is a 1 or a 0, and put either '1' or '0' into a buffer accordingly, and then print the buffer. ... mov %cl, buf# store one char in the buffer ... int $0x80 ...
    (comp.lang.asm.x86)
  • 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: 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, terminate program ... mov dx, offset buf;get address of buf to DX. ...
    (comp.lang.asm.x86)
  • Re: opening and reading a character from a file with emu8086 assembler
    ... into a buffer pointed to by dx. ... 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, ... It isn't an error to read past the end of a file, so the "jc terminate" will never happen. ...
    (comp.lang.asm.x86)