EuroAssembler Index Manual Download Source Macros


Sitemap Links Forum Tests Projects

Sort32.htm
ShellSort

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

The library contains macroinstructions which sort data in memory.


sort32 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
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
ESI is pointer to the first record to compare,
EDI 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.
EBX,ECX,EBP must be preserved by CallbackProc. Other GPR may be destroyed.
Output
None, all registers are preserved.
Example
MyTable DD 5,1,7,6,3,4,2 ; Unsorted DWORD data. ShellSort MyTable, SIZE# MyTable/4, 4, MyCmpProc MyCmpProc PROC1 MOV EAX,[ESI] MOV EDX,[EDI] CMP EDX,EAX JNC .Done: ; Jump if both records are in order. MOV [ESI],EDX ; Otherwise swap them and return CF. MOV [EDI],EAX .Done: RET ENDP1 MyCmpProc
Tested by
tmac32.htm
ShellSort %MACRO TablePtr,NrOfRecords,RecordSize,CallbackProc
     PUSHD %CallbackProc,%RecordSize,%NrOfRecords,%NrOfRecords,%TablePtr
     CALL ShellSort32@RT::
ShellSort32@RT:: PROC1
      PUSHAD
      MOV EBP,ESP
%ShellSortTablePtr     %SET EBP+36     
%ShellSortNrOfRecords  %SET EBP+40   
%ShellSortRecordNr     %SET EBP+44    
%ShellSortRecordSize   %SET EBP+48
%ShellSortCallbackProc %SET EBP+52
 .10: SARD [%ShellSortNrOfRecords],1
      JNG .90:
      MOV ECX,[%ShellSortRecordNr]
      SUB ECX,[%ShellSortNrOfRecords]
      MOV EBX,1
 .20: MOV ESI,EBX
 .30: MOV EDI,ESI
      ADD EDI,[%ShellSortNrOfRecords]
      PUSH ESI,EDI
       MOV EAX,ESI
       DEC EAX
       MULD [%ShellSortRecordSize]
       ADD EAX,[%ShellSortTablePtr]
       MOV ESI,EAX
       MOV EAX,EDI
       DEC EAX
       MULD [%ShellSortRecordSize]
       ADD EAX,[%ShellSortTablePtr]
       MOV EDI,EAX
       CALL [%ShellSortCallbackProc]
      POP EDI,ESI
      JNC .40:
      SUB ESI,[%ShellSortNrOfRecords]
      JA .30:
 .40: INC EBX
      CMP EBX,ECX
      JNA .20:
      JMP .10:
 .90: POPAD
      RET 5*4
    ENDPROC1 ShellSort32@RT::
 %ENDMACRO ShellSort
   ENDHEAD sort32

▲Back to the top▲