3

I was wondering if anyone could help me better understand both why this code that I have written does not work, and help me also to fix it. The following is the Y86 I've written which should sum the array which I've defined within it, however, all I can get my program to return is d4a instead of the expected cba.

.pos 0
init:
irmovq Stack, %rsp 
rrmovq %rsp,  %rbp

xorq   %rcx,  %rcx    #zeroing out all temp variables
xorq   %rdi,  %rdi 
xorq   %rbx,  %rbx

irmovq $8,    %rdi    #rdi has 4
irmovq ele1,  %rbx    #initialize rbx as ele1
irmovq $0,    %rcx    #sum is initially 0
call sum_list 
halt 

# Sample linked list
.align 8
ele1:
.quad 0x00a
.quad ele2
ele2:
.quad 0x0b0
.quad ele3
ele3:
.quad 0xc00
.quad 0

sum_list: 
mrmovq (%rbx),  %rax  #current rbx value into rax
addq   %rax,    %rcx  #rcx += current rbx value
addq   %rdi,    %rbx  #add 4 to rbx address
andq   %rax,    %rax  #check if previous rbx value was 0
jne    sum_list       #if it wasn't zero, restart, except rbx+4
done: 
rrmovq %rcx,    %rax  #if it was 0, move the sum to rax
rrmovq %rbp,    %rsp  #restore rsp
popq   %rbp           #and rbp
ret                   #return rax, which should be the sum of linked list
.pos 0x300 
Stack: 

Thank you ahead of time guys! Assembly is really hard to get a grasp on and it really helps when people take the time to explain these things to me!

  • What is the `andq %rax, %rax` supposed to do? – EOF Apr 12 '16 at 17:21
  • @EOF set zero flag if rax is 0 – Iłya Bursov Apr 12 '16 at 17:22
  • @Lashane but it also modifies `%rax`. Does Y86 not have `test`? – EOF Apr 12 '16 at 17:23
  • You should comment your code, especially if you want others to help. Also, learn to use a debugger so you can see where your code doesn't do what you expect. – Jester Apr 12 '16 at 17:23
  • 3
    @EOF how do you think that modifies eax? `x & x` is `x` for all `x`. – Jester Apr 12 '16 at 17:23
  • what `.quad ele2` is supposed to do? – Iłya Bursov Apr 12 '16 at 17:24
  • @Lashane it's a linked list, that's the `next` pointer. – Jester Apr 12 '16 at 17:24
  • @Jester: Ah, fair point. Anyway, I don't see where in the code the `next`-pointer is moved from `%rax` into `%rbx` to traverse the list. – EOF Apr 12 '16 at 17:25
  • @EOF that's why we need comments. Anyway, `rdi` is loaded with the constant `8`, which is the offset of the `next` pointer. Thus `addq %rdi, %rbx; mrmovq (%rbx), %rax` loads the `next` pionter. – Jester Apr 12 '16 at 17:26
  • 2
    @Jester Yes, but then the `next` pointer is in `%rax`, but the code jumps back to the loop where it dereferences `%rbx` again instead. – EOF Apr 12 '16 at 17:27
  • 1
    @EOF congrats, you found the bug :) – Jester Apr 12 '16 at 17:27
  • 1
    Wow thanks so much guys, I can see where my error is >.< Honestly this is the first time I've asked a question here and I'm really glad to have y'all help me out. I was having problems using gdb on my work computer, but I will do that next time before I bring it here. I will also comment my code next time as well. I really appreciate the help!!! – Christopher Gonzalez Apr 12 '16 at 19:52
  • Actually, I'm confused about why that is creating the problem. Yes the next pointer is in rax, however, I just andq the value. When I dereference rbx again it still should be pointing to the next element in the linked list right? I only move the value to rax because I can't andq (%rbx), (%rbx). – Christopher Gonzalez Apr 12 '16 at 20:01
  • I'm thinking that the error is relating to the fact that I add in every element to rax, and some of the elements I'm adding are just addresses to the next value in the list. Thoughts anyone? – Christopher Gonzalez Apr 12 '16 at 20:10
  • I've updated the code and commented it as well, it is still not working for me. – Christopher Gonzalez Apr 12 '16 at 20:36
  • 1
    One thing that does *not* help is making changes at random. Also, if you want to update your question, don't edit the existing code, add the new code to the bottom of the question. – EOF Apr 12 '16 at 21:21
  • I'll keep that in mind next time. I just made some minor changes to the code, like removing a repeated instruction and making the program more readable. – Christopher Gonzalez Apr 12 '16 at 22:00
  • I discovered the reason... So my code because of the structure of the list, is adding the second value twice as it hits ele2, adds 8 and hits ele2 again. ISSUE RESOLVED. – Christopher Gonzalez Apr 12 '16 at 22:38

0 Answers0