简体   繁体   中英

Assembly Code to C, what are the arguments in the C code that will make the Assembly code

So I just signed up for this online course, and this was part of my first assignment, I have already found the missing pieces in the assembly code and have gotten this far.

This is the assembly code:

0x08048394 <call1+0>:  push   %ebp
0x08048395 <call1+1>:  mov    %esp,%ebp
0x08048397 <call1+3>:  sub    $0x10,%esp
0x0804839a <call1+6>:  mov    %ebx,(%esp)
0x0804839d <call1+9>:  mov    %esi,0x4(%esp)
0x080483a1 <call1+13>: mov    0x8(%ebp),%edx
0x080483a4 <call1+16>: mov    0xc(%ebp),%ecx
0x080483a7 <call1+19>: mov    (%ecx,%edx,4),%eax
0x080483aa <call1+22>: mov    0x10(%ebp),%ebx
0x080483ad <call1+25>: mov    (%ebx,%edx,4),%esi
0x080483b0 <call1+28>: cmp    %esi,%eax
0x080483b2 <call1+30>: jle    0x80483b9 <call1+37>
0x080483b4 <call1+32>: mov    %eax,(%ebx,%edx,4)
0x080483b7 <call1+35>: jmp    0x80483be <call1+42>
0x080483b9 <call1+37>: mov    %esi,(%ecx,%edx,4)
0x080483bc <call1+40>: mov    %esi,%eax
0x080483be <call1+42>: pop    %ebx
0x080483bf <call1+43>: pop    %esi
0x080483c0 <call1+44>: add    $0x8,%esp
0x080483c3 <call1+47>: leave
0x080483c4 <call1+48>: ret

My question is, what arguments in the following C code snippet will lead to the above assembly code:

int main(){

int a1[] = {10, 12, 3, 4, 25};

int a2[] = {9, 28, 7, 16, 5};

call1(_________________________________);

}

I think its just a1 and a2 but I am not sure which is why I need some help. This assembly code to me looks like it may just be swapping the values of the two arrays... Am I right, or completely off?

As I said in my comment above, this question is ill-formed: "what arguments in the following C code snippet will lead to the above assembly code?" - any argument will lead to that code. That is the code of the function itself, it will always be the same no matter what arguments you pass to it. If you want to figure out which arguments are passed, you need to look at the assembly code of the caller ( main ).

However, even without the full code of main , with the part of C source that you have and the assembly of the function we can infer the following:

  1. The function is passed 3 arguments, as we can see it referencing 0x8(%ebp) , 0xc(%ebp) and 0x10(%ebp) . These arguments are in order first, second and third.

  2. The first argument (offset 0x8 from ebp ) is used as an index, as we can see from:

     mov 0x8(%ebp),%edx mov 0xc(%ebp),%ecx mov (%ecx,%edx,4),%eax
  3. The other two arguments (offsets 0xc and 0x10 ) are treated as pointers to arrays and indexed with the first.

Given the above, a fair reconstruction of the code would be the following:

int call1(int index, int *a1, int *a2) {
    int eax, esi;

    eax = a1[index];
    esi = a2[index];

    if (eax <= esi) {
        a1[index] = esi;
        eax = esi;
    } else {
        a2[index] = eax;
    }

    return eax;
}

This assembly code to me looks like it may just be swapping the values of the two arrays... Am I right, or completely off?

Yep, that seems right to me. Of course in reality we do not know what is actually passed to the function, but if the call made in main is the following:

call1(some_index, a1, a2);

Then the function takes a1 , a2 and some index, and it checks if the element of a1 at the given index is lower than or equal to the element of a2 at the same index. If so, the first element is overridden by the second, otherwise the second element is overridden by the first. In any case, the function is returning the value of the largest element.

Note that we actually have no idea if the first argument passed is a1 or a2 . It could be either way, or it could even be a1 + something and a2 + something_else . What the exact parameters are can only be determined by looking at the full code (C or assembly) of main !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM