Facebook
From asc, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 129
  1. bits 32
  2.  
  3. global start        
  4.  
  5. extern exit, printf, scanf, fprintf, fopen, fclose              
  6. import exit msvcrt.dll    
  7. import printf msvcrt.dll
  8. import scanf msvcrt.dll
  9. import fopen msvcrt.dll
  10. import fclose msvcrt.dll
  11. import fprintf msvcrt.dll          
  12.  
  13.  
  14. segment data use32 class=data
  15.     file_name db "numbers.txt", 0
  16.     format db "%s", 0
  17.     access_mode db "w", 0
  18.     file_descriptor dd -1
  19.     count db 0
  20.     n resd 1
  21.  
  22. segment code use32 class=code
  23.     start:
  24.         ;A file name (defined in data segment) is given.
  25.         ;Create a file with the given name, then read words from the keyboard until character '$' is read from the keyboard.
  26.         ;Write only the words that contain at least one digit to file.
  27.    
  28.         ;Open the file
  29.         push dword access_mode
  30.         push dword file_name
  31.         call [fopen]
  32.         add esp, 4*2
  33.        
  34.         ;Store the file descriptor
  35.         mov [file_descriptor], eax
  36.        
  37.         ;Check if [fopen] has successfully created the file
  38.         cmp eax, 0
  39.         je end_repeat
  40.        
  41.         ; Loop until we read $
  42.         ; Check if at least one byte is a digit
  43.        
  44.         ; Bag adresa lu n in esi ca sa pot da lodsb
  45.         mov esi, n
  46.        
  47.         repeat:
  48.             ; Reset counter (in counter o sa fie 1 daca am gasit digit si 0 daca nu)
  49.             mov byte[count], 0
  50.            
  51.             ; Read n
  52.             push dword n
  53.             push dword format
  54.             call [scanf]
  55.             add esp, 4*2
  56.            
  57.            
  58.             ; Compare n to $
  59.             cmp dword[n], '$'
  60.             je end_repeat
  61.            
  62.             ; Loop through every byte of the dword
  63.             repeat2:
  64.                 lodsb
  65.  
  66.                 ; Daca ascii e intre 48 si 57 adaug 1 in count
  67.                
  68.                 cmp al, 48
  69.                 jl repeat2
  70.                
  71.                 cmp al, 57
  72.                 jg repeat2
  73.                
  74.                 add byte[count], 1
  75.            
  76.            
  77.             cmp byte[count], 0
  78.             je do_not_write
  79.            
  80.            
  81.             write:
  82.             ; Write n to the file
  83.             push esi
  84.             push dword n
  85.             push dword [file_descriptor]
  86.             call [fprintf]
  87.             add esp, 4*2
  88.             pop esi
  89.            
  90.             jmp repeat
  91.            
  92.             do_not_write:
  93.             jmp repeat
  94.            
  95.         end_repeat:
  96.        
  97.         ;Close the file
  98.         push dword [file_descriptor]
  99.         call [fclose]
  100.         add esp, 4
  101.        
  102.         push dword 0
  103.         call [exit]
  104.