07-24-2023, 05:50 AM
I think this is actually a pretty simple problem. I have to reverse engineer this assembly code to c code. I'll also provide what I think is going on so you can hopefully point to where I went wrong and I can learn from my mistakes now.
.LFBO
pushq %rbp
movq %rsp,%rbp
movl %edi,-4(%rbp)
movl %esi,-8(%rbp)
movl -4(%rbp),%eax
compl -8(%rbp),%eax
jg .L2
movl -8(%rbp),%eax
jmp .L3
.L2:
movl -4(%rbp),%eax
.L3:
popq %rbp
ret
So this is what I think is going on with this:
the first two lines after .LFBO:
pushq %rbp
movq %rsp,%rbp
are just setting up the stack for the execution that is about to follow.
movl %edi,-4(%rbp)
is grabbing the first variable, call it x
movl %esi,-8(%rbp)
is grabbing the second variable call it y
movl -4(%rbp),%eax
is grabbing x to be compared in the next line
compl -8(%rbp),%eax
compares the variables x and y by computing x-y
jg .L2
says jump to .L2 if x > y
if x <= y then compute the next lines without jumping to .L2
movl -8(%rbp),%eax
copy x = y
jmp .L3
jump to .L3
if x > y at the jg line then you jump to .L2: and complete this line
movl -4(%rbp),%eax
this is where I realized I was really confused. It looks to me that you're copying x to x
then .L3 is completed and I think x is returned
.LFBO
pushq %rbp
movq %rsp,%rbp
movl %edi,-4(%rbp)
movl %esi,-8(%rbp)
movl -4(%rbp),%eax
compl -8(%rbp),%eax
jg .L2
movl -8(%rbp),%eax
jmp .L3
.L2:
movl -4(%rbp),%eax
.L3:
popq %rbp
ret
So this is what I think is going on with this:
the first two lines after .LFBO:
pushq %rbp
movq %rsp,%rbp
are just setting up the stack for the execution that is about to follow.
movl %edi,-4(%rbp)
is grabbing the first variable, call it x
movl %esi,-8(%rbp)
is grabbing the second variable call it y
movl -4(%rbp),%eax
is grabbing x to be compared in the next line
compl -8(%rbp),%eax
compares the variables x and y by computing x-y
jg .L2
says jump to .L2 if x > y
if x <= y then compute the next lines without jumping to .L2
movl -8(%rbp),%eax
copy x = y
jmp .L3
jump to .L3
if x > y at the jg line then you jump to .L2: and complete this line
movl -4(%rbp),%eax
this is where I realized I was really confused. It looks to me that you're copying x to x
then .L3 is completed and I think x is returned