0

I am doing a class project which I am to take C code, turn it in x86-64 assembly and then change it to Y86. An in this I am suppose to return the sum of the elements in a linked list to to rax. However, when i try to use the y86 compiler, it doesn't appear. The y86 I made looked like this:

.pos 0
irmovq Stack,%rsp
irmovq Stack,%rbp
jmp Main

Main:
        irmovq ele1,%rax
        pushq %rax
        call sum_list
        halt

sum_list:
        pushq %rbp
        rrmovq %rsp,%rbp
        irmovq $24,%rdx
        subq %rdx,%rsp
        irmovq $0,%rdx
        rmmovq %rdx,-8(%rbp)
        jmp L2
L3:
        mrmovq 24(%rbp),%rax
        mrmovq (%rax),%rax
        mrmovq -8(%rbp),%rdx
        addq %rax,%rdx
        rmmovq %rdx,-8(%rbp)
        mrmovq 24(%rbp),%rax
        mrmovq -8(%rax),%rax
        rmmovq %rax,24(%rbp)
L2:
        irmovq $0,%rcx
        mrmovq 24(%rbp),%rdx
        subq %rcx,%rdx
        jne L3
        mrmovq -8(%rbp),%rax
        rrmovq %rbp,%rsp
        popq %rbp
        ret

#linked-list
.align 8
ele1:
        .quad 0x00d
        .quad ele2
ele2:
        .quad 0x0e0
        .quad ele3
ele3:
        .quad 0xf00
        .quad 0

.pos 0x500
Stack:

And so rax should have 0xfed, but in my result, nothing appears.

This is the C code I got it from:

typedef struct ELE{
  long val;
  struct ELE *next;
} *list_ptr

long sum_list(list_ptr ls){
  long val = 0;
  while(ls){
    val += ls->val;
    ls = ls->next;
  }
  return val;
}
4

1 回答 1

2

查看代码,似乎指向节点的指针应该在 16(rbp),而不是 24(rbp)。0(rbp) = 保存的 rbp 值,8(rbp) = 返回地址,16(rbp) = 指向节点的指针(指向链表)。在保存 rbp 之前,我看不到额外的 8 个字节被推入堆栈的位置。

程序以停止指令结束。当这种情况发生时,您是否能够确定 rax 的内容(例如使用调试器)?

于 2016-10-10T00:08:45.027 回答