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

时间:2015-06-04 17:02:29

标签: assembly project

我正在用汇编语言编写一个项目,我遇到了从文件中读取并在屏幕上打印出来的内容的问题。

我接受了部分代码(这是读取和打印部分),我试图修复它并重新编写它,但我仍然有问题。

如果有人可以帮助我,我会更开心

这是代码:

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 

1 个答案:

答案 0 :(得分:1)

这些是您的代码中的问题:

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

还有另外一个问题,那不是你的错。打开文件时EMU8086存在问题。 EMU8086在子目录cx中运行程序,有时EMU8086不允许打开子目录c:\emu8086\mybuild之外的文件。要使用EMU8086中的文件,请将它们存储在mybuild

接下来是您的代码。我修复了问题,并注释了更改子目录的代码,更改用箭头指示< =========:

c:\emu8086\mybuild