Advertisement
Levii_Valenok

Untitled

May 27th, 2022
3,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. .model small
  3. .stack 100h
  4. .data
  5.     ;file_path db 'C:\emu8086\MyBuild\test1.txt', 0  
  6.     ;file_path db 'C:\borlandc\bin\test1.txt', 0  
  7.    ; file_path db
  8.     crlf db 0Dh, 0Ah, '$'
  9.     wordd db 10, 0, 15 dup('$'), '$'
  10.     buf db 2, 0, 2 dup('$'), '$'
  11.     buf254 db 255, 0, 2 dup('$'), '$'
  12.    
  13.     msg_prompt_to_input db 'Enter the word to search', 0Dh, 0Ah, '$'
  14.     msg_result db 0Dh, 0Ah, 'End of the programs: $'
  15.     msg_bad db 0Dh, 0Ah, 'overflow$'
  16.     msg_bad_args db 'command line parse error', 0Dh, 0Ah, '$'
  17.     msg_empty_args db 'no command line args', 0Dh, 0Ah, '$'
  18.     msg_error db 0Dh, 0Ah, 'error', 0Dh, 0Ah, '$'
  19.     word_capacity equ 50
  20.     word_buffer db word_capacity + 2 dup(0)
  21.     cmd_capacity equ 127
  22.     cmd_length db ?
  23.     cmd_text db cmd_capacity dup('$')  
  24.     flag db 2, 0, 1 dup('$')
  25.     file_path db cmd_capacity dup('$')
  26.    
  27. .code
  28.  
  29. ;-----macros--------------------
  30.  
  31. is_empty macro str, is_0
  32.     push si
  33.     lea si, str
  34.     call strlen
  35.     pop si
  36.    
  37.     cmp ax, 0
  38.     je is_0
  39. endm
  40.  
  41.                                 ; string output macro
  42. puts macro str
  43.     push ax
  44.     push dx
  45.     lea dx, str
  46.     mov ah, 9
  47.     int 21h
  48.     pop dx
  49.     pop ax    
  50. endm
  51.  
  52.                                 ; string input marco
  53. gets macro str
  54.     push bx
  55.     push cx
  56.     push dx
  57.  
  58. again:                          ; check empty word input
  59.     mov ah, 0Ah
  60.     lea dx, str
  61.     int 21h
  62.    
  63.     xor ax, ax
  64.     xor cx, cx
  65.    
  66.     mov cl, [wordd + 1]
  67.     cmp cl, 0                   ; if str is empty
  68.     je again
  69.    
  70.     pop dx
  71.     pop cx
  72.     pop bx
  73. endm
  74.  
  75.  
  76.  
  77. fopen macro
  78.     lea dx, file_path
  79.     mov ah, 3Dh
  80.     mov al, 2
  81.     int 21h
  82.     jc exit
  83.    
  84.     mov bx, ax
  85. endm
  86.  
  87. fclose macro
  88.     mov ah, 3Eh     ; close file function
  89.     int 21h
  90. endm
  91.  
  92. fread macro
  93.     local continue
  94.     push ax
  95.     push cx
  96.     push dx
  97.    
  98.     mov cx, 1
  99.     lea dx, buf
  100.    
  101.     mov ah, 3Fh         ; read from file
  102.     int 21h             ; bx - file id
  103.     jc exit             ; cx - bytes count for reading
  104.                         ; cf == 1 -> error
  105.     mov cx, ax          ; returns bytes have been read to ax
  106.     test cx, cx         ; if eof
  107.     jnz continue
  108.     fclose
  109.    
  110.     jmp good_exit
  111.    
  112. continue:
  113.     pop dx
  114.     pop cx
  115.     pop ax
  116. endm
  117.  
  118.  
  119. fread254 macro
  120.     local continue
  121.     ;push ax
  122. ;    push cx
  123. ;    push dx
  124.    
  125.     mov cx, 254
  126.     lea dx, buf254
  127.    
  128.     mov ah, 3Fh         ; read from file
  129.     int 21h             ; bx - file id
  130.     jc exit             ; cx - bytes count for reading
  131.                         ; cf == 1 -> error
  132.     mov cx, ax          ; returns bytes have been read to ax
  133.     test cx, cx         ; if eof
  134.     jnz continue
  135. ;    fclose
  136. ;    
  137. ;    jmp good_exit
  138.    
  139. continue:
  140.     ;pop dx
  141. ;    pop cx
  142. ;    pop ax
  143. endm
  144.  
  145. ;------procedures---------------
  146.  
  147.    ;returns 0 if an error has occured else 1 to ax
  148.  parse_cmd_text proc
  149.      push bx
  150.      push cx
  151.      push dx
  152.    
  153.      mov cl, cmd_length                        
  154.      xor ch, ch
  155.    
  156.      lea si, cmd_text                           ; cmd text offset to source
  157.      lea di, file_path                          ; parsing result offset to data
  158.      call to_asciiz                             ; convert to asciiz
  159.    
  160.      is_empty file_path, bad_cmd_args          
  161.    
  162.      lea di, word_buffer
  163.      call to_asciiz
  164.    
  165.      is_empty word_buffer, good_cmd_args
  166.  
  167. ;-----errors handle
  168.    
  169.  bad_cmd_args:
  170.      puts msg_bad_args
  171.      mov ax, 1
  172.      jmp end_parse_cmd_text
  173.    
  174.  good_cmd_args:
  175.      mov ax, 0
  176.    
  177.  end_parse_cmd_text:
  178.      pop bx
  179.      pop cx
  180.      pop bx
  181.      ret    
  182.  parse_cmd_text endp
  183.  
  184.    
  185. to_asciiz proc
  186.     push ax
  187.     push cx
  188.     push di
  189.    
  190.     ;---------------------;
  191.    
  192.     parse_to_asciiz:
  193.         mov al, ds:[si]
  194.         cmp al, ' '
  195.         je is_delimeter
  196.         cmp al, 0Dh
  197.         je is_delimeter
  198.         cmp al, 09h
  199.         je is_delimeter
  200.         cmp al, 0Ah
  201.         je is_delimeter
  202.         cmp al, 00h
  203.         je is_delimeter
  204.         cmp al, '$'
  205.         je is_delimeter
  206.        
  207.         mov es:[di], al        ; write symbol
  208.         inc di                
  209.         inc si                
  210.     loop parse_to_asciiz
  211.    
  212. is_delimeter:
  213.     mov al, 00h
  214.     mov es:[di], al
  215.     mov al, '$'
  216.     inc di
  217.     mov es:[di], al
  218.     inc si
  219.    
  220.     ;---------------------;
  221.    
  222.     pop di
  223.     pop cx
  224.     pop ax
  225.     ret
  226. to_asciiz endp
  227.  
  228. strlen proc
  229.     push bx
  230.     push si
  231.    
  232.     xor ax, ax
  233. start_strlen:
  234.     mov bl, ds:[si]
  235.     cmp bl, 00h
  236.     je end_strlen
  237.     inc si
  238.     inc ax
  239.     jmp start_strlen
  240. end_strlen:
  241.     pop si
  242.     pop bx
  243.     ret
  244. strlen endp
  245.  
  246. ;------main---------------------------------
  247.  
  248. count_raws:
  249.    
  250.    
  251.     xor dx, dx
  252.     xor di, di    
  253.     xor ax, ax
  254.     xor cx, cx
  255.    
  256.     search:
  257.         cmp di, 0
  258.         je beginning_of_the_word
  259.         fread
  260.        
  261.         mov al, 32
  262.         mov cl, [buf]
  263.         inc di
  264.         cmp cl, al
  265.         je beginning_of_the_word
  266.         cmp cl, 0Ah
  267.         je di_reset
  268.         jmp search
  269.        
  270.         beginning_of_the_word:
  271.         fread
  272.         mov al, [wordd+2]
  273.         mov cl, [buf]
  274.         inc di ;number of pass symbols
  275.         cmp cl, 0Ah
  276.        
  277.         je di_reset      
  278.         cmp cl, al
  279.         je check_word
  280.         jmp search
  281.    
  282. di_reset:
  283.    xor di, di
  284.    ; inc di
  285.   ;  xor cx,cx
  286.   ;  mov dx,di
  287.    ; cmp dx, FFFFh
  288.    ; je incCX
  289.    ; incCX:
  290.      
  291.    ; push dx
  292.     ;xor di, di
  293.     jmp beginning_of_the_word
  294.    
  295. check_word:
  296.    
  297.     lea si, wordd+2
  298.     mov al, [si]
  299.     mov ah, 1
  300.    
  301.     whilee:
  302.         inc ah
  303.         inc si
  304.         mov al, [si]
  305.         mov cl, [wordd+1]
  306.         cmp ah, cl
  307.         jg success
  308.         fread
  309.         mov cl, [buf]
  310.         inc di
  311.         cmp al, cl
  312.         jne search
  313.     je whilee
  314.    
  315. success:
  316.    fread
  317.    xor cx, cx
  318.    inc di
  319.    mov cl, [buf]
  320.    cmp cl, 32
  321.    je skip
  322.    cmp cl, 0Ah
  323.    je if_end
  324.    jmp check_word
  325.    
  326.    ;go_next:
  327. ;   push dx
  328. ;   cmp cl, 0Ah
  329. ;   je if_end
  330.  
  331. skip:
  332.     fread
  333.     inc di
  334.     ;mov al, 13
  335.     mov al, 0Ah            ;    \n
  336.     mov cl, [buf]
  337.     cmp al, cl
  338.     jne skip
  339.     if_end:
  340.     ;inc di
  341.     xor dx,dx
  342.     push di ;length of deletable string
  343.     jmp read_next_line
  344.  
  345. read_next_line:
  346.    
  347.     ;add di, 2
  348.     ;mov dx, di
  349.     ;push dx
  350.     xor ax, ax
  351.     fread254
  352.     mov cx, ax
  353.     xor dx, dx
  354.    
  355.     pop di
  356.     ;pop dx
  357.    
  358.     ;add di, ax
  359.     ;push di
  360.     metka:
  361.     ;mov ax, dx
  362.      
  363.    
  364.     lea dx, buf254
  365.     push di
  366.     push cx
  367.     push dx
  368.     push ax
  369.    
  370.    
  371. shift_to_begin_of_deletable_string: ;to set position to begin of line
  372.      xor ax, ax
  373.      xor cx, cx
  374.      
  375.      pop ax
  376.      
  377.      ;inc di
  378.      
  379.      xor dx, dx
  380.      mov dx, di
  381.      add dx, ax
  382.      ;add dx, 1
  383.      neg dx
  384.      mov cx, -1
  385.      
  386.      ;neg di
  387.      xor ax, ax
  388.      mov ax, 4201h
  389.      
  390.      ;neg di
  391.      ;mov dx, di
  392.      ;mov cx, -1
  393.      int 21h
  394.      xor cx, cx
  395.      xor dx, dx
  396.      pop dx
  397.      ;pop di
  398.      
  399.        
  400. delete_line:
  401.     ;mov cx, ax
  402.     pop cx
  403.    ; mov dx, ax
  404.     pop di
  405.     mov ah, 40h
  406.     int 21h        
  407.    
  408.     cmp ax, 254
  409.     jl cut_file
  410.     jge read_next
  411.    
  412.    
  413.     read_next:
  414.     xor cx, cx
  415.     xor dx,dx
  416.     xor ax,ax  
  417.  
  418.    
  419.     mov dx, di
  420.     mov ax, 4201h
  421.     int 21h
  422.    
  423.     jmp read_next_254_bytes
  424.    
  425.    
  426.     read_next_254_bytes:
  427.   ;  pop di
  428. ;    pop dx
  429. ;    add dx, 254
  430. ;    push di
  431. ;    push dx
  432.     xor cx,cx
  433.     fread254
  434.     ;pop di
  435.     ;pop dx
  436.     ;add dx, 254
  437. ;    add di,dx
  438. ;    mov cx, ax
  439.    
  440.     jmp metka
  441.    
  442.    
  443.  ;   xor ax, ax
  444. ;    xor dx, dx
  445.     ;xor cx, cx
  446. ;    sub di, dx
  447. ;    mov dx, di
  448. ;    mov ax,4200h ;to shift from end of line
  449. ;    ;neg di
  450. ;    
  451. ;    int 21h
  452. ;    fread
  453. ;    mov cl, [buf]
  454.     cut_file:
  455.    
  456.     xor cx, cx
  457.     mov cx, 0
  458.     xor dx, dx
  459.     xor ax, ax
  460.     mov ah, 40h
  461.     int 21h
  462.    
  463.     go_to_begin_file:
  464.     xor cx, cx
  465.     xor ax,ax
  466.     xor dx,dx
  467.     xor di,di
  468.     mov ax, 4200h  
  469.     mov dx, 0
  470.     int 21h
  471.    
  472. jmp count_raws
  473.    
  474.    
  475.              
  476.  
  477. start:
  478.     mov ax, @data
  479.     mov es, ax
  480.     xor ch, ch
  481.     mov cl, ds:[80h]
  482.     mov cmd_length, cl
  483.     mov si, 82h
  484.     lea di, cmd_text
  485.     rep movsb               ;text from command line to variable cmd_text
  486.     mov ds, ax
  487.    
  488.     call parse_cmd_text
  489.     test ax, ax
  490.     jne exit
  491.    
  492.     puts msg_prompt_to_input
  493.     gets wordd
  494.     fopen
  495.     puts msg_result              
  496.     jmp count_raws
  497.    
  498. count_raws_end:
  499.  
  500. exit:
  501.     puts msg_error
  502.     pop dx
  503.     pop cx
  504.     pop ax
  505.     mov ax, 4c00h
  506.     int 21h
  507. good_exit:
  508.     puts crlf
  509.    ; pop dx
  510. ;    pop cx
  511. ;    pop ax
  512.     xor ax, ax
  513.     ;call puti
  514.     mov ax, 4c00h
  515.     int 21h
  516. end start
  517.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement