MASM我似乎无法在C数组中找到要交换的正确元素

时间:2015-12-04 04:09:49

标签: c++ arrays masm bubble-sort

我尽力学习MASM,今天我尝试比较C ++和MASM之间的冒泡排序。我已经让C ++正常运行,但是当我进入MASM函数时,数组的第一个元素不应该是它应该是什么,通常是300万个而不是0-32767之间的随机值。然后我正在比较的寄存器的值将不会交换并读取传递给函数的相同的随机,未排序的数组。我还发现我无法遍历数组。谢谢!

c ++代码(不那么重要)

#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int deepBlueDecend(int* num, int size);
int deepBlueAscend(int* num, int size);
//int setArray(int* num, int size);

extern"C"
{
    int KasparovAscend(int *, int);
    int KasparovDecend(int *, int);
}

int main()
{
    int ascOrDec = 2, size = 0;
    cout << "How big do you want the array to be sorted? ";
    cin >> size;

    int* myArray = 0;
    myArray = new int[size];
    int* asmArray = 0;
    asmArray = new int[size];

    srand((unsigned)time(0));                       // gets actually random numbers somehow
    for (int i = 0; i < size; i++)                  //populates the arrays with random numbers
    {
        myArray[i] = rand();
        asmArray[i] = myArray[i];
    }

    for (int i = 0; i < size; i++) cout << asmArray[i] << " "; //displays asmArray[]
    cout << endl;

    while (ascOrDec < 0 || ascOrDec > 1)            //test if ascending or decending has been chosen
    {
        cout << "Ascending (0) or decending(1)? ";
        cin >> ascOrDec;        
        if (ascOrDec == 0 || ascOrDec == 1)
            break;
    }

    cout << endl;
    if (ascOrDec == 0)
    {
        KasparovAscend(myArray, size);

        for (int i = 0; i < size; i++) cout << asmArray[i] << " "; //to see if anything changed in the assembly sort
        cout << endl;

        clock_t startTime = clock();
        deepBlueAscend(myArray, size);
        clock_t endTime = clock();
        clock_t clockTicksTaken = endTime - startTime;
        double timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC;


        for (int i = 0; i < size; i++) cout << myArray[i] << " "; //to see if anything changed in the C++ sort
        cout << "\nTime Taken for c++: " << clockTicksTaken << endl;
    }
    else
    {
        clock_t startTime = clock();
        deepBlueDecend(myArray, size);
        clock_t endTime = clock();
        clock_t clockTicksTaken = endTime - startTime;
        double timeInSeconds = clockTicksTaken / (double)CLOCKS_PER_SEC;
        cout << "\nTime Taken for c++: " << clockTicksTaken << endl;
    }

    system("pause");
    return 0;
}

int deepBlueAscend(int* arr, int size)
{
    int i, j, flag = 1;    // set flag to 1 to start first pass
    int temp;             // holding variable

    for (i = 1; (i <= size) && flag; i++)
    {
        flag = 0;
        for (j = 0; j < (size - 1); j++)
        {
            if (arr[j + 1] < arr[j])      // ascending order <
            {
                temp = arr[j];             // swap elements
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                flag = 1;               // indicates that a swap occurred.
            }
        }
    }
    return 0;
}

int deepBlueDecend(int* arr, int size)
{
    int i, j, flag = 1;    // set flag to 1 to start first pass
    int temp;             // holding variable

    for (i = 1; (i <= size) && flag == 1; i++)
    {
        flag = 0;
        for (j = 0; j < (size - 1); j++)
        {
            if (arr[j + 1] > arr[j])      // decending order  >
            {
                temp = arr[j];             // swap elements
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                flag = 1;               // indicates that a swap occurred.
            }
        }
    }
    return 0;
}

Masm Code:

.686
.model flat
.code

_KasparovAscend PROC ; named _test because C automatically prepends an underscode, it is needed to interoperate
    push ebp
    mov ebp,esp ; stack pointer to ebp

    push edi            ;save this
    push ebx            ;save this

    mov ebx,[ebp+8]     ; address of first array element
    mov ecx,[ebp+12]    ; number of elements in array

    mov edi, [ebx+4]        ;second element
    mov edx, 0          ;set counter to 0




LoopTraverse:
    cmp ecx, edx
    je AllDone
    inc edx                 ;increment loopTraverse counter
    mov eax, -1             ;set couter in loopSwap to be 0 when first entered


    LoopSwap:
        inc eax             ;increment
        cmp ecx, eax        ;compares counter to number of elements
        je LoopTraverse     
        cmp edi, ebx        ;comparing the two values
        jg NextElements

        push ebx            ;trade the two elements
        mov ebx, edi
        pop edi

        add ebx, 4          ;goes to the next element in array
        add edi, 4
        jmp LoopSwap

        NextElements:
            add ebx, 4      ;goes to next element in array
            add edi, 4
            jmp LoopSwap

allDone:

    pop edi
    pop edx
    pop ebp
    ret
_KasparovAscend ENDP

END 

0 个答案:

没有答案