Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; multi-segment executable file template.
- data segment
- ; add your data here!
- pkey db "press any key...$"
- args db 100 dup (' ')
- msg db "no parametres! $"
- CmdLnLen equ byte ptr es:[80h] ;Command line length
- CmdLn equ byte ptr es:[81h] ;Command line data
- filein db 20 dup(' ')
- fileout db 20 dup(' ')
- handlein dw ?
- handleout dw ?
- someword db 50 dup(' ')
- buffer db 1000 dup(' ')
- coordx db 1
- coordy db 1
- vpage db 0
- clen dw 1000
- emsg db "ERROR WHILE READING $"
- ends
- stack segment
- dw 128 dup(0)
- ends
- code segment
- ; -------------
- ; AL - horizontal coord X ( start from 1 )
- ; AH - vertical coord Y ( 1 + )
- ; CL - page number ( 0 + )
- ; OUTPUT:
- ; DI - offset for character
- CharAddr PROC
- ;
- mov CH, AH
- mul CH ;
- shl AX, 1 ;
- sub AX, 2 ;
- ;
- mov DI, AX ;
- push DX ;
- mov AX, 4000 ;
- xor CH, CH
- mul CX ;
- pop DX ;
- ;
- add DI, AX
- ret
- CharAddr ENDP
- ; -------------------------------------------------------------------
- ; AL - horizontal coord
- ; AH - vertical coord
- ; DL - ASCII
- ; DH - attribute
- ; CL - page number ( 0 + )
- WriteChar PROC
- push ES
- ;push DI
- ; in ES - videobuffer
- push 0B800h
- pop ES
- ; calc full offset for character
- ; call CharAddr
- ; put symbol on buffer
- mov ES:[DI], DX
- ; pop DI
- pop ES
- ret
- WriteChar ENDP
- ; ----------------------------------------------------------------------
- ; load args from PSP to buffer
- loadargs proc near
- push bx
- push si
- push di
- push cx
- push ax
- lea bx, CmdLn
- lea si, CmdLnLen
- ; copy command line to our buffer:
- xor cx, cx ; zero cx register.
- mov cl, es:[si] ; get command line size.
- lea di, args ; load buffer address to di.
- cmp cx, 0 ; cx = 0 ?
- jz no_param ; then skip the copy.
- inc si ; copy from second byte.
- next_char:
- mov al, es:[bx]
- mov [di], al
- inc bx
- inc di
- loop next_char
- ; set '$' sign in the end of the buffer:
- mov byte ptr [di], '$'
- jmp exit ; skip error message.
- no_param:
- ; print out the error message:
- lea dx, msg
- mov ah, 9
- int 21h
- exit:
- pop ax
- pop cx
- pop di
- pop si
- pop bx
- ret
- loadargs endp
- getfilename proc near
- push bx
- push cx
- push ax
- push di
- push si
- lea bx, someword
- mov si, 1 ; counter for args string
- mov di, 0 ; counter for word & file name strings
- mov ax , 0 ; arg # 0 - word 1 - input file 2 - output file
- xor cx, cx
- mov cl, CmdLnLen
- dec cx ; ?
- loop1:
- cmp args[si], ' '
- jne not_eq
- mov [bx + di] , 0
- xor di, di
- cmp ax, 0
- jne ax_12
- inc ax ; ax - 1
- lea bx, filein
- jmp loop_1_end
- ax_12:
- cmp ax, 2
- je exit_loop1
- inc ax ; ax - 2
- lea bx, fileout
- jmp loop_1_end
- not_eq:
- push ax
- mov al , args[si]
- mov [bx + di], al
- pop ax
- inc di
- loop_1_end:
- inc si
- loop loop1
- exit_loop1:
- mov [bx + di] , 0 ; ASCIIZ string for file operations
- pop si
- pop di
- pop ax
- pop cx
- pop bx
- ret
- getfilename endp
- ; ---------------------------------------------------------------------
- ; find line with given word
- ; BX - start of line
- ; DI - end of line
- ; SI - current pos
- ; ---------------------------------------------------------------------
- findline proc near
- mov bx, 0 ; start of line ( beg of buffer - always start of line )
- mov si, 0
- mov di, 0
- xor cx, cx ; cl - cmp success
- ;
- loop_check:
- call findbeg ; place SI on beg of word; change DI as well
- cmp di, 0
- jne shuffle_2
- call cmpwords ; cmp word in buffer with given
- ; ret 1 in CX if equal
- cmp cx, 1
- jne not_eq_words
- ; write to file
- ; find EndOfLine
- mov di, si
- dec di
- eline:
- inc di
- cmp di, clen
- je endofbuffer ; write DI bytes
- cmp buffer[di], 0Dh
- jne eline
- endofbuffer:
- mov cx, di ; number of bytes to write
- inc cx ; index == count - 1 -> increment
- mov ah, 40h
- mov bx, handleout
- lea dx, buffer
- int 21h
- ; shuffle buffer
- shuffle_2:
- mov cx, di ; di - index of ODh
- mov si, di ;
- xor bx, bx
- inc si ; ignore ODh
- shuffle:
- mov al, buffer[si]
- mov buffer[bx], al
- inc bx
- inc si
- cmp si, clen
- jbe shuffle
- ; end shuffle
- sub clen, cx
- ;sub clen, 1 ; ?
- cmp clen, 0000h
- je ret_
- xor cx, cx
- xor bx, bx
- xor di, di
- mov si, -1
- not_eq_words:
- inc si
- cmp si, clen
- jne loop_check ;?
- ret_:
- ret
- findline endp
- ; ---------------------------------------------------------------------
- ; findbeg - find beginning of word
- ; ---------------------------------------------------------------------
- findbeg proc near
- mov al, 0
- loop_find_beg:
- cmp buffer[si], 20h ; space
- je _eq
- cmp buffer[si], 09h ; tab
- je _eq
- cmp buffer[si], 0Dh ; newline
- je _eq_nl
- cmp si, 0
- jne not_eq_
- ret
- _eq_nl:
- mov di, si ; find new line
- _eq:
- mov al, 1 ; set flag that previous char is delimeter
- jmp al_n1
- not_eq_:
- cmp al, 1
- jne al_n1
- ret
- al_n1:
- inc si
- cmp si, clen
- jne loop_find_beg
- ret
- findbeg endp
- ; ---------------------------------------
- ; cmpwords
- ; --------------------------------------
- cmpwords proc near
- push di
- mov di, 0
- xor cx, cx ; cx - 0
- push ax
- mov al, someword[di]
- loop_cmp:
- cmp buffer[si], al
- jne ret_failure
- inc si
- inc di
- mov al, someword[di]
- cmp al, 0
- je loop_css
- ;jne loop_cmp
- cmp si, clen
- jbe loop_cmp
- loop_css:
- mov cx, 1
- ret_failure:
- pop ax
- pop di
- cmp si, clen
- jne not_clen
- sub si, 1
- not_clen:
- ret
- cmpwords endp
- ;
- ; ----------------------------------------------------------------------
- start:
- ; set segment registers:
- push ds
- mov ax, data
- mov ds, ax
- mov es, ax
- ; ----------------------
- pop es
- call loadargs ; copy args from PSP to buffer string
- ; ----------------------
- call getfilename ; copy file names && target word to different strings
- ; open in file
- mov ah, 3Dh
- mov al, 02
- mov dx, offset filein
- int 21h
- mov handlein, ax
- ; open out file
- mov ah, 3Dh
- mov al, 01
- mov dx, offset fileout
- int 21h
- mov handleout, ax
- ; read from in file to buffer
- ; --- video
- ; set video mode
- mov ax, 0003h
- int 10h
- ; -------------
- mov di, 0
- ; mov clen , 0013h ; 20 char
- loop_read:
- mov clen, 03e8h
- mov ah, 3fh
- mov bx, handlein
- mov cx, clen
- mov dx, offset buffer
- int 21h
- jnc cf0
- mov ax, 0003h
- int 10h
- mov ah, 09h
- lea dx, emsg
- int 21h
- ;cmp ax, 0
- je ex
- cf0:
- cmp ax, 0
- je ex
- ; find line with given word
- ; -----------------------------------
- ;pusha
- mov clen, ax
- sub clen , 1
- mov di, ax
- inc di
- loop_find_nl:
- sub di, 1
- cmp di, 0
- je zero_di
- cmp buffer[di], 0Dh
- jne loop_find_nl
- mov si, ax ; AX - number of readed bytes
- sub si ,1
- sub di, si
- add clen, di ; ?
- ; change read pointer
- mov ax, 4201h
- mov bx, handlein
- mov cx, 0
- mov dx, di
- int 21h
- zero_di:
- ; video
- ; -----------------------------------
- mov si, 0
- mov di, 0
- ;loop_vout:
- ;
- ;
- ; ; push ax
- ; push dx
- ; push cx
- ; ;mov al, coordx
- ; ; mov ah, coordy
- ; mov dl, buffer[si]
- ; cmp dl, 0Ah
- ; je equal_
- ; cmp dl, 0Eh
- ; je equal_
- ; cmp dl, 0dh
- ; jne not_equal_
- ; equal_:
- ; mov dl, 20h
- ; not_equal_:
- ; mov dh, 1Fh
- ; mov cl, vpage
- ; call WriteChar
- ; add di, 2
- ;
- ;; cmp coordx, 80
- ;; jne go_2
- ;; mov coordx, 0
- ;;
- ;; cmp coordy, 25
- ;; jne go_
- ;; mov coordy, 1
- ;; inc vpage
- ;; jmp go_2
- ;;go_:
- ;; inc coordy
- ;;go_2:
- ;; inc coordx
- ; pop cx
- ; pop dx
- ;; pop ax
- ; inc si
- ;
- ; cmp si, clen
- ; jne loop_vout
- ;
- ; ; WAIT FOR ANY KEY
- ; mov ah, 1
- ; int 21h
- ; mov ax, 0003h ; clear screen
- ; int 10h
- ;
- ; ; end of vout
- ; find line in buffer
- call findline ; find line with given word and write in to out file
- ; -----------------------------
- cmp ax, ax
- je loop_read
- ex:
- ; close files
- mov bx, handleout
- mov ah, 3eh
- int 21h
- mov ah, 3eh
- mov bx, handlein
- int 21h
- ; wait for any key....
- mov ah, 1
- int 21h
- mov ax, 0003h ; clear screen
- int 10h
- mov ax, 4c00h ; exit to operating system.
- int 21h
- ends
- end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement