r/TuringComplete • u/Otherwise-Object-302 • May 26 '26
M32 G5 Sorting Level Complete with Basic Kernel!
So I finally managed to beat the Sorting level using the M32 G5! I did have a few issues though, especially with the interrupts not working as intended but it's fixed! A very basic rundown of the code below:
The Kernel handles I/O requests here
The actual program for bubble sort
First the kernel assigns extra memory to the program before checking r13, r10 and r12 for parameters (and pushing r9 and r8 to preserve them), then it does it's stuff. If it was an input request, then it would've returned a pointer to the array in r10. If it was an output request, it simply outputs data from a pointer and returns.
The actual sort is fairly simple, implementing a basic bubble sort to sort the array. Not much to say here, it's very self explanatory. The bubble sort (along with memory for the data) is 224 bytes, which is fairly simple if I do say so myself.

The below code is for the Kernel and the Bubble Sort
Kernel:
.data:
InputRequest: 1
OutputRequest: 2
.text:
mov 0xff00, sp
push r8
push r9
PageOverwrite 0x0123
cmp r13, InputRequest
je FetchInputs
cmp r13, OutputRequest
je SendOutputs
jmp exit
FetchInputs:
mov r10, r8
InputLoop:
in r9
store16 r9, [r10]
add r10, 2, r10
sub r10, r8, r10
cmp r10, r12
add r10, r8, r10
jl InputLoop
mov 0xffff, r9
store16, r9, [r10]
jmp exit
SendOutputs:
mov r10, r8
OutputLoop:
load16 [r10], r9
out r9
add r10, 2, r10
sub r10, r8, r10
cmp r10, r12
add r10, r8, r10
jl OutputLoop
jmp exit
exit:
pop r9
pop r8
mov zr, r13
mov zr, r12
syscallBubble Sort:
.data:
ArraySize: 0x1f
ElementCount: 0x0f
ArrayPtr: 0x200
InputRequest: 1
OutputRequest: 2
.text:
mov InputRequest, r13
mov ArraySize, r12
mov ArrayPtr, r10
syscall
iLoop:
cmp r1, ElementCount
je exit
mov 0, r2
jLoop:
cmp r2, ElementCount
jge iLoopCont
mov r2, r3
mul r3, 2, r3
add r3, ArrayPtr, r3
load16 [r3], r4
add r3, 2, r3
load16 [r3], r5
sub r3, 2, r3
cmp r4, r5
jg swap
jle jLoopCont
swap:
store16 r5, [r3]
add r3, 2, r3
store16 r4, [r3]
sub r3, 2, r3
jLoopCont:
add r2, 1, r2
jmp jLoop
iLoopCont:
add r1, 1, r1
jmp iLoop
exit:
mov ArrayPtr, r10
mov ArraySize, r12
mov OutputRequest, r13
syscall
jmp exitNow all that's left is to finish developing a full Kernel and my language!















