0

I'm having difficulties in one of my first program in the assembler x86 I'm trying to avg an array like that:

code:

    for(i = 0 ; i <20; i++)cin >> A[i];
_asm
{
    lea edx, A[0]
    mov ecx, 20
    mov esi, 0
    mov eax, 0
    L2:
        add eax, [edx + esi]
        add esi, 4
    loop L2
            mov eax, eax// dividend
            mov     ebx, 20
            div     ebx 
            mov i, eax

printf("mean is: ");
cout << i;

and this div ebx showing me an overflow. what am i doing wrong? what is the correct way to da that?

GBasi
  • 1
  • 1
  • 1
    Consult an instruction set reference. `div` uses `edx` as the top 32 bits of the dividend so you zero or sign extend `eax` into it. `mov eax, eax` makes no sense. – Jester Jun 14 '20 at 17:40
  • mov eax, eax is the only way eax keeps its value. what other way do you recommend? thanx for the help – GBasi Jun 14 '20 at 17:42
  • 1
    `mov eax, eax` does nothing at all; it stores in `eax` the same value that was already there. It is like writing `x=x;` in C++. What do you think it is doing? What do you mean "keep its value"? – Nate Eldredge Jun 14 '20 at 17:43
  • i know that you mean and im 100% agree. This is a silly command that for some unclear reason allows me to work with EAX. Without it, the compiler says EAX contains no value. And that's one of the reasons I went to the forum, I can't figure it out – GBasi Jun 14 '20 at 17:49

0 Answers0