home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / SORT.ZIP / SORT.ASM
Encoding:
Assembly Source File  |  1986-10-11  |  5.0 KB  |  102 lines

  1. ;This routine is an external procedure that can be called from a Turbo Pascal
  2. ;program! 
  3. ;
  4. ;--------------------------------------------------------------------------*
  5. ;  The following are offsets in the stack for the input parameters:        |
  6. ;  The invoking statement is:                                              |
  7. ;                                                                          |
  8. ;    Sort(var Table; NumRec, FStart, Size: integer);                       |
  9. ;--------------------------------------------------------------------------*
  10. FSize   equ     4               ; Size of field to sort
  11. FStart  equ     FSize+2         ; Start position within record
  12. NumRec  equ     FStart+2        ; Number of records to sort
  13. Table   equ     NumRec+2        ; Start column of field
  14.  
  15. ;--------------------------------------------------------------------------*
  16. ; Initialize registers for program to use                                  |
  17. ;--------------------------------------------------------------------------*
  18. START:
  19.         PUSH    BP              ; save base page register for Turbo
  20.         MOV     BP,SP           ; set base pointer equal to stack pointer
  21.         PUSH    DS              ; Save data segment register
  22.         MOV     AX,NUMREC[BP]   ; Get number of records to sort (GAP)
  23. LP1:
  24.         SHR     AX,1            ; GAP := GAP DIV 2
  25.         OR      AX,AX           ; Check for end of sort (GAP = 0)
  26.         JZ      DONE            ; Exit from module if done!
  27.         MOV     BX,AX           ; BX = NextEl := GAP
  28. LP2:
  29.         INC     BX              ; BX = GAP := GAP + 1
  30.         CMP     BX,NUMREC[BP]   ; Check for end of first loop...
  31.         JA      LP1             ; Exit if NextEl > NumRec
  32.         MOV     DX,BX           ; DX = SortEl := NextEl - Gap
  33. LP3:
  34.         SUB     DX,AX
  35.         JBE     LP2             ; Exit if SortEl <= 0
  36.         PUSH    BX              ; Save NextEl
  37.         PUSH    DS              ; Save data segment ptr
  38.         LDS     BX,TABLE[BP]    ; Get address of pointer table
  39.         MOV     DI,DX           ; Get SortEl
  40.         DEC     DI              ; Back up one (table entries start at "1")
  41.         ADD     DI,AX           ; SortEl + Gap
  42.         SHL     DI,1            ; Calculate offset within table
  43.         SHL     DI,1            ;
  44.         ADD     DI,BX           ; Add in starting address of table
  45.         PUSH    DI              ; Save that offset!
  46.         LES     DI,DS:[DI]      ; Get address of Table[SortEl+Gap]
  47.         ADD     DI,FSTART[BP]   ; Add in offset from start of record
  48.         MOV     SI,DX           ; Get SortEl
  49.         DEC     SI              ; Back up one (table entries start at "1")
  50.         SHL     SI,1            ; Calculate offset within table
  51.         SHL     SI,1            ;
  52.         ADD     SI,BX           ; Add in starting address of table
  53.         PUSH    SI              ; Save that offset!
  54.         LDS     SI,DS:[SI]      ; Get address of Table[SortEl]
  55.         ADD     SI,FSTART[BP]   ; Add in offset from start of record
  56.         MOV     CX,FSIZE[BP]    ; Get size of field to test
  57.  
  58. ; Jim,  I think this is a quicker way to do a string comparison
  59. ;   Let the machine do the looping!!  Work Smarter not Harder!
  60.  
  61.         REPE    CMPSB           ; Compare Table[SortEl] w/ Table[SortEl+Gap]
  62.                                 ; Repeat compare as long as they are the same
  63.                                 ; And CX is non zero.
  64.  
  65.         JBE     OK              ; If Table[SortEl+Gap] < Table[SortEl] then
  66.                                 ; skip the swap portion.
  67.  
  68. ; All this just to do that! Jim, Jim, Jim... Arrr Arrr Arr....
  69. ;LP4:
  70. ;        CMPSB                   ; Compare Table[SortEl] w/ Table[SortEl+Gap]
  71. ;        JB      OK              ; Table[SortEl] < Table[SortEl+Gap]
  72. ;        JNE     SWAP            ; Table[SortEl] > Table[SortEl+Gap]
  73. ;        LOOP    LP4             ; Table[SortEl] = Table[SortEl+Gap]
  74. ;        JMP     OK              ;
  75.  
  76.  
  77. SWAP:
  78.         POP     SI              ; Restore SortEl ptr
  79.         POP     DI              ; Restore SortEl+Gap ptr
  80.         POP     DS              ; Restore Data segment register
  81.         MOV     BX,DS:[SI]      ; Swap segment values
  82.         XCHG    BX,DS:[DI]      ;
  83.         MOV     DS:[SI],BX      ;
  84.         ADD     SI,2            ; Increment to offset values
  85.         ADD     DI,2            ;
  86.         MOV     BX,DS:[SI]      ; Swap offset values
  87.         XCHG    BX,DS:[DI]      ;
  88.         MOV     DS:[SI],BX      ;
  89.         POP     BX              ; Restore Gap value
  90.         JMP     LP3             ; repeat until sorted
  91. OK:
  92.         POP     SI
  93.         POP     DI
  94.         POP     DS
  95.         POP     BX
  96.         JMP     LP2
  97. DONE:
  98.         POP     DS              ; Restore data segment value
  99.         MOV     SP,BP           ; reset stack pointer
  100.         POP     BP              ; reset base pointer
  101.         RET     10              ; Exit from module & clean up stack!
  102.