Skip to content

Commit 8578823

Browse files
author
Julian
authored
Merge pull request #547 from theinventor13/patch-1
Added Bubblesort in Chip-8 assembly
2 parents 09e5b96 + 5116bd5 commit 8578823

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

book.json

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
"lang": "c",
5353
"name": "C"
5454
},
55+
{
56+
"lang": "c8",
57+
"name": "chip-8"
58+
},
5559
{
5660
"lang": "py",
5761
"name": "Python"

contents/bubble_sort/bubble_sort.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1414
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs)
1515
{% sample lang="c" %}
1616
[import:10-20, lang:"c_cpp"](code/c/bubble_sort.c)
17+
{% sample lang="c8" %}
18+
[import:39-63, lang:"chip-8"](code/chip8/bubblesort.c8)
1719
{% sample lang="java" %}
1820
[import:2-12, lang:"java"](code/java/bubble.java)
1921
{% sample lang="kotlin" %}
@@ -85,6 +87,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
8587
[import, lang:"csharp"](code/csharp/Program.cs)
8688
{% sample lang="c" %}
8789
[import, lang:"c_cpp"](code/c/bubble_sort.c)
90+
{% sample lang="c8" %}
91+
[import, lang:"chip-8"](code/chip8/bubblesort.c8)
8892
{% sample lang="java" %}
8993
[import, lang:"java"](code/java/bubble.java)
9094
{% sample lang="kotlin" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)