|
| 1 | +;chip 8 bubble sort |
| 2 | +;by codingwithethanol |
| 3 | + |
| 4 | + |
| 5 | + LD V2, 1 ;index increment |
| 6 | + LD I, ARRAY ;get array pointer |
| 7 | + LD V3, 12 ;our array will be 12 bytes long |
| 8 | + |
| 9 | +FILL |
| 10 | + RND V0, #0F ;get random byte value from 0-F |
| 11 | + LD [I], V0 ;load it to current array index |
| 12 | + ADD I, V2 ;increment i |
| 13 | + ADD V3, -1 ;decrement counter |
| 14 | + SE V3, #0 ;check if counter has reached zero |
| 15 | + JP FILL ;if it hasnt, loop |
| 16 | + LD V3, 2 ;x position to print array to |
| 17 | + LD V4, 4 ;y position to print array to |
| 18 | + CALL DISPLAY ;display array |
| 19 | + CALL BUBBLESORT ;sort array |
| 20 | +HERE |
| 21 | + JP HERE ;block |
| 22 | + |
| 23 | +DISPLAY |
| 24 | + LD V5, V3 ;save original x offset |
| 25 | + ADD V5, 60 ;add length of array times char width |
| 26 | + LD V2, 0 ;array index |
| 27 | +DRAWLOOP |
| 28 | + LD I, ARRAY ;get array pointer |
| 29 | + ADD I, V2 ;add index |
| 30 | + ADD V2, 1 ;increment index |
| 31 | + LD V0, [I] ;load element of array |
| 32 | + LD F, V0 ;get address of corresponding hex sprite |
| 33 | + DRW V3, V4, 5 ;draw it at (V3, V4) |
| 34 | + ADD V3, 5 ;increment x by char width |
| 35 | + SE V3, V5 ;if we arent at the end of the array |
| 36 | + JP DRAWLOOP ;draw another char |
| 37 | + RET |
| 38 | + |
| 39 | +BUBBLESORT |
| 40 | +SETUP |
| 41 | + LD V4, 0 ;no swap has been performed |
| 42 | + LD V3, 0 ;we are starting at the beginning of the array |
| 43 | +CHECKLOOP |
| 44 | + LD I, ARRAY ;load array pointer |
| 45 | + ADD I, V3 ;load array index |
| 46 | + LD V1, [I] ;load 2 bytes from arraypos |
| 47 | + LD V2, V1 ;temp = b |
| 48 | + SUB V2, V0 ;temp -= a |
| 49 | + SE VF, 0 ;if b < a |
| 50 | + JP GRTHAN ;jump here |
| 51 | +LSTHAN |
| 52 | + LD V2, V1 ;temp = b |
| 53 | + LD V1, V0 ;b = a |
| 54 | + LD V0, V2 ;a = temp |
| 55 | + LD [I], V1 ;store back swapped values |
| 56 | + LD V4, 1 ;swap = true |
| 57 | +GRTHAN |
| 58 | + ADD V3, 1 ;increment array index |
| 59 | + SE V3, 12 ;if not end of array |
| 60 | + JP CHECKLOOP ;step |
| 61 | +CHECKDONE ;if end of array |
| 62 | + SE V4, 0 ;if a swap was done |
| 63 | + JP SETUP ;iterate through array again |
| 64 | + ;otherwise |
| 65 | + LD V3, 2 ;x position to print array to |
| 66 | + LD V4, 12 ;y position to print array to |
| 67 | + CALL DISPLAY ;display sorted array |
| 68 | + RET |
| 69 | + |
| 70 | +ARRAY |
0 commit comments