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



> 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
>
......
>
> so where is the bug?


Here is the solution:


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, 99 ;read as much as we can!
mov dx, offset buf ;get address of buf to DX.
mov ah, 3fh ;read from the opened file (its handler in bx)
int 21h
JZ terminate ;end program if wrong file handle or some terrible
I/O error.

; here you can click the "vars" button,
; click "buf",
; set show as "ascii",
; elements=99
; and you should be able to see what we just read from the file.


MOV SI, offset buf ; get address of buf to SI.
print:
mov al, [SI]
mov ah,0eh ;print character
int 10h
INC SI
CMP [SI], 0
JNE print ;repeat if not end of file


terminate:

mov ah, 0 ; wait for any key...
int 16h

ret


file db "c:\input.txt",0
buf db 99 dup(0)
counter db 0



end

Hope this helps.

========================================================
In addition it's better if you update your emu8086 to version
4.00-Beta-12 or up.

If you have any other questions feel free to e-mail:
pnp[at]emu8086[dot]com

========================================================

.


Quantcast