尝试将C函数转换为x86_64 AT&T程序集

问题描述

我一直在尝试将此功能转换为程序集:

void foo (int a[],int n) {
  int i;
  int s = 0;
  for (i=0; i<n; i++) {
    s += a[i];
    if (a[i] == 0) {
      a[i] = s;
      s = 0;
    }
  }
}

但是出了点问题。

这是我到目前为止所做的:

.section .text
.globl foo
foo:
.L1:
    pushq %rbp 
    movq %rsp,%rbp 
    subq $16,%rsp 

    movl $0,-16(%rbp) /*s*/ 
    movl $0,-8(%rbp) /*i*/

    jmp .L2

.L2:
    cmpl -8(%rbp),%esi 
    jle .L4 

    leave
    ret

.L3:
    addl $1,-8(%rbp) 
    jmp .L2

.L4:
    
    movl -8(%rbp),%eax 
    imull $4,%eax 
    movslq %eax,%rax 
    
    addq %rdi,%rax 
    
    movl (%rax),%eax 
    addl %eax,-16(%rbp) 

    cmpl $0,%eax
    jne .L3 

    /*      if      */
    leaq (%rax),%rdx 
    
    movl -16(%rbp),%eax 
    movl %eax,(%rdx) 
    movl $0,-16(%rbp) 
    jmp .L3

    

我正在使用.c模块(例如,使用int nums [5] = {65,23,11,34}来编译.s模块,并且我将返回相同的数组而不是{65,99,34}

有人可以帮我吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)