Search code examples
arrayssortingassemblyx86inline-assembly

sort the array and print the smallest number in inline assembly in visual studio


I write the following code to read some numbers ranging from -15 to 15 from the user and the user may define how many numbers to enter. Then I bubble sort the array to get the smallest number. (Bubble sort because I will need to print other information) However, the code is not working. Here is my code.

// 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;
}

It would not return the smallest number. Sometimes it returns strange characters. I get really confusing. Additionally, when I enter negative numbers, the bubble sort seems not working well.


Solution

  • I have solved the problem. Here is my updated code:

    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
        }
    }