The following is a homework problem for an assembly language class. We're to create Y86 code by reading X86 code generated from C in gdb. The purpose of the function is to sum the elements of a linked list.
As the function currently stands, it works! The correct value is contained in the %eax register when the program terminates. Unfortunately, it is only correct due to a hack. I added a halt
instruction right at the end of the function, right before the ret
instruction. If I uncomment that, what appears to happen is that when it carries out the ret
instruction, the PC is set to 0x0. In other words, it seems to start over at the beginning, when what it should do is return to the point where it was called. It enters an infinite loop.
The code follows. It is standalone, if you have the Y86 simulator installed.
.pos 0
init: irmovl Stack, %esp
irmovl Stack, %ebp
jmp Main
Main:
irmovl ele1, %eax
pushl %eax
call sum_list
halt
sum_list:
pushl %ebp
rrmovl %esp, %ebp
irmovl $16, %edx
subl %edx, %esp
irmovl $0, %edx
rmmovl %edx, -4(%ebp)
jmp L2
L3:
mrmovl 8(%ebp), %eax
mrmovl (%eax), %eax
mrmovl -4(%ebp), %edx
addl %eax, %edx
rmmovl %edx, -4(%ebp)
mrmovl 8(%ebp), %eax
mrmovl 4(%eax), %eax
rmmovl %eax, 8(%ebp)
L2:
irmovl $0, %ecx
mrmovl 8(%ebp), %edx
subl %ecx, %edx
jne L3
mrmovl -4(%ebp), %eax
rrmovl %esp, %ebp
popl %ebp
halt #THIS DOESN'T BELONG. COMMENT OUT TO SEE BAD BEHAVIOR.
ret
#linked list
.align 4
ele1:
.long 0x00a
.long ele2
ele2:
.long 0x0b0
.long ele3
ele3:
.long 0xc00
.long 0
.pos 0x300
Stack:
Thanks for your help!
This may or may not be the cause of the error, but I think you are tearing down the stack frame of sum_list
incorrectly. You set it up as follows:
pushl %ebp
rrmovl %esp, %ebp
but then you tear it down like this:
rrmovl %esp, %ebp
popl %ebp
Notice that you copy from %esp
to %ebp
in both cases, which is not correct since the teardown is supposed to undo what the setup does. Instead, try
rrmovl %ebp, %esp
popl %ebp
or simply
leave
which does the same thing.