Last active
October 22, 2016 22:44
-
-
Save jcchurch/9a10e30d2ae00bec31959590db9a0a48 to your computer and use it in GitHub Desktop.
BubbleSort in the MarieSim language.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bubble Sort | |
// By James Church | |
// 2016-10-20 | |
// | |
// How this works: | |
// Scroll down the bottom of this file. | |
// You'll see 10 values that are randomly | |
// ordered. This is the input to the algorithm. | |
// These can be rearranged into any order. | |
// | |
// The unsorted array exists from 040 to 049. | |
// You can watch the memory layout of that range | |
// and watch the bubble sort work. | |
// | |
// Set the output to "Hex". Set the speed to be | |
// as fast as possible. Compile and run the | |
// program. | |
// Load the next two values to test into | |
// aaddr and baddr. | |
loop, load arrayaddr | |
add index | |
store aaddr | |
add one | |
store baddr | |
// Test to see if value at aaddr > value at baddr | |
// If not, continue | |
// If so, send these values to swap. | |
loadi aaddr | |
store c | |
loadi baddr | |
subt c | |
skipcond 800 | |
jns swap | |
// Increment index and and check if index - loopcount < 0 | |
// If not, continue | |
// If so, jump to resetloops | |
load index | |
add one | |
store index | |
subt loopcount | |
skipcond 000 | |
jns resetloops | |
// Check if loopcount != 0. | |
// If not, continue to print values. | |
// If so, jump to loop (beginning of program) | |
load loopcount | |
skipcond 400 | |
jump loop | |
// Print the values and halt! | |
print, load arrayaddr | |
add index | |
store aaddr | |
loadi aaddr | |
output | |
load index | |
add one | |
store index | |
subt ten | |
skipcond 400 | |
jump print | |
halt | |
// Set index to 0, Decrement loop count | |
resetloops, hex 0 | |
clear | |
store index | |
load loopcount | |
subt one | |
store loopcount | |
jumpi resetloops | |
// Swap the values of aaddr and baddr. | |
swap, hex 0 | |
loadi aaddr | |
store c | |
loadi baddr | |
storei aaddr | |
load c | |
storei baddr | |
jumpi swap | |
// Variables | |
one, hex 1 // Constant | |
ten, hex A // Constant | |
index, hex 0 // Inner index | |
loopcount, hex 9 // Outer index | |
aaddr, hex 0 // A address | |
baddr, hex 0 // B address | |
c, hex 0 // C (Temporary) | |
arrayaddr, hex 040 // Array location | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
hex 0 // Unused | |
// Array to sort, 10 elements long | |
hex 7 | |
hex 5 | |
hex 8 | |
hex A | |
hex 4 | |
hex 3 | |
hex 9 | |
hex 1 | |
hex 6 | |
hex 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment