1

I have an array of 10 int and I am trying to write a loop to calculate a number of occurrence of a specific integer which I stored in a variable in x86 assembly using the Irvine32 library

the int i want to calculate number of occurrence of it is stored in "maxi" the var where i will count in is "numofocc" array 's type is word so i add 2 to the array to move to the next place

mov ecx ,10                                   
 L:         
  cmp maxi , [array]  
  je count    
  add array ,2     
 count:    
  add  numofocc ,1    
  add array ,2     
 loop L 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mera
  • 11
  • 3
  • Please provide some marked up code and describe the problem. – Bouke Aug 23 '18 at 11:26
  • As far as I know memory-to-memory operands like `cmp maxi, [array]` are not allowed. Also you have incremented the array address twice (each time by 2) and the branch test does not skip the incrementing of `numofocc`, which is always done. – Weather Vane Aug 23 '18 at 12:58
  • but by adding these brackets [] to array we access the first element stored in the array not the addrees so it is allowed ? – mera Aug 23 '18 at 13:14
  • also i incremented it twice becouse the two cases if the number is equal or not it should pass to the next element – mera Aug 23 '18 at 13:19
  • The branch should skip `add numofocc, 1` and the first `add array, 2` can be removed, because you must always advance the array pointer. – Weather Vane Aug 23 '18 at 13:28
  • sorry , what is the meaning of The branch should skip add numofocc, 1? – mera Aug 23 '18 at 13:35
  • The loop does 2 things. If the element value matches, you should increment the counter but if not the branch test should skip over that one instruction with `jne` not with `je`. Then the array pointer is advanced. – Weather Vane Aug 23 '18 at 13:37
  • `add array, 2` increments the first element (in MASM, a label is a memory reference even without `[]`, unless you use `mov edx, OFFSET array`. Unlike in NASM). You can't change label addresses at runtime. Get the address in a register. Unless you have some unusual macro definitions, this won't assemble because `maxi` and `[array]` are both memory locations. There's no `cmp immediate, [memory]`, only [`cmp [mem], imm`](http://felixcloutier.com/x86/CMP.html), so `maxi` can't be an `equ` assemble-time constant. – Peter Cordes Aug 23 '18 at 14:36
  • [Test if value in EAX is the same as any value in a array x86](https://stackoverflow.com/q/7736884) shows how to loop over an array. It's looking for the *first* match, but skipping over an `add` isn't your problem; everything else is, and that question covers it. – Peter Cordes Aug 23 '18 at 14:44

0 Answers0