home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / directry / sort / dirsort.asm next >
Encoding:
Assembly Source File  |  1993-03-13  |  10.1 KB  |  226 lines

  1. ;; DIRSORT.ASM - simple program to get all filenames in the current directory
  2. ;;               sort all in asending or desending order and display them.
  3. ;;
  4. ;; This program can be assembled using the A86 or TASM assemblers 
  5. ;;
  6. ;; Not tested with Masm, should work.
  7. ;;
  8. ;; This code is "PUBLIC DOMAIN"
  9. ;;
  10. ;; by William Cravener 03/11/93
  11. ;;
  12. ;******************************************************************
  13. code                    SEGMENT
  14. ASSUME          cs:code, ds:code, es:code
  15. ORG             100h
  16. start:
  17.         jmp     filenames
  18. ;*******************************************************************
  19. FIELDLENGTH             EQU     13
  20. MAXNAMES                EQU     75
  21. sort_flag               DB      0        ; flag we need
  22. file_count              DB      0        ; file counter
  23. star_dot_star           DB      '*.*', 0 ; look for all filenames
  24. message                 DB      'ยป-Swift-Ware->', 0dh, 0ah
  25.                         DB      'Sorted directory listing'
  26.                         DB      ' in alphabetical order.$'
  27. ;*******************************************************************
  28. filenames               PROC    NEAR
  29.         mov     ah, 6                   ; BIO's scroll function
  30.         mov     al, 0                   ; scroll entire screen
  31.         mov     bh, 7                   ; attribute 
  32.         mov     cx, 0                   ; upper left
  33.         mov     dx, 184fh               ; lower right
  34.         int     10h                     ; interrupt
  35.         call    set_dta                 ; set up disk transfer area       
  36.         call    get_first               ; get first filename 
  37.         jnc     storename               ; if found store it
  38.         jmp     no_names                ; no names to get
  39. storename: 
  40.         lea     di, filename_buffer     ; point to buffer.
  41.         mov     bp, di                  
  42.         add     bp, FIELDLENGTH*75-13   ; point to buffer end minus 13 bytes.
  43.         call    store_filename          ; store the gotten name.
  44. find_next:
  45.         inc     file_count              ; increment count of names.
  46.         call    get_next                ; get another name
  47.         jnc     gotone                  ; anymore names?
  48.         jmp     sort_names              ; no - go sort and display
  49. gotone:
  50.         call    store_filename          ; store filename in buffer
  51.         cmp     di, bp                  ; are we encroaching buffer end?
  52.         jbe     find_next               ; if not go get next name
  53. sort_names:
  54.         call    sort                    ; sort filenames
  55.         call    display_names           ; display filenames
  56. no_names:
  57.         mov     ax, 4c00h               ; exit to DOS
  58.         int     21h
  59. filenames               ENDP
  60. ;;
  61. ;*******************************************************************
  62. ;; Sort routine requires one extra field of "0's" at end of buffer.
  63. ;; Sorts in ascending order.
  64. sort                    PROC    NEAR
  65.         cmp     file_count, 2           ; at least 2 filenames?
  66.         jb      sort_return             ; no - then leave
  67.         sub     di, FIELDLENGTH         ; point to beginning of last 
  68.         mov     bp, di                  ; also need it in BP
  69. next_pass:
  70.         mov     sort_flag, 0            ; set sort flag to "0"
  71.         lea     bx, filename_buffer     ; point to filename buffer
  72. next_sort:
  73.         mov     si, bx                  ; point SI to current name field
  74.         mov     di, si
  75.         add     di, FIELDLENGTH         ; point DI to next name field
  76.         mov     cx, FIELDLENGTH         ; length of name field
  77. compare:
  78.         repe    cmpsb                   ; is next < or = to current?
  79.         jbe     end_sort                ; no - move ahead one
  80. ; Change - JBE to JAE for DESENDING order.
  81. swap:
  82.         mov     si, bx                  ; yes - point SI to current
  83.         mov     di, bx                  ; 
  84.         add     di, FIELDLENGTH         ; point DI to next field
  85.         mov     cx, FIELDLENGTH         ; length of field to swap
  86. next_swap:
  87.         mov     al, [di]                ; get next field byte
  88.         movsb                           ; move current to next
  89.         mov     [si-1], al              ; move next to current
  90.         loop    next_swap               ; do 13 byte field swap
  91.         mov     sort_flag, 1            ; say we did a field swap
  92. end_sort:
  93.         add     bx, FIELDLENGTH         ; move ahead one
  94.         cmp     bx, bp                  ; are we at last?
  95.         jb      next_sort               ; no - do another
  96.         cmp     sort_flag, 0            ; sort anything?
  97.         jnz     next_pass               ; yes do another pass
  98. sort_return:
  99.         ret                             ; all fields sorted!
  100. sort                    ENDP
  101. ;;
  102. ;*******************************************************************
  103. ;;
  104. store_filename          PROC    NEAR
  105.         push    di                      ; save buffer pointer
  106.         mov     si, 80h                 ; point to DTA
  107.         add     si, 30                  ; point to filename.
  108.         cmp     WORD PTR [si], 002eh    ; is it a directory
  109.         jne     is_a_filename           ; no - its a filename
  110.         ret                             ; yes - return
  111. is_a_filename:
  112.         mov     cx, FIELDLENGTH         ; store filename.
  113. next_store:
  114.         lodsb                           ; get a byte.
  115.         cmp     al, 0                   ; is it end of name
  116.         je      endofit                 ; yes - exit
  117.         stosb                           ; no - store byte.
  118.         loop    next_store              ; get next byte.
  119. endofit:
  120.         pop     di                      ; restore buffer pointer
  121.         add     di, FIELDLENGTH         ; point to next field offset
  122.         ret
  123. store_filename          ENDP
  124. ;;
  125. ;*******************************************************************
  126. ;;
  127. display_names           PROC    NEAR
  128.         mov     dh, 1                   ; where we will -
  129.         xor     dl, dl                  ; place the message
  130.         call    setcursor               ; set the cursor
  131.         mov     ah, 9                   ; DOS print string function
  132.         lea     dx, message             ; the message
  133.         int     21h
  134.         mov     file_count, MAXNAMES    ; displaying up to 75 names
  135.         mov     dh, 4                   ; type names starting
  136.         xor     dl, dl                  ; at row 4 column 0
  137.         push    dx                      ; save row column
  138.         lea     si, filename_buffer     ; start of filenames
  139.         mov     bp, 15                  ; type out 15 names per column
  140. next_one:
  141.         call    setcursor               ; set the cursor location
  142.         inc     dh                      ; move down to next row
  143.         mov     cx, FIELDLENGTH         ; 13 bytes per name field
  144. the_loop:
  145.         lodsb                           ; get a byte
  146.         call    print_a_char            ; print a character
  147.         loop    the_loop                ; all 13 bytes of field
  148.         dec     file_count              ; shown 75 names yet?
  149.         jz      fin                     ; yes - all finished
  150.         dec     bp                      ; decrement row counter
  151.         jz      next_column             ; if=0 move to next column
  152.         cmp     di, si                  ; was that the last name? 
  153.         jnb     next_one                ; no - type out next
  154. fin:
  155.         pop     dx                      ; done - balance stack
  156.         mov     dh, 20                  ; set DOS prompt
  157.         mov     dl, 0                   ; at row 20 column 0
  158.         call    setcursor               ; set cursor
  159.         ret
  160. next_column:
  161.         pop     dx                      ; retreive starting row column
  162.         add     dl, 16                  ; move over one column
  163.         push    dx                      ; save row column on stack
  164.         call    setcursor               ; set the cursor
  165.         mov     bp, 15                  ; reset rows count
  166.         jmp     next_one                ; go do another column
  167. display_names           ENDP
  168. ;;
  169. ;*******************************************************************
  170. ;;
  171. setcursor               PROC    NEAR
  172.         mov     ah, 2                   ; BIO's set cursor function
  173.         mov     bh, 0                   ; page 0
  174.         int     10h
  175.         ret
  176. setcursor               ENDP
  177. ;;
  178. ;*******************************************************************
  179. ;;
  180. print_a_char            PROC    NEAR
  181.         mov     ah, 0eh                 ; BIO's teleytype function
  182.         mov     bh, 0                   ; page 0
  183.         int     10h                     ; interrupt
  184.         ret
  185. print_a_char            ENDP
  186. ;;
  187. ;*******************************************************************
  188. ;;
  189. set_dta                 PROC    NEAR
  190.         mov     ah, 1ah                 ; DTA function
  191.         mov     dx, 80h                 ; use programs PSP
  192.         int     21h
  193.         ret
  194. set_dta                 ENDP
  195. ;;
  196. ;*******************************************************************
  197. ;;
  198. get_first               PROC    NEAR
  199.         mov     ah, 4eh                 ; DOS service request number
  200.                                         ; to find first match
  201.         lea     dx, star_dot_star       ; find any and all filenames
  202.         mov     cx, 0                   ; Normal attribute for file
  203.         int     21h                     ; Carry flag set if no match
  204.         ret
  205. get_first               ENDP
  206. ;;
  207. ;******************************************************************
  208. ;;
  209. get_next                PROC    NEAR
  210.         mov     ah, 4fh                 ; DOS service request number
  211.                                         ; for next match search
  212.         int     21h                     ; Carry flag set if no match
  213.         ret
  214. get_next                ENDP
  215. ;;
  216. ;*******************************************************************
  217. ;;
  218. filename_buffer         DB      FIELDLENGTH * 75 + 13 DUP (0)
  219. ;;               Enough fields for 75 names plus 1 extra blank field.
  220. ;;
  221. ;*******************************************************************
  222. ;;
  223. code                    ENDS            ; end of coding
  224. END             start
  225.  
  226.