'Replacing even array elements with zeros in assembler

There is the following problem: I have an array, it is necessary to give its size and then in this array for each element that has an even value, assign the value zeros, and return the modified array.

There is my C++ code:

#include <iostream>
 
int main()
{
    const int size = 10;
    int arr[size] = { 1,2,3,4,5,6,7,8,9,10 };
    for (int i = 0; i < size; i++)
    {
        if (arr[i] % 2 == 0)
            arr[i] = 0;
    }
    for (int i = 0; i < size; i++)
    {
        std::cout << arr[i] << ' ';
    }
    system("pause");
    return 0;
}

There is my nasm code:

%include "io64.inc"

section .text
global CMAIN
CMAIN:
mov DWORD size$[rbp], 10 ; size of array
 
; elements of array
mov DWORD  arr$[rbp], 1
mov DWORD  arr$[rbp+4], 2
mov DWORD  arr$[rbp+8], 3
mov DWORD  arr$[rbp+12], 4
mov DWORD  arr$[rbp+16], 5
mov DWORD  arr$[rbp+20], 6
mov DWORD  arr$[rbp+24], 7
mov DWORD  arr$[rbp+28], 8
mov DWORD  arr$[rbp+32], 9
mov DWORD  arr$[rbp+36], 10
 

mov DWORD  i$4[rbp], 0
    jmp SHORT $LN4@main
$LN2@main:
    mov eax, DWORD  i$4[rbp]
    inc eax
    mov DWORD  i$4[rbp], eax
$LN4@main:
    cmp DWORD  i$4[rbp], 10
    jge SHORT $LN3@main
    movsxd  rax, DWORD  i$4[rbp]
    mov eax, DWORD  arr$[rbp+rax*4]
    cdq
    and eax, 1
    xor eax, edx
    sub eax, edx
    test    eax, eax
    jne SHORT $LN8@main
    movsxd  rax, DWORD  i$4[rbp]
    mov DWORD  arr$[rbp+rax*4], 0
    
$LN8@main:
    jmp SHORT $LN2@main
    
$LN3@main:
    mov DWORD  i$5[rbp], 0
    jmp SHORT $LN7@main
    
$LN5@main:
    mov eax, DWORD  i$5[rbp]
    inc eax
    mov DWORD  i$5[rbp], eax
    
$LN7@main:
    cmp DWORD i$5[rbp], 10
    jge SHORT $LN6@main
    
$LN6@main:
    mov edi, eax
    lea rcx, QWORD  [rbp-32]
    mov eax, edi
    lea rsp, QWORD  [rbp+360]
    pop rdi
    pop rbp
ret

My question is: will this code work and how can it be optimized?

I just get errors like this:

C:\Users\79268\AppData\Local\Temp\SASM\program.asm:1: fatal: unable to open include file `io64.inc'
gcc.exe: error: C:\Users\79268\AppData\Local\Temp\SASM\program.o: No such file or directory

C:\Users\79268\AppData\Local\Temp\SASM\program.asm:6: error: comma, colon, decorator or end of line expected after operand
C:\Users\79268\AppData\Local\Temp\SASM\program.asm:9: error: comma, colon, decorator or end of line expected after operand
C:\Users\79268\AppData\Local\Temp\SASM\program.asm:10: error: comma, colon, decorator or end of line expected after operand

So, I install NASM and IDE SASM correctly...

I try to rewrite code and compile it in SASM:

section .data
    arr dd 1, 2, 3, 4, 5, 6, 7, 8, 9,10
    
section .text
global CMAIN
CMAIN:
    call calc
    push arr

calc:
lea rsi, [arr]
mov rcx, [10]
mov rdi, rsi
xor rbx, rbx
@@for:
  lodsq
  test al, 1
  cmovz rax, rbx
  stosq
loop @@for
ret

And I get the same error:

c:/program files (x86)/sasm/mingw64/bin/../lib/gcc/x86_64-w64
mingw32/4.8.1/../../../../x86_64-w64
mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):
crt0_c.c:(.text.startup+0x25): undefined reference to `WinMain'

Now I get the same error for updated code:

BITS 64

section .text
    global _start       
_start:  
    push arr
    lea rsi, [arr]
    mov rcx, [10]
    mov rdi, rsi
    xor rbx, rbx
    @@for:
      lodsq
      test al, 1
      cmovz rax, rbx
      stosq
    loop @@for
ret

section .data
    arr dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
$nasm -f elf *.asm; ld -m elf_i386 -s -o demo *.o
$demo
/usr/bin/timeout: the monitored command dumped core
sh: line 1: 21184 Segmentation fault      /usr/bin/timeout 10s demo

Any idea for fix and compile this programm? SASM is nit wirking on my machine, and a try to use this online NASM compilier: ASM Online compilier



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source