/** * Demonstration ULNAv2-A program. * * Copyright(c) 2019-2020 Jason Tang * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program bubble sorts an array of 4 integres, from largest to * smallest. It sorts in-place, starting from memory address 0x10. */ movi R0, #0x10 // starting address for array movi R1, #0x20 movi R2, #0x30 movi R3, #0x10 movi R4, #0x40 stwi R1, R0, #0 // A[0] = 0x20 stwi R2, R0, #1 // A[1] = 0x30 stwi R3, R0, #2 // A[2] = 0x10 stwi R4, R0, #3 // A[3] = 0x40 bl R7, bsort halt // start of bubble sort bsort: movi R1, #0 // i = outer index = 0 // outer loop top_of_outer: cmpi. R1, #3 // while (i < 3) { b.lt .1 br r7 .1: movi R2, #0 // j = inner index = 0 negi. R3, R1, #4 // k = 3 - i // inner loop top_of_inner: cmp. R2, R3 // while (j < k) { b.ge end_of_inner ldw R4, R0, R2 // a = A[j] addi. R2, R2, #1 // j++ ldw R5, R0, R2 // b = A[j] cmp. R4, R5 // if (a < b) { b.ge top_of_inner // swap values stw R4, R0, R2 // A[j] = a addi. R6, R2, -1 stw R5, R0, R6 // A[j - 1] = b // after swap } b top_of_inner // } // end of inner loop end_of_inner: addi. R1, R1, 1 // i++ b top_of_outer // }