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

# sort16.htm
ShellSort

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

The library contains macroinstructions which sort data in memory.

    sort16 HEAD

ShellSort TablePtr, NrOfRecords, RecordSize, CallbackProc
  This macro can sort a table of records of the same size using the [ShellSort] algorithm. The entire table must be loaded in memory and it will be sorted in situ.
Input
  DS is segment which containts the Table.
  CS is segment which contains the CallbackProc.
  TablePtr is pointer to the 1st record.
  NrOfRecords is number of records in the table.
  RecordSize is the size of one record in bytes. Table 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
    DS:SI is pointer to the first record to compare,
    DS:DI is pointer to the second record to compare.
  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.
    BX,CX,BP,DS must be preserved by CallbackProc. Other GPR may be destroyed.
Output
  None, all registers are preserved.
Example
  MyTable DW 5,1,7,6,3,4,2 ; Unsorted WORD data. ShellSort MyTable, SIZE# MyTable/2, 2, MyCmpProc MyCmpProc PROC1 MOV AX,[SI] MOV DX,[DI] CMP DX,AX JNC .Done: ; Jump if both records are in order. MOV [SI],DX ; Otherwise swap them and return CF. MOV [DI],AX .Done: RET ENDP1 MyCmpProc
Tested by
  tmac16.htm

    ShellSort %MACRO TablePtr,NrOfRecords,RecordSize,CallbackProc
         PUSHW %CallbackProc,%RecordSize,%NrOfRecords,%NrOfRecords,%TablePtr
         CALL ShellSort16@RT::
    ShellSort16@RT:: PROC1
          PUSHAW
          MOV BP,SP
    %ShellSortTablePtr     %SET BP+18
    %ShellSortNrOfRecords  %SET BP+20
    %ShellSortRecordNr     %SET BP+22
    %ShellSortRecordSize   %SET BP+24
    %ShellSortCallbackProc %SET BP+26
     .10: SARW [%ShellSortNrOfRecords],1
          JNG .90:
          MOV CX,[%ShellSortRecordNr]
          SUB CX,[%ShellSortNrOfRecords]
          MOV BX,1
     .20: MOV SI,BX
     .30: MOV DI,SI
          ADD DI,[%ShellSortNrOfRecords]
          PUSH SI,DI
           MOV AX,SI
           DEC AX
           MULW [%ShellSortRecordSize]
           ADD AX,[%ShellSortTablePtr]
           MOV SI,AX
           MOV AX,DI
           DEC AX
           MULW [%ShellSortRecordSize]
           ADD AX,[%ShellSortTablePtr]
           MOV DI,AX
           CALL [%ShellSortCallbackProc]
          POP DI,SI
          JNC .40:
          SUB SI,[%ShellSortNrOfRecords]
          JA .30:
     .40: INC BX
          CMP BX,CX
          JNA .20:
          JMP .10:
     .90: POPAW
          RET 5*2
        ENDPROC1 ShellSort16@RT::
     %ENDMACRO ShellSort

       ENDHEAD sort16

▲Back to the top▲