При выполнении ассемблерной вставки на последнем printf вылазит нарушение прав доступа, хотя результат считает правильно
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
short int a[N];
short int c = 0, d = 0 , k=1;
printf("c<=a[i]<=d\n\n");
do {
printf("Enter the values of the range [-32768...32767]:\n");
printf("c = "); scanf_s("%hd", &c);
printf("d = "); scanf_s("%hd", &d);
if (c >= d)
{
printf("c can not be greater or equal d! Enter values again.\n\n");
}
} while (c >= d);
int n = N;
short int res = 1, resC = 1;
for (int i = 0; i < N; i++)
{
a[i] = rand() % 10;
printf("A[%d] = %d\n", i, a[i]);
if (c <= a[i] && a[i] <= d) resC *= a[i];
}
printf("Result C++ = %d\n", resC);
__asm
{
mov di, c // <di>=c
mov bx, d //<bx>=d
mov ecx, n //<ecn>=n
dec ecx //<ecn>=n-1
mov sp,1
mov ax, sp;
cycle :
shl ecx, 1 // зсув вліво на 1 розряд
mov si, a[ecx] //<si>= a[ecx]
cmp si, 0 // порівнюємо значення регістра <si> з 0
jl exit1 // якщо менше - перейти до циклу exit1
cmp si, di // порівнюємо значення регістра <si> з значенням в регістрі <di>
jl exit1 // якщо менше - перейти до циклу exit1
cmp si, bx // порівнюємо значення регістра <si> з значенням в регістрі <bx>
jg exit1 // якщо більше - перейти до циклу exit1
imul si
idiv sp
exit1 :
shr ecx, 1 // зсув вправо на 1 розряд
dec ecx // зменшуємо значення в регістрі <ecx> на 1
cmp ecx, 0 // порівнюємо значення регістра <ecx> з 0
jnl cycle // поки не менше - перейти до циклу cycle
mov res,ax
}
if (res > 32767 || res < -32768)
{
printf("Overflow!\n");
}
else
{
printf("Result ASM = %d\n", res);
}
_getch();
return 0;
}