
Untitled
By: a guest on
Jul 15th, 2012 | syntax:
None | size: 1.26 KB | hits: 13 | expires: Never
#include <stdio.h>
void sort_array(int n, int target[])
{
// Variable init.
int i;
// Preliminary implementation in C:
/*int temp;
int location;
for (i = 1; i < n; i++)
{
temp = target[i];
location = i;
while (location >= 1 && target[location - 1] > temp)
{
target[location] = target[location - 1];
location--;
}
target[location] = temp;
}*/
// Assemblin' time!
__asm{
mov i, 1
top1: mov eax, i
cmp eax, n
jge done1
mov edx, 0
mov ebx, 4
mul ebx
mov ebx, target[eax]
top2: cmp eax, 4
jl done2
cmp target[eax] - 4, ebx
jle done2
mov edx, target[eax] - 4
mov target[eax], edx
sub eax, 4
jmp top2
done2: mov target[eax] - 4, ebx
inc i
jmp top1
done1: nop
}
// Print out the array.
for (int i = 0; i < n; i++)
printf("%d ", target[i]);
printf("\n");
}
void main()
{
// Initialize the arrays.
int array1[] = {100, 99, 97, 95, 90, 87, 86, 83, 81, 77, 74, 69, 63, 50, 44, 43, 39, 31, 29, 12};
int array2[] = {123456, 342100, 87539, 606006, 443322, 198371, 99109, 88018, 707007};
int size1 = 20;
int size2 = 9;
// Do the sort.
sort_array(size1, array1);
sort_array(size2, array2);
// Wait for keypress because Windows consoles disappear.
getchar();
}