我在汇编中读取和写入文件,但我的程序崩溃了

时间:2016-01-02 17:09:47

标签: assembly masm

我需要从.txt文件一次读取8个字节。现在,我只想将该块显示到目标.txt文件。问题是我的程序崩溃了,我无法弄清楚在哪一点上。我选择一次读取一个字节,直到我有8个,然后重复直到文件结束。我用masm。这是代码:

.386
.model flat, stdcall
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
includelib msvcrt.lib
extern printf: proc
extern scanf: proc
extern fscanf: proc
extern fprintf: proc
extern fopen: proc
extern fclose: proc
extern exit: proc

;
public start

;data section
.data
  scanf_format db "%d",0
  printf_format db "%c",0

  ascii db 0 
  type_read db "r",0
  type_write db "w", 0
  destination db "destinatie.txt",0
  fscanf_format db "%c",0 
  fprintf_format db "%c",0
  start_msg db "The path to the source file :",0
  source dd 0 
  source_format db "%s", 0
  pointer_source dd 0
  pointer_destination dd 0

  buffer db 8 dup(0)
  buffer_l equ 7

  step_nr dd 0
  step dd 0
  end_of_file dd 0
;the code section
.code

start:
    xor eax,eax
    ; The path to the source file
    push offset start_msg
    add esp,4
    ; read the source file name
    push  offset source
    push offset source_format
    call scanf
    add esp,8
    ; open source file
    push offset type_read
    push offset source
    call fopen
    add esp,8
    mov pointer_source,eax 


    ; create the destination file 
    push offset type_write
    push offset destination
    call fopen
    mov pointer_destination,eax 
    add esp,8



read_file: 
       lea esi,buffer
       mov step,esi
       mov esi,step
       xor edi,edi
       mov edi,esi
       add edi,7
       dec esi
       for_0_7:
          push offset ascii
          push offset fscanf_format
          push pointer_source
          call fscanf
          add esp,12
          mov end_of_file,eax
          cmp end_of_file,0ffffffffH
          je write_file    
          xor ebx,ebx
          mov bl,ascii
          inc esi
          mov [esi],ebx
          cmp esi,edi
          jb for_0_7

       ;display the blocks
        mov end_of_file,eax 
        mov step,esi
        inc step   
        xor edi,edi
        xor esi,esi
        lea ESI, buffer
        mov ecx,8
        dec esi

       write_file:
         inc esi
         xor edx,edx
         mov dl,[esi]
         ;push [esi]
         push edx
         push offset fprintf_format
         push pointer_destination
         call fprintf
         add esp,12
         cmp [esi],ecx
         jne write_file
       cmp end_of_file,0ffffffffH
       jne read_file
jmp next


final :
         push 0
         call exit

next: push pointer_source
          call fclose
          add esp,4
          push pointer_destination
          call fclose
          add esp,4
          jmp final


end start

0 个答案:

没有答案