用汇编器中的零替换数组元素

发布于 2025-01-23 19:58:25 字数 3686 浏览 0 评论 0原文

存在以下问题: 我有一个数组,有必要给出其大小,然后在此数组中,对于每个具有均值的元素,分配值零并返回修改的数组。

有我的C ++代码:

#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;
}

有我的NASM代码:

%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

我的问题是:该代码会起作用吗?

我只会得到这样的错误:

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

因此,我正确安装了NASM和IDE SASM ...

我尝试重写代码并以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

并且我遇到了相同的错误:

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'

现在,我对更新代码的错误遇到了相同的错误:

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

修复和编译此程序的想法吗? SASM在我的机器上正在固定,并尝试使用此在线nasm编译器:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文