Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
asm_sort.s@2:968db132481b, 2020-01-04 (annotated)
- Committer:
- samvilm
- Date:
- Sat Jan 04 12:34:04 2020 +0000
- Revision:
- 2:968db132481b
Sorts array of numbers in assembly language. Also sorts same array in C to compare and ensure same sorted array is returned.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| samvilm | 2:968db132481b | 1 | AREA asm_func, CODE, READONLY |
| samvilm | 2:968db132481b | 2 | EXPORT asm_sort |
| samvilm | 2:968db132481b | 3 | asm_sort |
| samvilm | 2:968db132481b | 4 | PUSH {R2, R3, R4, R5, LR} ; Some code goes here |
| samvilm | 2:968db132481b | 5 | outer_loop |
| samvilm | 2:968db132481b | 6 | LDR R2, =0x0 ;first index |
| samvilm | 2:968db132481b | 7 | LDR R3, =0x1 ;second index |
| samvilm | 2:968db132481b | 8 | SUB R1, R1, #1 ;size of array intervaled minus one |
| samvilm | 2:968db132481b | 9 | CMP R1, #1 ;make sure interval has at least two elements |
| samvilm | 2:968db132481b | 10 | BLT done |
| samvilm | 2:968db132481b | 11 | inner_loop |
| samvilm | 2:968db132481b | 12 | CMP R3, R1 ;compare interval |
| samvilm | 2:968db132481b | 13 | BGT loop_exit ;make sure to restart loop if at the end of first pass |
| samvilm | 2:968db132481b | 14 | LDR R4, [R0, R2, LSL #2] ;numbers[first] into R4 |
| samvilm | 2:968db132481b | 15 | LDR R5, [R0, R3, LSL #2] ;number[second] into R5 |
| samvilm | 2:968db132481b | 16 | CMP R4, R5 ;compare numbers |
| samvilm | 2:968db132481b | 17 | BGT asm_swap ;if out of order swap |
| samvilm | 2:968db132481b | 18 | ADD R2, R2, #1 ;increment index R2 |
| samvilm | 2:968db132481b | 19 | ADD R3, R3, #1 ;increment index R3 |
| samvilm | 2:968db132481b | 20 | B inner_loop ; go back to keep swapping |
| samvilm | 2:968db132481b | 21 | asm_swap |
| samvilm | 2:968db132481b | 22 | STR R4, [R0, R3, LSL #2] ;swap numbers in the array |
| samvilm | 2:968db132481b | 23 | STR R5, [R0, R2, LSL #2] |
| samvilm | 2:968db132481b | 24 | ADD R2, R2, #1 ;increment index |
| samvilm | 2:968db132481b | 25 | ADD R3, R3, #1;increment index |
| samvilm | 2:968db132481b | 26 | B inner_loop ;go back to keep swapping |
| samvilm | 2:968db132481b | 27 | loop_exit |
| samvilm | 2:968db132481b | 28 | B outer_loop ;next iteration |
| samvilm | 2:968db132481b | 29 | done |
| samvilm | 2:968db132481b | 30 | POP {R2, R3, R4, R5, LR} ;free regs |
| samvilm | 2:968db132481b | 31 | BX LR |
| samvilm | 2:968db132481b | 32 | ALIGN |
| samvilm | 2:968db132481b | 33 | END |