0

If you input the array 1 2 3 4 5 -1

the output is supposed to be 15 14 12 9 5.

I'm getting seg fault. What am I doing wrong?

    .global reverse_prefix_sum

reverse_prefix_sum:

    ld r24, X+
    cpi r24, -1
    breq done2
    push r24
    call reverse_prefix_sum
    pop r22
    add r24, r22
    adc r25, r23
    st Y+, r24

    jmp 1f


done2:
    ldi r24, 0
    clr r25

1:
    ret



    .global print_array

print_array:

    push r24
    push r25
    clr r25
    ld r24, X+
    cpi r24, -1
    breq done
    call print_array

done:
    clr r25
    ldi r24, 10

2:
    ret
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

As far as I can see, your problem is in print_array. I'm not clear what it is supposed to do, since I don't see it printing anything.

That said, you push r24 and r25 at the start of print_array but then never pop them.

So when you read the -1 into r24 on the sixth iteration, you'll branch to done: and return, except instead of popping a valid code address of the stack, you'll pop whatever was in r25 and go there. That's probably not going to end well.

dgnuff
  • 3,195
  • 2
  • 18
  • 32