2021年5月6日星期四

How can I navigate and describe what this function is trying to achieve from the given assembly code?

From my textbook, I am given the following function in x86-64 assembly:

foo:                             # line 1         movl $0, %eax             # line 2         movl $0, %r8d             # line 3         jmp .L2                   # line 4  .L3:                             # line 5         addl $1, %eax             # line 6  .L2:                             # line 7         cmpl %esi, %eax           # line 8         jge .L5                   # line 9         movslq %eax, %rcx         # line 10         cmpl %edx, (%rdi,%rcx,4)  # line 11         jne .L3                   # line 12         addl $1, %r8d             # line 13          jmp .L3                   # line 14  .L5:                             # line 15         movl %r8d, %eax           # line 16         ret                       # line 17  

So far, I think I've been able to figure out the function's C signature with the appropriate C types. Since the size specifiers are all l, I assumed it is returning an int and all of the arguments for the function are also int. Here is what I have:

int foo(int arg1, int arg2, int arg3)  

From further inspection, this function also contains a for loop. Using variable names that correspond to the register names used (e.g. using eax for %eax), here is what I think the loop structure is:

for (eax = 0; eax < esi; eax++)  

However, I am having a lot of trouble describing what this function is actually trying to accomplish. The jump instructions and the movslq %eax, %rcx are what are making me confused. Can anyone help me navigate the assembly of this function and help me understand what it is trying to achieve? I'm relatively new to assembly and I'm not fully used to reading it. Any help or suggestion would be greatly appreciated in order to increase my understanding of assembly.

https://stackoverflow.com/questions/67427462/how-can-i-navigate-and-describe-what-this-function-is-trying-to-achieve-from-the May 07, 2021 at 08:12AM

没有评论:

发表评论