Search code examples
cgccassemblyx86att

assembly, two functions in 1 file, getting Error: junk `(%ebp)' after expression


The assembly functions with commented c version:

/*
int f (int x) 
{ 
    return x+2; 
}

void map2 (int* one, int * another, int n) 
{
    int i;
    for (i=0; i<n; i++)
      *(another+i) = f(*(one+i));
}
*/

.text

.globl f
f:

/********************************** prologue *************************************/
    pushl   %ebp        
    movl    %esp, %ebp          
/********************************************************************************/

    movl    8(%ebp), %eax
    addl    $2, %eax

/************************************* end **************************************/
    movl    %ebp, %esp
    popl    %ebp
    ret
/*********************************************************************************/

.text

.globl map2
map2:
/********************************** prologue *************************************/
    pushl   %ebp        
    movl    %esp, %ebp
    pushl   %ebx
    pushl   %esi
    pushl   %edi            
/********************************************************************************/

    movl    $0, %ebx
L1: cmpl    $16(%ebp), %ebx         // error
    jge     out

    movl    $12(%ebp), %esi         // error
    addl    %ebx, %esi  

    movl    $8(%ebp), %edi          // error
    addl    %ebx, %edi

/********************************** calls f *************************************/
    pushl   %eax
    pushl   %ecx
    pushl   %edx

    pushl   (%edi)
    call    f
    movl    %eax, (%esi)    /* *(another+i) = f(*(one+i));              */

    popl    %edx
    popl    %ecx
    popl    %eax
/********************************************************************************/

/************************************* end **************************************/
    popl    %edi
    popl    %esi
    popl    %ebx
    movl    %ebp, %esp
    popl    %ebp
    ret
/********************************************************************************/

I'm getting

fmap2.s:44: Error: junk `(%ebp)' after expression
fmap2.s:47: Error: junk `(%ebp)' after expression
fmap2.s:50: Error: junk `(%ebp)' after expression

Why? There is something to do with having 2 prologues and endings in the same file? The base-dislocation numbers of those error lines are correct.


Solution

  • According to gcc documentation, you need to remove dollar signs in front of the numeric constants used for displacement:

    One point to bear in mind is that, when a constant is used for disp/scale, ’$’ shouldn’t be prefixed.

        movl    $0, %ebx
    L1: cmpl    16(%ebp), %ebx
        jge     out
    
        movl    12(%ebp), %esi
        addl    %ebx, %esi  
    
        movl    8(%ebp), %edi
        addl    %ebx, %edi