查找数组中的最大项 - 浮点 - ASM

时间:2014-01-24 01:39:56

标签: c assembly asmx

我为整数编写了这个程序。这是该版本的代码:

#include <stdio.h>
#include <malloc.h>

int N;     // length of list
int *nums; // the list
int max;   // the result

int main( void )
{
    int i;
    printf("How large will the list be?  ");
    scanf_s("%d",&N);
    nums = (int*) malloc(N * sizeof(int));
    for (i=0; i<N; i++)
    {
        printf("Enter next number:  ");
        scanf_s("%d",&(nums[i]));
    }

   __asm
   {
       mov eax, N;    // A has the count
       dec eax;       // A is end of list
       mov ebx, nums; // B is head of list
       mov ecx, 0;    // C has the current largest

top:
       cmp eax, 0; 
       jl done;

       mov edx, [ebx+eax*4];
       cmp edx, ecx;
       jg Change;
       jmp Increment;

Increment:
       //mov eax,[ebx+ecx*4];
       dec eax; 
       jmp top;

Change:
       mov ecx, edx;
       jmp Increment;
done:
       mov max, ecx;
   }
   printf("Largest number in list = %d\n",max);
   scanf_s("%d",i);

}

现在,我需要为浮点数做同样的事情。在大多数情况下,我知道我不能使用相同的寄存器存储东西,我知道我必须使用“fcomi”而不是“cmp”。我已经仔细研究了几个小时,但确实需要一些指导。这是我到目前为止所做的:

#include <stdio.h>
#include <malloc.h>

int N;       // length of list
float *nums; // the list
float max;   // the result

int main( void )
{
    int i;
    printf("How large will the list be?  ");
    scanf_s("%d",&N);
    nums = (float*) malloc(N * sizeof(float));
    for (i=0; i<N; i++)
    {
        printf("Enter next number:  ");
        scanf_s("%f",&(nums[i]));
    }

    __asm
    {
        mov eax, N;     // A has the count
        dec eax;        // A is end of list
        mov esi, nums;  // B is head of list
        mov ecx, 0;     // C has the current largest

        cmp eax, 0;     // Check to see if counter has finished
        jl done;        // Jump to end
top:
        fld [esi+eax*4];    // Load first item to compare
        dec eax;            // Decrement counter
        fld [esi+eax*4];    // Load second item to compare
        fcomi st(0), st(1); // Compare the two items
        ja Change;          // Jump if st0 is above st1

        fstp max;           // Store in max, then pop from stack
        jmp Increment;      // Jump to Increment

Increment:
        dec eax;            // Decrement counter 
        jmp top;            // Jump to top

Change:
        fstp max;
        jmp Increment;
done:
    }
    printf("Largest number in list = %d\n",max);
    scanf_s("%d",i);

}

我主要使用C ++进行编程并在MIPS中进行了汇编,但C和嵌入式ASM对我来说都是新手。任何帮助表示赞赏!

-Rachel

0 个答案:

没有答案