There are 2 modules (assembler and C), it is necessary to calculate the minimum element of the array, which I enter using C functions and then call the asm function, which should find the min. element, the problem is that during the execution of the program, after entering the array, the program crashes with an error (violation of access rights to read) and points to the line

mov ebx,[esi] 

This code is working and should be executed, since similar code is executed.

 mov eax, [esi] 

Moreover, even if you replace ebx with eax, the program still crashes, I don’t go outside the array, I don’t do anything forbidden, the program just crashes in this place. I don’t know what to do, because the code is correct and the program doesn’t work, I tried to re-create the project (in the settings I specified everything correctly, other programs work well).

Assembly module:

 .586 .MODEL FLAT, C .DATA .CODE MAS_FUNC PROC C mas:dword, n:dword xor eax,eax xor ebx,ebx mov esi,mas mov eax, [esi] mov ecx,n cycle: mov ebx,[esi] ;тут вылетает cmp eax,ebx ;тут тоже вылетает,даже если удалил пред строку и добавил ;[esi] jg metka continue: add esi,4 loop cycle metka: mov eax,[esi] jmp continue ret MAS_FUNC ENDP END 

C module:

 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; extern "C" int MAS_FUNC(int *, int); int main() { int *mas, n, k; system("chcp 1251"); system("cls"); cout << "Введите размер массива: "; cin >> n; mas = new int[n]; cout << "Введите элементы массива: " << endl; for (int i = 0; i<n; i++) { cout << "mas[" << i << "]= "; cin >> mas[i]; } k = MAS_FUNC(mas, n); cout << k; cin.get(); cin.get(); return 0; } 

Mistake

  • Something tells me that mov esi,mas places in esi the first element of the array, and not the address of this array. can try something like mov esi, offset mas or lea esi, mas . - Mike
  • @Mike if I write mov eax, [esi + 4], everything is correctly executed and the first element of the array is taken, if [esi + 8] is the second, everything is correctly entered - andybelous2
  • Then you would have to show in asm code how mas and n are declared. But it’s not clear that mas is the first element or variable containing the address of the first element (i.e., pointer) - Mike
  • @Mike mas is a pointer, in the example from which I reworked, it worked that way, access was done in exactly this way - andybelous2
  • I also don't like add esi,4; loop cycle add esi,4; loop cycle and especially immediately after it mov eax,[esi] at the last step of the cycle, when n has already ended, esi increases and begins to point outside the array, after which the transition to mov occurs. In general, you would be in the debugger to look like this code, it is more reliable than looking at the text - Mike

0