home *** CD-ROM | disk | FTP | other *** search
- ;; DIRSORT.ASM - simple program to get all filenames in the current directory
- ;; sort all in asending or desending order and display them.
- ;;
- ;; This program can be assembled using the A86 or TASM assemblers
- ;;
- ;; Not tested with Masm, should work.
- ;;
- ;; This code is "PUBLIC DOMAIN"
- ;;
- ;; by William Cravener 03/11/93
- ;;
- ;******************************************************************
- code SEGMENT
- ASSUME cs:code, ds:code, es:code
- ORG 100h
- start:
- jmp filenames
- ;*******************************************************************
- FIELDLENGTH EQU 13
- MAXNAMES EQU 75
- sort_flag DB 0 ; flag we need
- file_count DB 0 ; file counter
- star_dot_star DB '*.*', 0 ; look for all filenames
- message DB 'ยป-Swift-Ware->', 0dh, 0ah
- DB 'Sorted directory listing'
- DB ' in alphabetical order.$'
- ;*******************************************************************
- filenames PROC NEAR
- mov ah, 6 ; BIO's scroll function
- mov al, 0 ; scroll entire screen
- mov bh, 7 ; attribute
- mov cx, 0 ; upper left
- mov dx, 184fh ; lower right
- int 10h ; interrupt
- call set_dta ; set up disk transfer area
- call get_first ; get first filename
- jnc storename ; if found store it
- jmp no_names ; no names to get
- storename:
- lea di, filename_buffer ; point to buffer.
- mov bp, di
- add bp, FIELDLENGTH*75-13 ; point to buffer end minus 13 bytes.
- call store_filename ; store the gotten name.
- find_next:
- inc file_count ; increment count of names.
- call get_next ; get another name
- jnc gotone ; anymore names?
- jmp sort_names ; no - go sort and display
- gotone:
- call store_filename ; store filename in buffer
- cmp di, bp ; are we encroaching buffer end?
- jbe find_next ; if not go get next name
- sort_names:
- call sort ; sort filenames
- call display_names ; display filenames
- no_names:
- mov ax, 4c00h ; exit to DOS
- int 21h
- filenames ENDP
- ;;
- ;*******************************************************************
- ;; Sort routine requires one extra field of "0's" at end of buffer.
- ;; Sorts in ascending order.
- sort PROC NEAR
- cmp file_count, 2 ; at least 2 filenames?
- jb sort_return ; no - then leave
- sub di, FIELDLENGTH ; point to beginning of last
- mov bp, di ; also need it in BP
- next_pass:
- mov sort_flag, 0 ; set sort flag to "0"
- lea bx, filename_buffer ; point to filename buffer
- next_sort:
- mov si, bx ; point SI to current name field
- mov di, si
- add di, FIELDLENGTH ; point DI to next name field
- mov cx, FIELDLENGTH ; length of name field
- compare:
- repe cmpsb ; is next < or = to current?
- jbe end_sort ; no - move ahead one
- ; Change - JBE to JAE for DESENDING order.
- swap:
- mov si, bx ; yes - point SI to current
- mov di, bx ;
- add di, FIELDLENGTH ; point DI to next field
- mov cx, FIELDLENGTH ; length of field to swap
- next_swap:
- mov al, [di] ; get next field byte
- movsb ; move current to next
- mov [si-1], al ; move next to current
- loop next_swap ; do 13 byte field swap
- mov sort_flag, 1 ; say we did a field swap
- end_sort:
- add bx, FIELDLENGTH ; move ahead one
- cmp bx, bp ; are we at last?
- jb next_sort ; no - do another
- cmp sort_flag, 0 ; sort anything?
- jnz next_pass ; yes do another pass
- sort_return:
- ret ; all fields sorted!
- sort ENDP
- ;;
- ;*******************************************************************
- ;;
- store_filename PROC NEAR
- push di ; save buffer pointer
- mov si, 80h ; point to DTA
- add si, 30 ; point to filename.
- cmp WORD PTR [si], 002eh ; is it a directory
- jne is_a_filename ; no - its a filename
- ret ; yes - return
- is_a_filename:
- mov cx, FIELDLENGTH ; store filename.
- next_store:
- lodsb ; get a byte.
- cmp al, 0 ; is it end of name
- je endofit ; yes - exit
- stosb ; no - store byte.
- loop next_store ; get next byte.
- endofit:
- pop di ; restore buffer pointer
- add di, FIELDLENGTH ; point to next field offset
- ret
- store_filename ENDP
- ;;
- ;*******************************************************************
- ;;
- display_names PROC NEAR
- mov dh, 1 ; where we will -
- xor dl, dl ; place the message
- call setcursor ; set the cursor
- mov ah, 9 ; DOS print string function
- lea dx, message ; the message
- int 21h
- mov file_count, MAXNAMES ; displaying up to 75 names
- mov dh, 4 ; type names starting
- xor dl, dl ; at row 4 column 0
- push dx ; save row column
- lea si, filename_buffer ; start of filenames
- mov bp, 15 ; type out 15 names per column
- next_one:
- call setcursor ; set the cursor location
- inc dh ; move down to next row
- mov cx, FIELDLENGTH ; 13 bytes per name field
- the_loop:
- lodsb ; get a byte
- call print_a_char ; print a character
- loop the_loop ; all 13 bytes of field
- dec file_count ; shown 75 names yet?
- jz fin ; yes - all finished
- dec bp ; decrement row counter
- jz next_column ; if=0 move to next column
- cmp di, si ; was that the last name?
- jnb next_one ; no - type out next
- fin:
- pop dx ; done - balance stack
- mov dh, 20 ; set DOS prompt
- mov dl, 0 ; at row 20 column 0
- call setcursor ; set cursor
- ret
- next_column:
- pop dx ; retreive starting row column
- add dl, 16 ; move over one column
- push dx ; save row column on stack
- call setcursor ; set the cursor
- mov bp, 15 ; reset rows count
- jmp next_one ; go do another column
- display_names ENDP
- ;;
- ;*******************************************************************
- ;;
- setcursor PROC NEAR
- mov ah, 2 ; BIO's set cursor function
- mov bh, 0 ; page 0
- int 10h
- ret
- setcursor ENDP
- ;;
- ;*******************************************************************
- ;;
- print_a_char PROC NEAR
- mov ah, 0eh ; BIO's teleytype function
- mov bh, 0 ; page 0
- int 10h ; interrupt
- ret
- print_a_char ENDP
- ;;
- ;*******************************************************************
- ;;
- set_dta PROC NEAR
- mov ah, 1ah ; DTA function
- mov dx, 80h ; use programs PSP
- int 21h
- ret
- set_dta ENDP
- ;;
- ;*******************************************************************
- ;;
- get_first PROC NEAR
- mov ah, 4eh ; DOS service request number
- ; to find first match
- lea dx, star_dot_star ; find any and all filenames
- mov cx, 0 ; Normal attribute for file
- int 21h ; Carry flag set if no match
- ret
- get_first ENDP
- ;;
- ;******************************************************************
- ;;
- get_next PROC NEAR
- mov ah, 4fh ; DOS service request number
- ; for next match search
- int 21h ; Carry flag set if no match
- ret
- get_next ENDP
- ;;
- ;*******************************************************************
- ;;
- filename_buffer DB FIELDLENGTH * 75 + 13 DUP (0)
- ;; Enough fields for 75 names plus 1 extra blank field.
- ;;
- ;*******************************************************************
- ;;
- code ENDS ; end of coding
- END start
-
-