assembly 从txt文件(程序集)读取和写入

s5a0g9ez  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(220)

我正在用汇编语言写一个项目,我有一个问题,从一个文件中读取并在屏幕上打印它所写的内容。
我取了部分代码(这是读取和打印的部分),我试图修复它并重新编写它,我仍然有一个问题。
如果有人能帮我我会更开心

这是代码:

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 ?

这是读取和写入的函数:

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
lvjbypge

lvjbypge1#

以下是代码中的问题:

  • proc openfile中,您使用的数字3d应该是3dH。
  • 捕获“place”和“filename”后,在两个字符串的末尾插入chr(0),但执行的是mov dx,[si],这是一个错误,因为字符串(由[si]指向)的长度是一个字节,而您将两个字节移动到dx
  • 文件大小类型是DD,但应该是DW,请记住,您将在cx中使用此数字来写入屏幕,这就是它必须是DW的原因。
  • 你忘了好好完成程序。

还有另一个问题,这不是你的错。EMU8086在打开文件时有一个问题。EMU8086在子目录c:\emu8086\mybuild中运行程序,有时EMU8086不允许打开子目录mybuild之外的文件。为了使用EMU8086中的文件,请将它们存储在c:\emu8086\mybuild中。
接下来是你的代码。我修正了问题,并注解了修改子目录的代码,修改由箭头〈========指出:

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.

相关问题