The MASM Forum

General => The Campus => Topic started by: c.2602 on January 03, 2016, 09:08:16 AM

Title: Read And Write From And To A File Program Crashes
Post by: c.2602 on January 03, 2016, 09:08:16 AM
I need to read 8 byte at a time from a .txt file. For now, I just want to display that block to a destination .txt file. The problem is that my program is crashing and I cannot figure out at which point. It does get the first 8 byte segment and then crashes. It seems that it stops working at mov dl,[esi](i tried using ollydbg), but I am new to assembly so I am not sure. Can anyone please help or give me some suggestions/advice? I use masm. Here is the code:



.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 64 dup(0)
  buffer_l equ 63

  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
   call printf
    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,63
       dec esi
       for_0_63:
          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_63

       ;display the blocks
        mov end_of_file,eax
        mov step,esi
        inc step   
        xor edi,edi
        xor esi,esi
        lea ESI, buffer
        mov ecx,64
        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
Title: Re: Read And Write From And To A File Program Crashes
Post by: jj2007 on January 03, 2016, 09:59:13 AM
First of all: Welcome to the Forum :icon14:

Your code crashes first here:
         xor edx,edx
         mov dl,[esi]  ; doesn't make sense
         ;push [esi]
         push ecx  ; needs to be preserved
         push edx
         push offset fprintf_format
         push pointer_destination
         call fprintf
         add esp,12
         pop ecx
         cmp esi,[ecx]  ; why is there a loop here?


There will be more errors. Check Tips, Tricks & Traps (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm) for some rules, in particular on preserving registers. And please, learn how to use the invoke macro. Pushing args "by hand" leads to buggy code.
Title: Re: Read And Write From And To A File Program Crashes
Post by: c.2602 on January 03, 2016, 07:34:52 PM
I tried making some modifications but now it does not work at all. Also I read the part about preserving registers, I understood why I should do it, but I do not really understand how to. Could you possibly be more specific on what should I do to make it work? I am really struggling with this.  :(
Title: Re: Read And Write From And To A File Program Crashes
Post by: jj2007 on January 03, 2016, 08:19:31 PM
That is register ecx preserving:
push ecx
invoke someapi, arg1, arg2, arg3
pop ecx


Btw where did you get the ideas for your code? It looks very, very old-style, almost nobody codes like that any more. if you want to learn assembler seriously, you should study the examples folder, e.g. \Masm32\examples\exampl07\fileio\ppfileio.asm
Title: Re: Read And Write From And To A File Program Crashes
Post by: c.2602 on January 03, 2016, 08:54:22 PM
We learned just the basics in class, like what data types are there, basic operations, to print and read. So I looked at an example and tried to adapt it to what I need to do. I will check the section, thanks.