首页 > 解决方案 > 尝试使用 GCC 编译 x86 程序集,但收到有关缺少括号和“表达式后出现垃圾”的错误

问题描述

完全公开以下代码用于家庭作业,但我编写的代码只需要帮助弄清楚为什么我在尝试编译它时会遇到几个错误(我的教授从未谈论过 GCC 错误)。

我写了一个函数,它返回一个大小为 10 的数组的两个最大成员之和。我标记了第 70 行和第 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

我收到以下错误消息:

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

感谢您的帮助,并让我知道将来如何改进我的问题

标签: gccassemblyx86gnu-assembler

解决方案


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

推荐阅读