I'm trying to implement a selection sort algorithm in C++ Assembly Blocks. The code below shows the sorter function with the assembly block within. I am trying to emulate the selection sort algorithm shown below my code. When I compile my cpp file and try to run the given code (separate from this) I get the exact same numbers back in the same order. (the first data set has 10 numbers [10, -20, 5, 12, 30, -5, -22, 55, 52, 0]). What should I change to get my desired results?
void sorter (long* list, long count, long opcode)
{
/* Move the array pointer to rax, opcode to rbx, count to rcx */
/* The following sample code swap the array elements in reverse order */
/* You would need to replace it with the logic from the bubble sort algorithm */
long temp;
long y;
asm
(
"movq %0, %%rax;" //Sets array pointer (base address of array) to rax register
"movq %1, %%rbx;" //Sets opcode (1 for Asc. | 2 for Desc) to rbx register
"movq %2, %%rcx;" //Sets count (total amount of #'s) to rcx register
"xorq %%rdx, %%rdx;" //Sets rdx (x counter) to 0
"movq %3, %%r9;" //Sets temp (used for swapping) to r9 register
"loop_start:"
"dec %%rcx;" //Decrements rcx (count)
"cmpq %%rdx, %%rcx;" //Compares rdx (x counter) to rcx (count)
"jle done;" //If rcx (total amount of #'s) is zero, then finish
"cmpq $1,%%rbx;" //Compares rbx (opcode) to 1
"jne desc;" //Jump to descending if opcode != 1 (2 or more)
"mov %%rdx, %%rdi;" //Sets rdi (y counter) = x
"inner_loop:"
"inc %%rdi;" //Increments rdi (y counter) (y++)
"movq (%%rax, %%rdx, 8), %%rsi;" //Sets rsi to array pointer + 8*rdx (array[x])
"movq (%%rax, %%rdi, 8), %%r8;" //Sets r8 to array pointer + 8*rdi (array[y])
"cmpq %%r8, %%rsi;"
"jle swap;"
"cmpq %%rdi, %%rcx;" //Compares rdi (y) and rcx (count)
"jb inner_loop;" //Jump to inner_loop if y < count
"inc %%rdx;" //Increment rdx (x counter) (x++)
"jmp loop_start;" //Closing for outer loop (loop_start)
"swap:"
"xchgq %%rsi,%%r9;"
"xchgq %%r8, %%rsi;"
"xchgq %%r9, %%r8;"
"jmp inner_loop;"
"desc:" //if opcode is 2 then reverse the list
"movq (%%rax, %%rcx, 8), %%r10;" //Moves array pointer + 8*rcx(count) to r10 (starts at last index of the array)
"movq (%%rax, %%rdx, 8), %%r11;" //Moves array pointer + 8*rdx to r11 (starts at first index of the array)
"xchgq %%r10, (%%rax, %%rdx, 8);"
"xchgq %%r11, (%%rax, %%rcx, 8);"
"inc %%rdx;"
"jmp loop_start;"
"done:"
:
: "m" (list), "m" (opcode), "m" (count), "m" (temp)
:
);
}
Selection sort algorithm to implement:
void sorter (long* list, long count, long opcode)
{
long x, y, temp;
for (x = 0; x < count - 1; x++)
for (y = x; y < count; y++)
if (list[x] > list[y])
{
temp = list[x];
list[x] = list[y];
list[y] = temp;
}
}