I’m trying to write in assembly language a function sort. It will sort the two dimensional array such that the rows will now contain data in alphabetical order.
I have tried many things but it is honestly beyond my current knowledge.
here’s what I tried so far…
.386
public _Sort
.model flat
.code
_Sort proc
push ebp
mov ebp, esp
push esi
push edi
mov edi, [esp + 4] ; address of destination array
mov esi, [esp + 8] ; address of source array
mov ecx, [esp + 16] ; # of elements to mov
cld
rep movsd
L1:
mov eax, [esi]
cmp [esi + 8], eax
jg L2
xchg eax, [esi + 8]
mov [esi], eax
L2:
pop edi
pop esi
pop ebp
ret
_Sort endp
end
Here’s the C++ code…
#include <iostream>
using namespace std;
extern "C" int Sort (char [] [20], int, int);
void main ()
{
char Strings [10] [20]
= { "One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten" };
int i;
cout << "Unsorted Strings are" << endl;
for (i = 0; i < 10; i++)
cout << '\t' << Strings [i] << endl;
Sort (Strings, 10, 20);
cout << "Sorted Strings are" << endl;
for (i = 0; i < 10; i++)
cout << '\t' << Strings [i] << endl;
}
I do realize my assembly does not make sense, sorry about that.
You’ll want to build your assembly code in functions/procedures, just like you would code in some other language. Much like in C, string comparison, copying, etc., will need to be done in functions. Just for example:
[warning: this code isn’t necessarily intended to be used as-is–just an example of one of the kinds of functions you’ll normally need to write to do this sort of job in assembly language. It’s been long enough since I wrote much assembly language that you can undoubtedly do better on a modern processor–and at least for things done purely in assembly language, you normally want to put results in flags, rather than a -/0/+ in a register like
strcmp(and this) produce. But note that this does return with flags set by the finalsub]See also Why is memcmp so much faster than a for loop check? for some links to optimized implementations (SSE2/AVX2) for explicit-length strings, which can be much faster for medium to long strings. For optimization links in general, see the x86 tag wiki.
Your
sortwill depend on the sorting algorithm you decide to implement. Obviously, a Quicksort won’t look the same as an insertion sort. The main point, however, is simple: don’t try to write it as a single, monolithic chunk of code—break it up into pieces that are individually easy to write and understand.