2016-12-11 9 views
-2

ユーザーから-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; 
} 

最小の数値は返されません。時にはそれは奇妙な文字を返すことがあります。私は本当に混乱します。さらに、負の数を入力すると、バブルのソートがうまくいかないようです。

+2

あなたのコードをコメントしてください。デバッガを使用してプログラムをシングルステップ実行し、自分がどこに間違っているかを確認する方法を学びます。 – Jester

+0

私はそれを段階的に行い、情報 "アクセス違反の読み込み場所..."に情報があります –

+0

命令では...その命令が無効なメモリにアクセスする理由を確認してください。 – Jester

答えて

0

私はこの問題を解決しました。ここに私の更新されたコードがあります:

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 
    } 
} 
関連する問題