Mobyh
Mobyh

Reputation: 111

Trying to compile x86 assembly with GCC but getting errors about missing parenthesis and "junk after expression"

Full disclosure the following code is for a homework, but I wrote the code I just need help figuring out why I'm getting several errors when trying to compile it (My professor never talked about GCC errors).

I wrote a function that returns the sum of the two largest members of an array of size 10. I've marked the lines 70 and 74:

function1:
    pushl   %ebp
    pushl   %ebx
    movl    $2, %ebx #ebx will be counter
    movl    %esp, %ebp #first will be %edx and second will be %eax
    movl    8(%ebp), %edx #first = arr[0]
    movl    (%edx,1,4), %eax #second = arr[1] **LINE 70**
    cmpl    %eax, %edx #if(arr[0] > arr[1]) don't jump
    jle .L6
.L7:
    movl    (8(%ebp),%ebx,4), %ecx #%ecx = next value to compare **LINE 74**
    cmpl    %ecx, %edx #if first > next don't jump
    jle .L8
    cmpl    %ecx, %eax #if second > next don't jump
    jle .L9
    cmpl    $9, %ebx #check if counter = 9
    je  .L10
    addl    $1, %ebx #counter++ 
    jmp .L7
.L6:
    movl %edx, %ecx #move arr[0] into %ecx
    movl %eax, %edx #first = arr[1]
    movl %ecx, %eax #second = arr[0]
    jmp .L7
.L8:
    movl %edx, %eax #move previous first into second
    movl %ecx, %edx #move new first into first
    addl $1, %ebx   #counter++
    jmp .L7
.L9:
    movl %ecx, %eax #move new second into second
    addl $1, %ebx   #counter++
    jmp .L7
.L10:
    addl %edx, %eax
    popl    %ebx
    popl    %ebp
    ret

I am getting the following error messages:

assign3.s:70: Error: expecting `)' after scale factor in `(%edx,1,4)'
assign3.s:74: Error: missing ')'
assign3.s:74: Error: missing ')'
assign3.s:74: Error: junk `(%ebp),%ebx,4))' after expression

I appreciate the help and let me know how I can improve my questions in the future

Upvotes: 1

Views: 1189

Answers (1)

Mobyh
Mobyh

Reputation: 111

I had to rework my code completely but thanks to @fuz I was able to find the proper solution. My problem was both in syntax and in understanding of registers and pointers. Here's the correct code:

function1:
    #FIRST %edx
    #SECOND %eax
    #THIRD  %ebx
    #LOCATION OF ARRAY %esi
    #COUNTER %edi
    pushl   %ebp
    pushl   %ebx
    pushl   %esi
    pushl   %edi
    movl    %esp, %ebp
    movl    $1, %edi
    movl    %eax, %esi
    movl    (%esi), %edx
    movl    (%esi, %edi, 4), %eax
    addl    $1, %edi
    cmpl    %eax, %edx
    jle     .L6
.L7:
    movl    (%esi, %edi, 4), %ebx
    cmpl    %ebx, %edx
    jle     .L8
    cmpl    %ebx, %eax
    jle     .L9
.L11:
    addl    $1, %edi
    cmpl    $10, %edi
    jne     .L7
    jmp     .L10
.L6: #Switch FIRST and SECOND
    movl %edx, %ebx
    movl %eax, %edx
    movl %ebx, %eax
.L8: #THIRD is bigger than FIRST
    movl %edx, %eax
    movl %ebx, %edx
    jmp .L11
.L9: #THIRD is bigger than SECOND
    movl %ebx, %eax
    jmp .L11
.L10: #Add and return
    addl %edx, %eax
    popl    %edi
    popl    %esi
    popl    %ebx
    popl    %ebp
    ret

Upvotes: 1

Related Questions