assembly 在visual studio的内联程序集中对数组进行排序并打印最小的数字

6xfqseft  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(112)

我写了下面的代码来从用户那里读取一些数字,范围从-15到15,用户可以定义要输入多少个数字。然后我对数组进行冒泡排序以获得最小的数字。(冒泡排序,因为我将需要打印其他信息)然而,代码不起作用。这是我的代码。

// oops.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
char message0[] = "How many numbers do you want to enter? \n";
char message1[] = "Enter the current reading: \n";
char message2[] = "Error!\n";
char message3[] = "The smallest number is: \n";
char format1[] = "%d";
char format2[] = "%s";;
int myarray[10000];
int No;
int counter;
int *p;
p = myarray - 1;

_asm{
        lea eax, message0
        push eax
        call printf
        add esp, 4
//read how many numbers the user would like to input
        lea eax,counter
        push eax
        lea eax, format1
        push eax
        call scanf_s
        add esp,8

        mov No, 1
        mov ecx, counter
        mov ebx, 0
//read user's input
Input:      push ecx
            push No
            lea eax, message1
            push eax
           call printf
            add esp, 8

        lea eax, myarray[ebx]
        push eax
        lea eax, format1
        push eax
        call scanf_s

        add esp,8
//judge if the number is in the range of -15 to 15
JudgeInput: mov eax, myarray[ebx]
        cmp eax,-15
        jl Illegal
        cmp eax,15
        jle Legal

Illegal: lea eax,message2
     push eax
     call printf
     add esp,4
     pop ecx
     jmp Input

Legal:    add ebx,4
      inc No
      pop ecx
      loop Input

//bubble sort
mov esi, p
mov ecx, counter

outer : mov edx, ecx
inner : cmp edx, ecx
        jz exchangeNo
        mov eax, [esi + ecx * 4]

        mov ebx, [esi + edx * 4]
        cmp eax, ebx
        jnb exchangeNo
        mov[esi + ecx * 4], ebx
        mov[esi + edx * 4], eax

    exchangeNo :
        dec edx
            jnz inner
            loop outer


finish:
smallest: //print the smallest number  
      mov ebx,0
      lea eax,message3
      push eax
      lea eax, format2
      push eax
      call printf
      mov eax,0
      lea ebx,myarray
      sub ebx,4
      add ebx,No
      lea eax, [ebx]
      push eax
      lea eax,format1
      call printf
      add esp,16
}

return 0;
}

它不会返回最小的数字。有时它返回奇怪的字符。我真的很困惑。此外,当我输入负数时,冒泡排序似乎不能很好地工作。

mctunoxg

mctunoxg1#

我已经解决了这个问题。下面是我更新的代码:

int _tmain(int argc, _TCHAR* argv[])
{
    char message0[] = "How many numbers do you want to enter? \n";
    char message1[] = "Enter the current reading: \n";
    char message2[] = "Error!\n";
    char message3[] = "\nThe smallest number is: ";
    char format1[] = "%d";
    char format2[] = "%s";;
    int myarray[10000];
    int No;
    int counter;
    int *p;
    p = myarray - 1;

    _asm{
        lea eax, message0
        push eax
        call printf
        add esp, 4

        lea eax,counter
        push eax
        lea eax, format1
        push eax
        call scanf_s
        add esp,8

    //get the user's input into the array
        mov No, 1
        mov ecx, counter
        mov ebx, 0

    Input:      
        push ecx
        push No
        lea eax, message1
        push eax
        call printf
        add esp, 8

        lea eax, myarray[ebx]
        push eax
        lea eax, format1
        push eax
        call scanf_s
        add esp,8
    //judge if the input is between -15 and 15
    JudgeInput: 
        mov eax, myarray[ebx]
        cmp eax,-15
        jl Illegal
        cmp eax,15
        jle Legal
    //if not, print out error message
    Illegal: 
         lea eax,message2
         push eax
         call printf
         add esp,4
         pop ecx
         jmp Input

    //if yes, loop again
    Legal:    
          add ebx,4
          inc No
          pop ecx
          loop Input

    //bubble sort
    mov esi, p
    mov ecx, counter
    //the outer loop
    outer : mov edx, ecx
    //the inner loop
    inner : cmp edx, ecx
        je exchangeNo
        mov eax, [esi + ecx * 4]
        mov ebx, [esi + edx * 4]
        cmp eax, ebx
        jge exchangeNo
        mov[esi + ecx * 4], ebx
        mov[esi + edx * 4], eax
    exchangeNo :
            dec edx
            jge inner
            loop outer


    finish:
    //find out the smallest number
    smallest :
        lea eax, message3
        push eax
        lea eax, format2
        push eax
        call printf

        lea ebx, myarray
        mov eax, [ebx]

        push eax
        lea eax, format1
        push eax
        call printf
        add esp, 16
    }
}

相关问题