; Quick-Sort-Implementation in Assembler

.MODEL SMALL,C
.CODE

public qsort_asm

qsort_asm PROC LAENGE:WORD, FELD_ADR:PTR NEAR
          MOV  AX,0	      ; Linkes Element
          SHL  AX,1	      ; Multiplikation mit 2, da Wortelement
          PUSH AX	      ; Auf dem Stack ｜ergeben
          MOV  AX,LAENGE      ; Rechtes Element
          DEC  AX
          SHL  AX,1
          PUSH AX
          MOV  BX,FELD_ADR
          CALL SORT
          RET
qsort_asm ENDP

; Hier beginnt die QuickSort-Routine
SORT PROC RIGHT:WORD, LEFT:WORD
          MOV  AX,RIGHT
          CMP  AX,LEFT
          JLE  QS1
          MOV  DI,LEFT
          SUB  DI,2
          MOV  SI,AX
QS2:
          PUSH SI
          MOV  SI,RIGHT
          MOV  AX,[BX][SI]
          POP  SI
QS2A:
          ADD  DI,2
          CMP  [BX][DI],AX
          JL   QS2A
QS4:
          SUB  SI,2
          CMP  [BX][SI],AX
          JLE  QS3
          CMP  SI,0
          JG   QS4
QS3:
          MOV  DX,[BX][DI]
          MOV  AX,[BX][SI]
          MOV  [BX][DI],AX
          MOV  [BX][SI],DX
          CMP  SI,DI
          JG   QS2
          MOV  AX,[BX][DI]
          MOV  [BX][SI],AX
          PUSH SI
          MOV  SI,RIGHT
          MOV  AX,[BX][SI]
          MOV  [BX][DI],AX
          MOV  [BX][SI],DX
          POP  SI
          PUSH DI
          MOV  AX,LEFT
          PUSH AX
          SUB  DI,2
          PUSH DI
          CALL SORT
          POP  DI
          ADD  DI,2
          PUSH DI
          MOV  AX,RIGHT
          PUSH AX
          CALL SORT
QS1:
          POP  BP
          RETN 4
SORT ENDP
END