RosettaCodeData/Task/Sort-three-variables/Little-Man-Computer/sort-three-variables.lmc

61 lines
1.1 KiB
Plaintext

// Little Man Computer
// Sort x, y, z, in ascending order
// Based on a sorting network:
// if x > z then swap( x, z)
// if x > y then swap( x, y)
// if y > z then swap( y, z)
// with the addition that if the 2nd swap is executed
// then the 3rd comparison is not needed.
// Read numbers x, y, z, and display in their original order
INP
STA x
OUT
INP
STA y
OUT
INP
STA z
OUT
// Sort so that x <= y <= z, and display in new order
LDA z
SUB x
BRP label1
LDA x
STA t
LDA z
STA x
LDA t
STA z
label1 LDA y
SUB x
BRP label2
LDA x
STA t
LDA y
STA x
LDA t
STA y
BRA sorted // added to the sorting network
label2 LDA z
SUB y
BRP sorted
LDA y
STA t
LDA z
STA y
LDA t
STA z
sorted LDA x
OUT
LDA y
OUT
LDA z
OUT
HLT
x DAT
y DAT
z DAT
t DAT
// end