Search code examples
assemblyx86-64y86

Y86 Code - doesn't return or show rax


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

Solution

  • Looking at the code, it seems that the pointer to node should be at 16(rbp), not 24(rbp). 0(rbp) = saved rbp value, 8(rbp) = return address, 16(rbp) = pointer to node (to the linked list). I don't see where the extra 8 bytes are pushed onto the stack before rbp is saved.

    The program ends at a halt instruction. Are you able to determine the content of rax when this happens (such as using a debugger)?