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 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
- RSI is pointer to the first record to compare,
RDI 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.
RAX,RDX,RSI,RDI may be changed by CallbackProc. Other GPR must be preserved.
- Output
- None, all 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
MOV RBP,RSP
%ShellSortTablePtr %SET RBP+64
%ShellSortNrOfRecords %SET RBP+72
%ShellSortRecordNr %SET RBP+80
%ShellSortRecordSize %SET RBP+88
%ShellSortCallbackProc %SET RBP+96
.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
CALL [%ShellSortCallbackProc]
POP RDI,RSI
JNC .40:
SUB RSI,[%ShellSortNrOfRecords]
JA .30:
.40: INC EBX
CMP EBX,ECX
JNA .20:
JMP .10:
.90: POP RDI,RSI,RBP,RBX,RDX,RCX,RAX
RET 5*8
ENDPROC1 ShellSort64@RT::
%ENDMACRO ShellSort
ENDHEAD sort64
▲Back to the top▲