First/programs/quicksort.asm

201 lines
2.1 KiB
NASM

MOV $0xFF0000,%SP
MOV $0x0000,%R0h
MOV $0x0000,%R0l
PUSH %R0
MOV $0x0005,%R0l
PUSH %R0
CALL $QUICKSORT
HALT
#Doing Quicksort from DATA+%R3 to DATA+%R2
:QUICKSORT
#Get parameters from stack
MOV %SP,%R0
MOV $0x0000,%R1h
MOV $0x0004,%R1l
SUB %R1,%R0
SUB %R1,%R0
MOV (%R0),%R2
SUB %R1,%R0
MOV (%R0),%R3
#Compare input and quit if start >= end
CMP %R2,%R3
JMP,NS $BAIL
#Get pivot to %R5
MOV $<DATA,%R7h
MOV $>DATA,%R7l
MOV %R3,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R5
#Set %R0 and %R1
MOV %R3,%R0
DEC %R0
MOV %R3,%R1
#MAIN LOOP
:MAIN_LOOP
#Get current data being pointed to %R4
MOV %R1,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R4
#Check if we swap
CMP %R5,%R4
JMP,NS $COND_EXIT
INC %R0
PUSH %R2
#Do swap
MOV %R1,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R2
MOV %R0,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R4
MOV %R2,(%R6)
MOV %R1,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV %R4,(%R6)
POP %R2
:COND_EXIT
INC %R1
CMP %R2,%R1
JMP,NZ $MAIN_LOOP
INC %R0
#Do swap
PUSH %R2
MOV %R2,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R2
MOV %R0,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV (%R6),%R4
MOV %R2,(%R6)
MOV %R0,%R6
SL %R6
SL %R6
ADD %R7,%R6
MOV %R4,(%R6)
POP %R2
DEC %R0
#SAVE REGISTERS
PUSH %R0
PUSH %R1
PUSH %R2
PUSH %R3
PUSH %R4
#PASS PARAMETERS
PUSH %R2
PUSH %R0
CALL $QUICKSORT
#SKIP PARAMETERS
POP %R4
POP %R4
#RESTORE REGISTERS
POP %R4
POP %R3
POP %R2
POP %R1
POP %R0
#SAVE REGISTERS
PUSH %R0
PUSH %R1
PUSH %R2
PUSH %R3
PUSH %R4
#PASS PARAMETERS
INC %R0
INC %R0
PUSH %R0
PUSH %R3
CALL $QUICKSORT
#SKIP PARAMETERS
POP %R4
POP %R4
#RESTORE REGISTERS
POP %R4
POP %R3
POP %R2
POP %R1
POP %R0
:BAIL
RET
:DATA
DDW $0x00000010
DDW $0x00000007
DDW $0x00000008
DDW $0x00000009
DDW $0x00000001
DDW $0x00000005
#DDW $0x00000006
#DDW $0x00000007
#DDW $0x00000008
#DDW $0x00000009
#DDW $0x0000000a
#DDW $0x0000000b
#DDW $0x0000000c
#DDW $0x0000000d
#DDW $0x0000000e
#DDW $0x00000010
#DDW $0xfe4cb7ad
#DDW $0xb925a7a6
#DDW $0xce117350
#DDW $0xd587026a
#DDW $0xc341474a
#DDW $0x709182e0
#DDW $0x3b3d9cb8
#DDW $0x9b36108d
#DDW $0x069a847f
#DDW $0x0137e24c
#DDW $0xe848d5dc
#DDW $0xf98a1bc4
#DDW $0xe019a8e4
#DDW $0xd42640c3
#DDW $0x9dba659a
#DDW $0xdd94f7b9