;; This program reads in three integers a, b, c, ;; sorts them, i.e., rearranges the values so that ;; a <= b <= c, then prints them out. allocate-registers a b c ;; inputs for program allocate-registers x y ;; inputs/outputs for sort2 subroutine allocate-registers zero sort2 cont ;; constants allocate-registers test temp ;; temporaries li zero 0 li sort2 Lsort2 ;; first read in the three numbers read a read b read c ;; sort a b by calling sort2(a, b) add x a zero add y b zero li cont Lafter-first j sort2 Lafter-first: add a x zero add b y zero ;; sort b c by calling sort2(b, c) add x b zero add y c zero li cont Lafter-second j sort2 Lafter-second: add b x zero add c y zero ;; sort a b by calling sort2(a, b) add x a zero add y b zero li cont Lafter-third j sort2 Lafter-third: add a x zero add b y zero ;; write out a, b, c then quit write a write b write c halt ;; This routine sorts 2 numbers in x and y ;; and makes x <= y ;; ;; Precondition: ;; two numbers in registers x and y ;; register cont contains the return address ;; ;; Postcondition: ;; x and y still have the same two input numbers, ;; except that x (y) contains the smaller (larger) value. ;; ;; Registers used ;; x - input, output ;; y - input, output ;; test, temp - temporary Lsort2: sgt test x y jeqz test cont add temp x zero add x y zero add y temp zero j cont