Skip to content

Added Bubblesort in Chip-8 assembly #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"lang": "c",
"name": "C"
},
{
"lang": "c8",
"name": "chip-8"
},
{
"lang": "py",
"name": "Python"
Expand Down
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs)
{% sample lang="c" %}
[import:10-20, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="c8" %}
[import:39-63, lang:"chip-8"](code/chip8/bubblesort.c8)
{% sample lang="java" %}
[import:2-12, lang:"java"](code/java/bubble.java)
{% sample lang="kotlin" %}
Expand Down Expand Up @@ -85,6 +87,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
[import, lang:"csharp"](code/csharp/Program.cs)
{% sample lang="c" %}
[import, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="c8" %}
[import, lang:"chip-8"](code/chip8/bubblesort.c8)
{% sample lang="java" %}
[import, lang:"java"](code/java/bubble.java)
{% sample lang="kotlin" %}
Expand Down
70 changes: 70 additions & 0 deletions contents/bubble_sort/code/chip8/bubblesort.c8
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
;chip 8 bubble sort
;by codingwithethanol


LD V2, 1 ;index increment
LD I, ARRAY ;get array pointer
LD V3, 12 ;our array will be 12 bytes long

FILL
RND V0, #0F ;get random byte value from 0-F
LD [I], V0 ;load it to current array index
ADD I, V2 ;increment i
ADD V3, -1 ;decrement counter
SE V3, #0 ;check if counter has reached zero
JP FILL ;if it hasnt, loop
LD V3, 2 ;x position to print array to
LD V4, 4 ;y position to print array to
CALL DISPLAY ;display array
CALL BUBBLESORT ;sort array
HERE
JP HERE ;block

DISPLAY
LD V5, V3 ;save original x offset
ADD V5, 60 ;add length of array times char width
LD V2, 0 ;array index
DRAWLOOP
LD I, ARRAY ;get array pointer
ADD I, V2 ;add index
ADD V2, 1 ;increment index
LD V0, [I] ;load element of array
LD F, V0 ;get address of corresponding hex sprite
DRW V3, V4, 5 ;draw it at (V3, V4)
ADD V3, 5 ;increment x by char width
SE V3, V5 ;if we arent at the end of the array
JP DRAWLOOP ;draw another char
RET

BUBBLESORT
SETUP
LD V4, 0 ;no swap has been performed
LD V3, 0 ;we are starting at the beginning of the array
CHECKLOOP
LD I, ARRAY ;load array pointer
ADD I, V3 ;load array index
LD V1, [I] ;load 2 bytes from arraypos
LD V2, V1 ;temp = b
SUB V2, V0 ;temp -= a
SE VF, 0 ;if b < a
JP GRTHAN ;jump here
LSTHAN
LD V2, V1 ;temp = b
LD V1, V0 ;b = a
LD V0, V2 ;a = temp
LD [I], V1 ;store back swapped values
LD V4, 1 ;swap = true
GRTHAN
ADD V3, 1 ;increment array index
SE V3, 12 ;if not end of array
JP CHECKLOOP ;step
CHECKDONE ;if end of array
SE V4, 0 ;if a swap was done
JP SETUP ;iterate through array again
;otherwise
LD V3, 2 ;x position to print array to
LD V4, 12 ;y position to print array to
CALL DISPLAY ;display sorted array
RET

ARRAY