| Index | Manual | Download | Source | Macros | Embedded word Case ins.
Sitemap | Links | Forum | Tests | Projects

# sort64.htm
ShellSort

This OS-independent macrolibrary can be included to 64bit programs written in EuroAssembler.

The library contains macroinstructions which sort data in memory.

    sort64 HEAD

ShellSort TablePtr, NrOfRecords, RecordSize, CallbackProc
  This macro can sort an array of records of the same size using the [ShellSort] algorithm. The entire array must be loaded in memory and it will be sorted in situ.
Input
  TablePtr is pointer to the 1st record of the array.
  NrOfRecords is number of records (members) in the array.
  RecordSize is the size of one record in bytes. Array size = NrOfRecords * RecordSize.
  CallbackProc is pointer to the callback procedure which sorts two records.
  CallbackProc
    is called with register calling convention.
    It is expected to compare two records and return CF=0 if they are in proper order.
    Otherwise it must swap the contents of these records and return CF=1.
  Input
    RSI is pointer to the first record to compare,
    RDI is pointer to the second record to compare.
    RDX contains the remaining number of records (%ShellSortNrOfRecords), which can be used to show the progress of sorting.
  Output
    CF=0 if both records were in desired order, or
    CF=1 if both records were in wrong order, but they were swapped now.
    RAX,RDX,RSI,RDI,R8..R11 may be changed. Registers RBX,RCX,RBP must be preserved by CallbackProc.
Output
  Registers are preserved.
Example
  MyTable DQ 5,1,7,6,3,4,2 ; Unsorted QWORD data. ShellSort MyTable, SIZE# MyTable/8, 8, MyCmpProc MyCmpProc PROC1 MOV RAX,[RSI] MOV RDX,[RDI] CMP RDX,RAX JNC .Done: ; Jump if both records are in the desired order. MOV [RSI],RDX ; Otherwise swap them and return CF. MOV [RDI],RAX .Done: RET ENDP1 MyCmpProc
Tested by
  tmac64.htm

    ShellSort %MACRO TablePtr,NrOfRecords,RecordSize,CallbackProc
         PUSHQ %CallbackProc,%RecordSize,%NrOfRecords,%NrOfRecords,%TablePtr
         CALL ShellSort64@RT::
    ShellSort64@RT:: PROC1
          PUSH RAX,RCX,RDX,RBX,RBP,RSI,RDI,R8,R9,R10,R11
          MOV RBP,RSP
    %ShellSortTablePtr     %SET RBP+12*8
    %ShellSortNrOfRecords  %SET RBP+13*8
    %ShellSortRecordNr     %SET RBP+14*8
    %ShellSortRecordSize   %SET RBP+15*8
    %ShellSortCallbackProc %SET RBP+16*8
     .10: SARQ [%ShellSortNrOfRecords],1
          JNG .90:
          MOV RCX,[%ShellSortRecordNr]
          SUB RCX,[%ShellSortNrOfRecords]
          MOV EBX,1
     .20: MOV RSI,RBX
     .30: MOV RDI,RSI
          ADD RDI,[%ShellSortNrOfRecords]
          PUSH RSI,RDI
           MOV RAX,RSI
           DEC RAX
           MULQ [%ShellSortRecordSize]
           ADD RAX,[%ShellSortTablePtr]
           MOV RSI,RAX
           MOV RAX,RDI
           DEC RAX
           MULQ [%ShellSortRecordSize]
           ADD RAX,[%ShellSortTablePtr]
           MOV RDI,RAX
           MOV RDX,[%ShellSortNrOfRecords]
           CALL [%ShellSortCallbackProc]
          POP RDI,RSI
          JNC .40:
          SUB RSI,[%ShellSortNrOfRecords]
          JA .30:
     .40: INC EBX
          CMP EBX,ECX
          JNA .20:
          JMP .10:
     .90: POP R11,R10,R9,R8,RDI,RSI,RBP,RBX,RDX,RCX,RAX
          RET 5*8
        ENDPROC1 ShellSort64@RT::
     %ENDMACRO ShellSort

       ENDHEAD sort64

▲Back to the top▲