How to know some info?

View: New views
10 Messages — Rating Filter:   Alert me  

How to know some info?

by johncaponski :: Rate this Message:

| View Threaded | Show Only this Message

Hi everyone! I've got an AT&T GNU assembler code produced by the compilation of a C program that I don't know. I need to answer some questions about it. The thing is that I want to know how could I get that information using gcc compiler, I mean, I just want to know the options I should use with gcc and "myfile.s" to get that info.

The questions are:

- What architecture was this code compiled for?
- 32 or 64 bits?

In addition to that, I am asked to write the C program that generated that code. Is there any automatic way to do that?
This is the code:

       .file "practica2_funcion.c"
       .text
       .p2align 4,,15
.globl funcion
       .type funcion, @function
funcion:
       pushl %ebp
       movl    %esp, %ebp
       pushl %esi
       xorl    %esi, %esi
       pushl %ebx
       .p2align 4,,7
       .p2align 3
.L2:
       leal    matriz(,%esi,4), %eax
       xorl    %ecx, %ecx
       .p2align 4,,7
       .p2align 3
.L3:
       movl    (%eax), %ebx
       addl    $512, %ecx
       leal    0(,%ebx,8), %edx
       subl    %ebx, %edx
       movl    %edx, (%eax)
       addl    $512, %eax
       cmpl    $65536, %ecx
       jne     .L3
       addl    $1, %esi
       cmpl    $128, %esi
       jne     .L2
       popl    %ebx
       popl    %esi
       popl    %ebp
       ret
       .size funcion, .­-funcion
       .comm matriz,65536,32
       .ident "GCC: (Debian 4.4.1­4) 4.4.1"
                       .section      .note.GNU­stack,"",@progbits



Thank you in advance!

Re: How to know some info?

by Andrew Haley :: Rate this Message:

| View Threaded | Show Only this Message

johncaponski wrote:

> Hi everyone! I've got an AT&T GNU assembler code produced by the compilation
> of a C programme that I don't know. I need to answer some cuestions about
> it. The thing is that I want to know how could I get that information using
> gcc compiler, I mean, I just want to know the options I should use with gcc
> and "myfile.s" to get that info.
>
> The cuestions are:
>
> - What architecture was this code compiled for?
> - 32 or 64 bits?

32-bit Intel x86.

> In addition to that, I am asked to write the C programme that generated that
> code. Is there any automatic way to do that?

Not of which I am aware.  It's not difficult, though.

Andrew.

Re: How to know some info?

by John S. Fine :: Rate this Message:

| View Threaded | Show Only this Message

There is no automatic way to write an ordinary C program to generate
specific assembler code.  GCC has inline assembler directives, so I
expect you could put the asm code directly into a .c file that would
compile as the same asm code.  But that obviously is not the intent of
the question.

What kind of test or homework was this from?  I don't think doing it for
you is appropriate.

But here is the easy part:  64 bit x86 uses rsp and rbp for stack and
frame pointers.  The other 64 bit registers are often used in their 32
bit forms in 64 bit code, either because the operation is 32 bit or
because the compiler knows the high 32 of 64 bits must be zero in that
operation.  But the stack pointer in 64 bit architecture is 64 bit.

johncaponski wrote:

> Hi everyone! I've got an AT&T GNU assembler code produced by the compilation
> of a C programme that I don't know. I need to answer some cuestions about
> it. The thing is that I want to know how could I get that information using
> gcc compiler, I mean, I just want to know the options I should use with gcc
> and "myfile.s" to get that info.
>
> The cuestions are:
>
> - What architecture was this code compiled for?
> - 32 or 64 bits?
>
> In addition to that, I am asked to write the C programme that generated that
> code. Is there any automatic way to do that?
> This is the code:
>
>        .file "practica2_funcion.c"
>        .text
>        .p2align 4,,15
> .globl funcion
>        .type funcion, @function
> funcion:
>        pushl %ebp
>        movl    %esp, %ebp
>  


Re: How to know some info?

by Ian Lance Taylor-3 :: Rate this Message:

| View Threaded | Show Only this Message

johncaponski <wolfomaster@...> writes:

> - What architecture was this code compiled for?
> - 32 or 64 bits?

>        pushl %ebp

This is 32-bit x86 code.  I don't know if there is any way to
recognize this except to, well, recognize it.


> In addition to that, I am asked to write the C programme that generated that
> code. Is there any automatic way to do that?

There are various decompiler programs which attempt to back from
object code to C, but they won't give you the C code which will
generate the exact assembler code.  I don't know of any free software
decompiler programs, but I wouldn't be surprised if there are some.

Ian

Re: How to know some info?

by johncaponski () :: Rate this Message:

| View Threaded | Show Only this Message

Thank you guys, but I am still trying to make a C program that makes the same as the asm code. Could you please help me a little? I know that it is not very difficult and I almost have it, but I am not completely sure about it.

The code works with a matrix I think and there are 2 loops, one inside the other, but I am not sure if there is any other variable/s. In one part of the code there is a SUBL instruction that I don't know why is it used for... There is also an addition of 512 into the eax register which I don't understand either.
This is what I need to know:

.L3:
       movl    (%eax), %ebx
       addl    $512, %ecx
       leal    0(,%ebx,8), %edx
       subl    %ebx, %edx
       movl    %edx, (%eax)


Thanks


johncaponski wrote:
Hi everyone! I've got an AT&T GNU assembler code produced by the compilation of a C program that I don't know. I need to answer some questions about it. The thing is that I want to know how could I get that information using gcc compiler, I mean, I just want to know the options I should use with gcc and "myfile.s" to get that info.

The questions are:

- What architecture was this code compiled for?
- 32 or 64 bits?

In addition to that, I am asked to write the C program that generated that code. Is there any automatic way to do that?
This is the code:

       .file "practica2_funcion.c"
       .text
       .p2align 4,,15
.globl funcion
       .type funcion, @function
funcion:
       pushl %ebp
       movl    %esp, %ebp
       pushl %esi
       xorl    %esi, %esi
       pushl %ebx
       .p2align 4,,7
       .p2align 3
.L2:
       leal    matriz(,%esi,4), %eax
       xorl    %ecx, %ecx
       .p2align 4,,7
       .p2align 3
.L3:
       movl    (%eax), %ebx
       addl    $512, %ecx
       leal    0(,%ebx,8), %edx
       subl    %ebx, %edx
       movl    %edx, (%eax)
       addl    $512, %eax
       cmpl    $65536, %ecx
       jne     .L3
       addl    $1, %esi
       cmpl    $128, %esi
       jne     .L2
       popl    %ebx
       popl    %esi
       popl    %ebp
       ret
       .size funcion, .­-funcion
       .comm matriz,65536,32
       .ident "GCC: (Debian 4.4.1­4) 4.4.1"
                       .section      .note.GNU­stack,"",@progbits



Thank you in advance!

Re: How to know some info?

by Andrew Haley :: Rate this Message:

| View Threaded | Show Only this Message

johncaponski wrote:
> Thanks you guys, but I am still trying to make a C program that makes the
> same as the asm code. Could you please help me a little? I know that it is
> not very difficult and I almost have it, but I am not completely sure about
> it.

Show us what you've got.

Andrew.

Re: How to know some info?

by johncaponski :: Rate this Message:

| View Threaded | Show Only this Message

This is what I have. But there are some problems, I don't know how could I know the number of parameters the function receives and the type of the one returned...

int funcion (int** matriz, int valor)
{
        int i = 0;
        int j = 0;
        int a = 2;

        for (i = 0; i < 65536; i+=512)
        {
                for (j = 0; j < 128; j++)
                {
                        matriz[i][j] = matriz[i][j] - valor;
                }
        }

        return a;
}

Andrew Haley wrote:
johncaponski wrote:
> Thanks you guys, but I am still trying to make a C program that makes the
> same as the asm code. Could you please help me a little? I know that it is
> not very difficult and I almost have it, but I am not completely sure about
> it.

Show us what you've got.

Andrew.

Re: How to know some info?

by Andrew Haley :: Rate this Message:

| View Threaded | Show Only this Message

johncaponski wrote:
> This is what I have. But there are some problems, I don't know how could I
> know the number of parameters the function receives and the type of the one
> returned...

Arguments are passed on the stack.  The assembly code you posted has no
arguments.

Int arguments are returned in eax.  As far as I can see the assembly code
doesn't return anything.

Andrew.


Re: How to know some info?

by johncaponski :: Rate this Message:

| View Threaded | Show Only this Message

As I can find the main operation is the SUBL one, between what I think is the content of the matrix, stored in ebx register
and something that uses this content that is stored int edx register, but I dont know what it is...

...
movl    (%eax), %ebx  <-- Here I store Matrix[i][j] into ebx register
addl    $512, %ecx
leal    0(,%ebx,8), %edx <-- Here I store sth in edx... WHAT!!!??
subl    %ebx, %edx
movl    %edx, (%eax) <-- Here I store the difference into Matrix[i][j]
addl    $512, %eax <-- Next position for the Matrix
cmpl    $65536, %ecx
jne     .L3
...

Andrew Haley wrote:
johncaponski wrote:
> This is what I have. But there are some problems, I don't know how could I
> know the number of parameters the function receives and the type of the one
> returned...

Arguments are passed on the stack.  The assembly code you posted has no
arguments.

Int arguments are returned in eax.  As far as I can see the assembly code
doesn't return anything.

Andrew.

Re: How to know some info?

by John S. Fine :: Rate this Message:

| View Threaded | Show Only this Message

johncaponski wrote:
> leal    0(,%ebx,8), %edx <-- Here I store sth in edx... WHAT!!!??
> subl    %ebx, %edx
>  

These two instructions together are an optimized form of
edx = 7 * ebx