views:

38

answers:

1

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.

+1  A: 

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:

; compares [esi] to [edi], returns +, 0 or - to indicate order
;
strcmp proc 
    jmp short start
loop_top:
    inc esi
    inc edi
start:
    lodsb
    sub al, [edi]
    jz loop_top
    ret
strcmp endp

Your sort will 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.

Jerry Coffin