Search code examples
assemblymasm

how to read blocks of 10 bytes from a file and display them in assembly?


I need to make an encryption algorithm to a file, but I have to work with blocks of 10 bytes and I saw that my program crashes when I just read blocks and display them.I checked with the debugger and I noticed that the program form only the first 10 bits block then the program does not do anything. Can anyone help me? I am working in MASM. This 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
  scanf_format db "%d",0
  printf_format db "%c",0
  error_format db "Error!", 0 
  ascii db 0 
  type_read db "r",0
  type_write db "w", 0
  nume_destination db "destinatie.txt",0
  fscanf_format db "%c",0 
  fprintf_format db "%c",0
  mesaj_start db "The path to the source file :",0
  nume_source dd 0 
  nume_source_format db "%s", 0
  pointer_source dd 0
  pointer_destination dd 0

  array db 10 dup(0)
  lungArray equ 9

  rotireCheie dd 0
  numarPasi dd 0
  pas dd 0
  sfarsitSource dd 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   
.code

start:
    xor eax,eax
    ; The path to the source file
    push offset mesaj_start
    call printf
    add esp,4
    ; read the source file name
    push  offset nume_source
    push offset nume_source_format
    call scanf
    add esp,8
    ; open source file
    push offset type_read
    push offset nume_source
    call fopen
    add esp,8
    mov pointer_source,eax 
    ; test if the file exists 
    cmp pointer_source,0
    je eroare

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

lea esi,array   
mov pas,esi

bucla3: 
       mov esi,pas
       xor edi,edi
       mov edi,esi
       add edi,9
       dec esi
     eticheta_for_0_9:
          push offset ascii
          push offset fscanf_format
          push pointer_source
          call fscanf
          add esp,12
          mov sfarsitSource,eax
          cmp sfarsitSource,0ffffffffH
          je afisare_bloc      
          xor ebx,ebx
          mov bl,ascii
          inc esi
          mov [esi],ebx
          cmp esi,edi
          jbe eticheta_for_0_9
         ;display the blocks
        mov sfarsitSource,eax 
        mov pas,esi
        inc pas     
        xor edi,edi
        xor esi,esi
        lea ESI, array
        mov ecx,10
        dec esi
       afisare_bloc:
         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 afisare_bloc
       cmp sfarsitSource,0ffffffffH
       jne bucla3 
jmp continua

ends :
         push 0
         call exit

continua: push pointer_source
          call fclose
          add esp,4
          push pointer_destination
          call fclose
          add esp,4
          jmp ends
eroare: push offset error_format
        call printf
        jmp ends    

end start

Solution

  • lea esi,array   
    mov pas,esi
    bucla3:  
    

    You need to put the label on top.

    cmp esi,edi
    jbe eticheta_for_0_9  
    

    You are doing 1 iteration too many! Use jb

    cmp esi,ecx
    jne afisare_bloc  
    

    How do you expect this to work? ESI is an address and ECX is the number 10.