fork download
  1. #include <iostream>
  2.  
  3. int main() {
  4. int arr[] = {43, -25, 14, 16, -25, 78, 69, 96, -18, 23};
  5. const int size = sizeof(arr) / sizeof(arr[0]);
  6. int even_count = 0;
  7.  
  8. __asm__ (
  9. "movq $0, %%rcx \n\t"
  10. "movl $0, %%eax \n\t"
  11. "loop_start: \n\t"
  12. "cmpq %[size], %%rcx \n\t"
  13. "jge loop_end \n\t"
  14. "movl (%[arr], %%rcx, 4), %%ebx \n\t"
  15. "andl $1, %%ebx \n\t"
  16. "jz even_number \n\t"
  17. "jmp next_iteration \n\t"
  18. "even_number: \n\t"
  19. "incl %%eax \n\t"
  20. "next_iteration: \n\t"
  21. "incq %%rcx \n\t"
  22. "jmp loop_start \n\t"
  23. "loop_end: \n\t"
  24. "movl %%eax, %[count] \n\t"
  25. : [count] "=r" (even_count)
  26. : [arr] "r" (arr), [size] "r" ((long)size)
  27. : "rax", "rbx", "rcx"
  28. );
  29.  
  30. std::cout << "Number of even elements: " << even_count << std::endl;
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5240KB
stdin
Standard input is empty
stdout
Number of even elements: 5