Segment Sorter

Solution

Solution

@0

@1
MOV UP DOWN

@2

@3
REV: MOV DOWN ACC
 MOV ACC LEFT
 JGZ REV
 MOV LEFT NIL
OUT: MOV LEFT ACC
 MOV ACC DOWN
 JGZ OUT
 
@4
GET: MOV RIGHT ACC
MOV NIL DOWN
 SAV
 SUB DOWN
 JGZ INS
 MOV DOWN RIGHT
 JMP GET
 INS: SWP
 MOV ACC RIGHT
 MOV RIGHT NIL
 MOV DOWN ACC
 MOV ACC RIGHT
 JEZ GET
 MOV RIGHT NIL
 MOV 0 RIGHT
 
@5
IN: MOV UP ACC
 JEZ OUT
 SAV
FWD: SWP
 MOV ACC LEFT
 SAV
 MOV LEFT ACC
 MOV ACC RIGHT
 JGZ FWD
REV: MOV RIGHT ACC
JEZ IN
 MOV ACC DOWN
 JMP REV
OUT: MOV -1 RIGHT
 MOV RIGHT NIL
 
@6
RST: MOV 0 UP
IN: MOV LEFT ACC
 JGZ FWD
 JLZ OUT
REV: MOV UP ACC
 MOV ACC LEFT
 JGZ REV
 JMP RST
FWD: MOV ACC UP
 JMP IN
OUT: MOV ACC DOWN
 MOV DOWN LEFT
 
@7
REV: MOV DOWN ACC
 MOV ACC UP
 JGZ REV
OUT: MOV UP ACC
 MOV ACC DOWN
 JGZ OUT
 
@8
RST: MOV 0 RIGHT
LOOP: MOV UP NIL
 MOV RIGHT ACC
 MOV ACC UP
 MOV ACC UP
 JGZ LOOP
 
@9

@10
 MOV UP NIL
REV: MOV LEFT ACC
 MOV ACC RIGHT
 JGZ REV
OUT: MOV RIGHT ACC
 MOV ACC DOWN
 JGZ OUT
 MOV 0 LEFT
 MOV NIL UP
 
@11
REV: MOV LEFT ACC
 MOV ACC UP
 JGZ REV
OUT: MOV UP ACC
 MOV ACC LEFT
 JGZ OUT