How can I figure the definitions of C macro expressions in the following assembly

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m having so much difficulty with a question I was assigned for homework. I have the following C code and the subsequent assembly:

int foo(int n, int A[X(n)][Y(n)], int j){
    int i;
    int result = 0;
    for (i = 0; i < X(n); i++)
        result += A[i][j];
    return result;
}

movl    8(%ebp), %eax
leal    (%eax,%eax), %edx
leal    (%edx,%eax), %ecx
movl    %edx, %ebx
leal    1(%edx), %eax
movl    $0, %edx
testl   %eax, %eax
jle     .L3
leal    0(,%ecx,4), %esi
movl    16(%ebp), %edx
movl    12(%ebp), %ecx
leal    (%ecx,%edx,4), %eax
movl    $0, %edx
movl    $1, %ecx
addl    $2, %ebx
.L4:
addl    (%eax), %edx
addl    $1, %ecx
addl    %esi, %eax  
cmpl    %ebx, %ecx
jne     .L4
.L3:
movl    %edx, %eax

I need to find out the definitions of X and Y. I believe that n is initially stored in eax , and then 2n is stored in edx and 3n in ecx . So I think esi would equal 3n * 4. Also, because result is initially stored as movl $0, %edx and the following lines are incremented by one I m thinking that X would be equal to #define X(n + 1) . Also, I believe addl  %esi, %eax would be Y. So since esi = %ecx * 4 does Y = 4n? However, this is where I begin to get severely confused. Thank s all.

Answers

Cute exercise.

The declaration seems to define A as a C99 variable-length-array. Incidentally these have exceedingly poor compiler support and are optional in C11.

The inner Y(n) dimension may then be inferred from the array stride across loop iterations, where EAX is the pointer and ESI the pitch, and appears to be defined as n*3. As for X(n) we may infer it from the loop entry condition when i = 0, and it appears to expand as N*2+1.

#define X(n) ((n)*2+1) 
#define Y(n) ((n)*3)

Annotated assembly:

_foo:
    ;Prologue (assumed)
    push ebp
    mov ebp,esp

    ;Pre-scale N
    mov eax,[ebp+8]
    lea edx,[eax+eax]
    lea ecx,[edx+eax]   ;ECX = N*3
    mov ebx,edx         ;EBX = N*2

    ;Bail out earily if X(n) <= 0
    lea eax,[edx+1]     ;EAX = N*2+1
    mov edx,0
    test eax,eax        ;(OF=0)
    jle @@end           ;Proceed if N*2+1 > 0

    ;Prepare loop counters
    lea esi,[ecx*4]     ;ESI = N*3*sizeof int, array stride
    mov edx,[ebp+16]    ;EDX = j
    mov ecx,[ebp+12]
    lea eax,[ecx+edx*4] ;EAX = &A[0][j]

    mov edx,0           ;EDX = 0, accumulator
    mov ecx,1           ;ECX = 1, loop counter
    add ebx,2           ;EBX = N*2+2

    ;Step through the loop
@@loop:
    add edx,[eax]       ;EDX += A[i][j]
    add ecx,1           ;Increment loop counter
    add eax,esi         ;++A
    cmp ecx,ebx
    jne @@loop          ;[1..N*2+2) <=> [0..N*2+1)

@@end:
    ;Epilogue
    mov eax,edx         ;Return the sum
    pop ebp
    ret

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/20598918/how-can-i-figure-the-definitions-of-c-macro-expressions-in-the-following-assembl

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils