Reputation: 63
I am writing a project in assembly language and I have a problem with read from a file and print on the screen what is written in it.
I took part of the code (which is the read and print part) and I tried to fix it and re write it and i still have a problem.
If someone can help me I'll be more then happy
this is the code :
org 100h
mov ah,0ah
mov dx,offset place
int 21h ; getting the place(directory) of the 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 the 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 to the place of the file
;------------------
call openfile ;open the file
;------------------
mov ah,3fh
mov si,offset filehandle
mov bx,[si] ;move file adress to bx
mov cx,40000 ;numbers of bytes to read
mov dx,offset buff ;pointer to read buffer
int 21h
mov si,offset filesize ;move si pointer to filesize
mov [si],ax ;move to filesize how many bytes read
;------------------
;writing on the screen ->
mov bx,offset buff ;move bx pointer of buffer
mov si,offset filesize
mov cx,[si] ;move cx how many to 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 to dx
add dx,2
int 21h
ret
endp gotoplace
proc openfile
mov ah,3d
mov al,2 ;open for read / write
mov dx,offset filename ;move dx offset filename
add dx,2
int 21h
;--------------------------
mov si,offset filehandle ;move offset filehandle(location in the memory) to si
mov [si],ax ;move the file adress to the 'filehandle'(location in the 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 this is the function that read and write :
proc readprint
call gotoplace ;go to the place of the file
;------------------
call openfile ;open the file
;------------------
mov ah,3fh
mov si,offset filehandle
mov bx,[si] ;move file adress to bx
mov cx,40000 ;numbers of bytes to read
mov dx,offset buff ;pointer to read buffer
int 21h
mov si,offset filesize ;move si pointer to filesize
mov [si],ax ;move to filesize how many bytes read
;------------------
mov ah,2
mov bh,0
mov dh,1
mov dl,1
int 10h ;Move the cursor to the start of the page
;writing on the screen ->
mov bx,offset buff ;move bx pointer of buffer
mov si,offset filesize
mov cx,[si] ;move cx how many to 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
Upvotes: 2
Views: 7359
Reputation: 10391
These are the problems in your code :
proc openfile
you use the number 3d that should be 3dH.mov dx,[si]
, which is an error because the length of the string (pointed by [si]
) is one byte, and you are moving two bytes to dx
.cx
to write to screen, that's why it must be DW.There is another problem that it's not your fault. EMU8086 has an issue when opening files. EMU8086 runs programs in subdirectory c:\emu8086\mybuild
, sometimes EMU8086 doesn't allow to open files outside the subdirectory mybuild
. In order to work with files in EMU8086, store them in c:\emu8086\mybuild
.
Next is your code. I fixed the problems and commented the code that changes the subdirectory, changes are pointed by arrows <========= :
org 100h
;mov ah,0ah
;mov dx,offset place
;int 21h ; getting the place(directory) of the file
;ADD 0 TO END OF STRING <==================================
;mov si,offset place
;inc si
;mov dl,[si] ;<== LENGTH OF STRING IS BYTE, NOT WORD
;mov dh,0 ;<================== CLEAR DH TO USE DX
;inc dx
;add si,dx ;<========= SI POINTS TO FINAL CHAR + 1
;mov [byte ptr si],0 ;<========= THE NUMBER ZERO 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 the file name
;ADD 0 TO END OF STRING <==================================
mov si,offset filename
inc si
mov dl,[si] ;<== LENGTH OF STRING IS BYTE, NOT WORD
mov dh,0 ;<================== CLEAR DH TO USE DX
inc dx
add si,dx ;<========= SI POINTS TO FINAL CHAR + 1
mov [byte ptr si],0 ;<========= THE NUMBER ZERO HAS NO SIZE
;LINE BREAK.
mov ah,02
mov dl,13
int 21h
mov ah,02
mov dl,10
int 21h
;call gotoplace ;go to the place of the file
;------------------
call openfile ;open the file
;------------------
mov ah,3fh
mov si,offset filehandle
mov bx,[si] ;move file adress to bx
mov cx,40000 ;numbers of bytes to read
mov dx,offset buff ;pointer to read buffer
int 21h
mov si,offset filesize ;move si pointer to filesize
mov [si],ax ;move to filesize how many bytes read
;------------------
;writing on the screen ->
mov bx,offset buff ;move bx pointer of buffer
mov si,offset filesize
mov cx,[si] ;move cx how many to 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 ANY 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 to dx
add dx,2
int 21h
ret
endp gotoplace
proc openfile
mov ah,3dH
mov al,2 ;open for read / write
mov dx,offset filename ;move dx offset filename
add dx,2
int 21h
;--------------------------
mov si,offset filehandle ;move offset filehandle(location in the memory) to si
mov [si],ax ;move the file adress to the 'filehandle'(location in the 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 WE CANNOT READ MORE THAN 64KB.
Upvotes: 2