Search code examples
assemblyy86

Y86, getting an ADR. Dont understand why


So basically learning y86 here. I have this C code and im trying to translate to y86 assembly. I keep getting this error:

PC = 0x2058a090, Invalid instruciton address
Stopped in 2 steps at PC = 0x2058a090. Exception 'ADR', CC Z=1, S=0, O=0
Changes to registers:
%esp: 0x00000000    0x00000004

changes to memory:

And basically im trying to translate this c code:

int main(){
int i, j;
i = 0;
j = 0;

if(i>j){
    i=i+5;
}
else{
    i = 0;
    j++;
}
}

To Y86 code:

main:
pushl %ebp
rrmovl %esp, %ebp
mrmovl 2(%ebp), %esi                #esi = 1
mrmovl 4(%ebp), %ecx                #ecx = i
mrmovl 8(%ebp), %edx                #edx = j
mrmovl 12(%ebp), %ebx               #ebx = 5
irmovl $1, %esi
irmovl $0, %ecx                 # i = 0 (ecx) 
irmovl $0, %edx                 # j = 0 (edx
irmovl $5, %ebx                     
subl %edx, %ecx
irmovl $0, %ecx
jg L4

L2:      #else statement
irmovl $0, %ecx                 #i = 0
addl %esi, %edx 
rmmovl %edx, 8(%ebp)                #new value of j to j memory
rmmovl %ecx, 4(%ebp)                #new value of i to i memory
je End


L4:      #if statement
addl %ebx, %ecx                 #add 5(ebx) to i(ecx)
rmmovl %ecx, 4(%ebp)                #assign new value of i
je End

End:
rrmovl %ebp, %esp
popl %ebp
ret

can anyone tell me where the error is coming from? I know ADR means that the program is trying to write or read from an invalid memory address. Thanks!


Solution

  • Seems your stack is not properly setup, so you need to initialize ESP first and make sure that the ret after the function is finished, is jumping somehwere which can terminate the program.

    In your error you can see that esp = 0