project - read and write from a txt file (assembly) -
i writing project in assembly language , have problem read file , print on screen written in it.
i took part of code (which read , print part) , tried fix , re write , still have problem.
if can me i'll more happy
this code :
org 100h mov ah,0ah mov dx,offset place int 21h ; getting place(directory) of file mov si,offset place inc si mov dx,[si] inc dx mov si,dx mov [si],0 mov ah,02 mov dl,13 int 21h mov ah,02 mov dl,10 int 21h mov ah,0ah mov dx,offset filename int 21h ;getting file name mov si,offset filename inc si mov dx,[si] inc dx mov si,dx mov [si],0 mov ah,02 mov dl,13 int 21h mov ah,02 mov dl,10 int 21h call gotoplace ;go place of file ;------------------ call openfile ;open file ;------------------ mov ah,3fh mov si,offset filehandle mov bx,[si] ;move file adress bx mov cx,40000 ;numbers of bytes read mov dx,offset buff ;pointer read buffer int 21h mov si,offset filesize ;move si pointer filesize mov [si],ax ;move filesize how many bytes read ;------------------ ;writing on screen -> mov bx,offset buff ;move bx pointer of buffer mov si,offset filesize mov cx,[si] ;move cx how many write startwrite: mov ah,2 mov dl,[bx] ;move dl letter in place [bx] int 21h inc bx dec cx jnz startwrite proc gotoplace mov ah,3bh mov dx,offset place ;move offset place dx add dx,2 int 21h ret endp gotoplace proc openfile mov ah,3d mov al,2 ;open read / write mov dx,offset filename ;move dx offset filename add dx,2 int 21h ;-------------------------- mov si,offset filehandle ;move offset filehandle(location in memory) si mov [si],ax ;move file adress 'filehandle'(location in meory' ret endp openfile ret filehandle dd ? filename db 40 db 42 dup (0) place db 40 db 42 dup (0) buff db 40000 dup (0) filesize dd ?
and function read , write :
proc readprint call gotoplace ;go place of file ;------------------ call openfile ;open file ;------------------ mov ah,3fh mov si,offset filehandle mov bx,[si] ;move file adress bx mov cx,40000 ;numbers of bytes read mov dx,offset buff ;pointer read buffer int 21h mov si,offset filesize ;move si pointer filesize mov [si],ax ;move filesize how many bytes read ;------------------ mov ah,2 mov bh,0 mov dh,1 mov dl,1 int 10h ;move cursor start of page ;writing on screen -> mov bx,offset buff ;move bx pointer of buffer mov si,offset filesize mov cx,[si] ;move cx how many write startwrite: mov ah,2 mov dl,[bx] ;move dl letter in place [bx] int 21h inc bx dec cx jnz startwrite ;------------------ ret endp readprint
these problems in code :
- in
proc openfile
use number 3d should 3dh. - after capturing "place" , "filename" insert chr(0) @ end of both strings, doing
mov dx,[si]
, error because length of string (pointed[si]
) 1 byte, , moving 2 bytesdx
. - the file size type dd should dw, remember use number in
cx
write screen, that's why must dw. - you forgot finish program properly.
there problem it's not fault. emu8086 has issue when opening files. emu8086 runs programs in subdirectory c:\emu8086\mybuild
, emu8086 doesn't allow open files outside subdirectory mybuild
. in order work files in emu8086, store them in c:\emu8086\mybuild
.
next code. fixed problems , commented code changes subdirectory, changes pointed arrows <========= :
org 100h ;mov ah,0ah ;mov dx,offset place ;int 21h ; getting place(directory) of file ;add 0 end of string <================================== ;mov si,offset place ;inc si ;mov dl,[si] ;<== length of string byte, not word ;mov dh,0 ;<================== clear dh use dx ;inc dx ;add si,dx ;<========= si points final char + 1 ;mov [byte ptr si],0 ;<========= number 0 has no size ;line break. ;;mov ah,02 ;mov dl,13 ;int 21h ;mov ah,02 ;mov dl,10 ;int 21h mov ah,0ah mov dx,offset filename int 21h ;getting file name ;add 0 end of string <================================== mov si,offset filename inc si mov dl,[si] ;<== length of string byte, not word mov dh,0 ;<================== clear dh use dx inc dx add si,dx ;<========= si points final char + 1 mov [byte ptr si],0 ;<========= number 0 has no size ;line break. mov ah,02 mov dl,13 int 21h mov ah,02 mov dl,10 int 21h ;call gotoplace ;go place of file ;------------------ call openfile ;open file ;------------------ mov ah,3fh mov si,offset filehandle mov bx,[si] ;move file adress bx mov cx,40000 ;numbers of bytes read mov dx,offset buff ;pointer read buffer int 21h mov si,offset filesize ;move si pointer filesize mov [si],ax ;move filesize how many bytes read ;------------------ ;writing on screen -> mov bx,offset buff ;move bx pointer of buffer mov si,offset filesize mov cx,[si] ;move cx how many write startwrite: mov ah,2 mov dl,[bx] ;move dl letter in place [bx] int 21h inc bx dec cx jnz startwrite ;wait until user press key <=========================== mov ah,7 int 21h ;finish program <========================================== mov ax, 4c00h int 21h proc gotoplace mov ah,3bh mov dx,offset place ;move offset place dx add dx,2 int 21h ret endp gotoplace proc openfile mov ah,3dh mov al,2 ;open read / write mov dx,offset filename ;move dx offset filename add dx,2 int 21h ;-------------------------- mov si,offset filehandle ;move offset filehandle(location in memory) si mov [si],ax ;move file adress 'filehandle'(location in meory' ret endp openfile ret filehandle dd ? filename db 40 db 42 dup (0) place db 40 db 42 dup (0) buff db 40000 dup (0) filesize dw ? ;<========= in 8086 cannot read more 64kb.
Comments
Post a Comment