I am new to Assembler and I have the following question: there is a sequence of integers. It is necessary to determine whether this sequence is ordered. How to implement it? I could only write array initialization

#include <iostream> #include <cstdlib> #include <ctime> #include <Windows.h> using namespace std; int main() { setlocale(LC_ALL, ".1251"); int f1, f2; // границы интегрирования int numb; // количество элементов последовательности int f; // счетчик, пока не заполним наш массив int x; //элемент последовательности int *num, *num2; //массивы int i, z = 0, k = 0, a, b; cout << "Введите количество элемнетов последовательности: "; cin >> numb; num = new int[numb]; num2 = new int[numb]; srand(time(NULL)); cout << "Введите границы генерирования чисел последовательности от "; cin >> f1; cout << "До "; cin >> f2; cout << "Элементы исходной последовательности:" << endl; _asm { xor ecx, ecx xor edi, edi // смещение относительно начала массива xor ebx, ebx xor edi, edi mov ecx, numb mov f, ecx mov edi, num mov ebx, num2 cycle1 : xor eax, eax xor edx, edx } x = rand() % (f2 - f1 + 1) + f1; cout << x << " "; _asm { xor eax, eax xor esi, esi mov edx, z mov eax, x cmp edx, 0 je insert find1 : cmp eax, [edi][esi] je ravno add esi, 4 cmp esi, edx je insert jmp find1 ravno : inc[ebx][esi] jmp end1 insert : mov[edi][edx], eax mov eax, 1 mov[ebx][edx], eax add edx, 4 inc k end1 : dec f cmp f, 0 mov z, edx mov eax, [ebx][0] mov a, eax mov edx, [edi][0] mov b, edx jne cycle1 } cout << endl; } 
  • If “ordering” means strictly “ascending” or “descending”, then we compare the previous element in the cycle with the current element and remember “more-less”, if at the next check this sign changes, it is not ordered. if they reach the end of the array and the sign has not changed - it is ordered. Equal neighboring elements apparently ignore - Mike

1 answer 1

Something I did not understand what your code does and for the first time I see [edi] [esi], and why two arrays? I wrote my implementation. My perverted mind wanted to use self-modifying code, but for this, I would have to change the flags of the code section or call VirtualProtect, so I simply managed a dynamic transition.

 int main() { setlocale(LC_ALL, "Russian"); cout << "Размер массива: "; int n; bool res = false; //результат cin >> n; int* arr = new int[n]; cout << "Массив: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } //ebx - первый элемент массива //edi - индекс //eax - текущий элемент //esi - предыдущий элемент //ecx - направление __asm { mov ebx, arr xor edi, edi mov ecx, setdir mov eax, [ebx] //eax = arr[0] loop1: mov esi, eax //esi = arr[i] inc edi //i++ cmp edi, n je yes //ответ да lea eax, [ebx + edi * 4] mov eax, [eax] //eax = arr[i] cmp eax, esi je loop1 jmp ecx setdir: //ТРЮК С ДИНАМИЧЕСКИМ ПЕРЕХОДОМ!!! ВАУ!!! mov ecx, asc //ecx = сравнение на возрастание jg loop1 mov ecx, desc //ecx = сравнение на убывание jmp loop1 asc: jge loop1 //если esi > eax, то ответ нет jmp exit1 desc: jle loop1 //если esi < eax, то ответ нет jmp exit1 yes: inc res //res = true exit1: } if (res) cout << "Массив упорядочен"; else cout << "Массив неупорядочен"; return 0; }