Advertisement
Levii_Valenok

Untitled

May 29th, 2022
3,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model small
  2. .stack 100h
  3. .data
  4.     ;file_path db 'C:\emu8086\MyBuild\test.txt', 0  
  5.     file_path db 'C:\borlandc\bin\test.txt', 0
  6.     crlf db 0Dh, 0Ah, '$'
  7.     wordd db 10, 0, 15 dup('$'), '$'
  8.     buf db 2, 0, 2 dup('$'), '$'
  9.     msg_prompt_to_input db 'Enter the word to search', 0Dh, 0Ah, '$'
  10.     msg_result db 0Dh, 0Ah, 'Count of strings: $'
  11.     msg_bad db 0Dh, 0Ah, 'overflow$'
  12.     msg_bad_args db 'command line parse error', 0Dh, 0Ah, '$'
  13.     msg_empty_args db 'no command line args', 0Dh, 0Ah, '$'
  14.     msg_error db 0Dh, 0Ah, 'error', 0Dh, 0Ah, '$'
  15.     word_capacity equ 50
  16.     word_buffer db word_capacity + 2 dup(0)
  17.     cmd_capacity equ 127
  18.     cmd_length db ?
  19.     cmd_text db cmd_capacity dup('$')
  20.   ;  file_path db cmd_capacity dup('$')
  21.  
  22. .code
  23.  
  24. ;-----macros--------------------
  25.  
  26. is_empty macro str, is_0
  27.     push si
  28.     lea si, str
  29.     call strlen
  30.     pop si
  31.  
  32.     cmp ax, 0
  33.     je is_0
  34. endm
  35.  
  36.                                 ; string output macro
  37. puts macro str
  38.     push ax
  39.     push dx
  40.     lea dx, str
  41.     mov ah, 9
  42.     int 21h
  43.     pop dx
  44.     pop ax    
  45. endm
  46.  
  47.                                 ; string input marco
  48. gets macro str
  49.     push bx
  50.     push cx
  51.     push dx
  52.  
  53. again:                          ; check empty word input
  54.     mov ah, 0Ah
  55.     lea dx, str
  56.     int 21h
  57.  
  58.     xor ax, ax
  59.     xor cx, cx
  60.  
  61.     mov cl, [wordd + 1]
  62.     cmp cl, 0                   ; if str is empty
  63.     je again
  64.  
  65.     pop dx
  66.     pop cx
  67.     pop bx
  68. endm
  69.  
  70. puti proc                      ; integer number output
  71.     ;local put1
  72. ;    local put2
  73. ;    local ex
  74.  
  75.     push ax
  76.     push cx
  77.     push -1                     ; break condition
  78.     mov cx, 10
  79. put1:
  80.     xor dx, dx
  81.     ;xor ah, ah
  82.    ; div cl
  83.     div cx                     ; al - result, ah - remainder
  84.     ;mov dl, ah
  85.     ;mov dx, ah
  86.     push dx
  87.     ;cmp al, 0
  88.     ;cmp ah, 0
  89.     cmp al, 0
  90.     jne put1
  91.  
  92.     mov ah, 2
  93.  
  94. put2:
  95.     pop dx
  96.     cmp dx, -1          ; if -1 break
  97.     je ex
  98.     add dl, '0'         ; to char
  99.     int 21h
  100.     jmp put2
  101. ex:
  102.     mov dl, ' '
  103.     int 21h
  104.     pop cx
  105.     pop ax
  106. endp
  107.  
  108. fopen macro
  109.     lea dx, file_path
  110.     mov ah, 3Dh
  111.     mov al, 00h
  112.     int 21h
  113.     jc exit
  114.  
  115.     mov bx, ax
  116. endm
  117.  
  118. fclose macro
  119.     mov ah, 3Eh     ; close file function
  120.     int 21h
  121. endm
  122.  
  123. fread macro
  124.     local continue
  125.     push ax
  126.     push cx
  127.     push dx
  128.  
  129.     mov cx, 1
  130.     lea dx, buf
  131.  
  132.     mov ah, 3Fh         ; read from file
  133.     int 21h             ; bx - file id
  134.     jc exit             ; cx - bytes count for reading
  135.                         ; cf == 1 -> error
  136.     mov cx, ax          ; returns bytes have been read to ax
  137.     test cx, cx         ; if eof
  138.     jnz continue
  139.     fclose
  140.  
  141.     jmp good_exit
  142.  
  143. continue:
  144.     pop dx
  145.     pop cx
  146.     pop ax
  147. endm
  148.  
  149. ;------procedures---------------
  150.  
  151.    ;returns 0 if an error has occured else 1 to ax
  152.  parse_cmd_text proc
  153.      push bx
  154.      push cx
  155.      push dx
  156.  
  157.      mov cl, cmd_length                        
  158.      xor ch, ch
  159.  
  160.      lea si, cmd_text                           ; cmd text offset to source
  161.      lea di, file_path                          ; parsing result offset to data
  162.      call to_asciiz                             ; convert to asciiz
  163.  
  164.      is_empty file_path, bad_cmd_args          
  165.  
  166.      lea di, word_buffer
  167.      call to_asciiz
  168.  
  169.      is_empty word_buffer, good_cmd_args
  170.  
  171. ;-----errors handle
  172.  
  173.  bad_cmd_args:
  174.      puts msg_bad_args
  175.      mov ax, 1
  176.      jmp end_parse_cmd_text
  177.  
  178.  good_cmd_args:
  179.      mov ax, 0
  180.  
  181.  end_parse_cmd_text:
  182.      pop bx
  183.      pop cx
  184.      pop bx
  185.      ret    
  186.  parse_cmd_text endp
  187.  
  188.  
  189. to_asciiz proc
  190.     push ax
  191.     push cx
  192.     push di
  193.  
  194.     ;---------------------;
  195.  
  196.     parse_to_asciiz:
  197.         mov al, ds:[si]
  198.         cmp al, ' '
  199.         je is_delimeter
  200.         cmp al, 0Dh
  201.         je is_delimeter
  202.         cmp al, 09h
  203.         je is_delimeter
  204.         cmp al, 0Ah
  205.         je is_delimeter
  206.         cmp al, 00h
  207.         je is_delimeter
  208.         cmp al, '$'
  209.         je is_delimeter
  210.  
  211.         mov es:[di], al        ; write symbol
  212.         inc di                
  213.         inc si                
  214.     loop parse_to_asciiz
  215.  
  216. is_delimeter:
  217.     mov al, 00h
  218.     mov es:[di], al
  219.     mov al, '$'
  220.     inc di
  221.     mov es:[di], al
  222.     inc si
  223.  
  224.     ;---------------------;
  225.  
  226.     pop di
  227.     pop cx
  228.     pop ax
  229.     ret
  230. to_asciiz endp
  231.  
  232. strlen proc
  233.     push bx
  234.     push si
  235.  
  236.     xor ax, ax
  237. start_strlen:
  238.     mov bl, ds:[si]
  239.     cmp bl, 00h
  240.     je end_strlen
  241.     inc si
  242.     inc ax
  243.     jmp start_strlen
  244. end_strlen:
  245.     pop si
  246.     pop bx
  247.     ret
  248. strlen endp
  249.  
  250. ;------main---------------------------------
  251.  
  252. count_raws:  
  253.  
  254.     xor dx, dx
  255.     xor di, di    
  256.     xor ax, ax
  257.  
  258.     lea si, wordd+2
  259.     search:
  260.         fread
  261.        ; pop dx
  262.         mov al, [si]
  263.         mov cl, [buf]
  264.         cmp cl, 13 ;enter
  265.         je di_reset  
  266.         inc dx
  267.         ;inc di  ;num of pass symbols
  268.         cmp cl, al
  269.         je are_equals
  270.         jne search  
  271.  
  272.     di_reset:
  273.         ;xor di, di
  274.         xor dx, dx
  275.         xor si, si
  276.         lea si, wordd+2
  277.         xor ax, ax
  278.         jmp search
  279.     are_equals:
  280.         inc ah ;counter of successfull match
  281.         mov cl, [wordd+1]
  282.         cmp ah, cl
  283.         jge success
  284.         inc si
  285.     shift_to_the_begin_of_string:
  286.         push ax
  287.         push cx
  288.         push di
  289.         ;push dx
  290.         ;xor dx, dx
  291.         xor ax, ax
  292.         xor cx, cx
  293.         mov ax, 4201h
  294.         ;neg di
  295.         ;mov dx, di
  296.         neg dx
  297.         mov cx, -1
  298.         int 21h
  299.         ;xor di, di
  300.         xor dx, dx
  301.         ;pop dx
  302.         pop di
  303.         pop cx
  304.         pop ax
  305.         jmp search
  306.  
  307.       success:
  308.         ;inc dx
  309.         inc di
  310.         mov ah, 0
  311.  
  312.       skip:
  313.         fread
  314.         mov al, 13             ;    \n
  315.         mov cl, [buf]
  316.         cmp al, cl
  317.         jne skip
  318.         lea si, wordd+2
  319.         ;push dx
  320.         fread
  321.         ;xor di, di
  322.         xor dx, dx
  323.         jmp search
  324. ;      
  325.         jmp count_raws_end
  326.  
  327.  
  328.  
  329. ;    search:
  330. ;        fread
  331. ;        mov al, [wordd+2]
  332. ;        mov cl, [buf]
  333. ;        cmp cl, al
  334. ;        je check_word
  335. ;    jmp search
  336. ;    
  337. ;check_word:
  338. ;    lea si, wordd+2
  339. ;    mov al, [si]
  340. ;    mov ah, 1
  341. ;    
  342. ;    whilee:
  343. ;        inc ah
  344. ;        inc si
  345. ;        mov al, [si]
  346. ;        mov cl, [wordd+1]
  347. ;        cmp ah, cl
  348. ;        jg success
  349. ;        fread
  350. ;        mov cl, [buf]
  351. ;        cmp al, cl
  352. ;        jne search
  353. ;    je whilee
  354. ;    
  355. ;success:
  356. ;    inc dx
  357. ;    
  358. ;skip:
  359. ;    fread
  360. ;    mov al, 13             ;    \n
  361. ;    mov cl, [buf]
  362. ;    cmp al, cl
  363. ;    jne skip
  364. ;    fread
  365. ;    jmp search
  366.  
  367.  
  368. start:
  369.     mov ax, @data
  370.     mov es, ax
  371.     xor ch, ch
  372.     mov cl, ds:[80h]
  373.     mov cmd_length, cl
  374.     mov si, 82h
  375.     lea di, cmd_text
  376.     rep movsb               ;text from command line to variable cmd_text
  377.     mov ds, ax
  378.  
  379.     call parse_cmd_text
  380.     test ax, ax
  381.     jne exit
  382.  
  383.     puts msg_prompt_to_input
  384.     gets wordd
  385.     fopen
  386.     puts msg_result              
  387.     jmp count_raws
  388.  
  389. count_raws_end:
  390.  
  391. exit:
  392.     puts msg_error
  393.     pop dx
  394.     pop cx
  395.     pop ax
  396.     mov ax, 4c00h
  397.     int 21h
  398. good_exit:
  399.     puts crlf
  400.     pop dx
  401.     pop cx
  402.     pop ax
  403.     mov ax, di
  404.     call puti
  405.     mov ax, 4c00h
  406.     int 21h
  407. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement