重定位被截断以适合:R_386_8 against '.rodata'

relocation truncated to fit: R_386_8 against '.rodata'

尝试在 x86 AT&T 程序集中编译一个简单的字符串大小写交换函数时出现此错误。

我尝试查看其他出现此错误的问题,但 none 非常相似,足以帮助解决我的问题。大多数都在处理不同库和文件中的东西,这里不是这种情况。

错误:

$ gcc -m32 -g main.c test.s -o Test4
/tmp/ccQ49nKM.o: In function `scLOOP':
/home/nikolay/Dropbox/comporg/ex3/solution/test.s:24:(.text+0x14): relocation truncated to fit: R_386_8 against `.rodata'
collect2: error: ld returned 1 exit status

代码:

    .section .rodata
.align      4
error:      .string "invalid input!\n"  # error message.
a:      .byte   97          #a in ascii.

    .text

    .global swapCase        #swaps the cases of the string.
    .type   swapCase, @function
swapCase:
    pushl   %ebp            #save old FP
    movl    %esp, %ebp      #set new FP

    movl    8(%ebp), %eax       #get pointer to pstring
    movb    (%eax), %cl     #get length of pstring
    addl    , %eax        #move pointer to string itself

    scLTEST:            #loop condition.
    cmpb    [=12=], %cl         #compare length with 0
    je  scDONE          #if length is zero, goto done
    jmp scLOOP          #else: goto loop

    scLOOP:
    cmpb    $a, (%eax)      #compares first byte of string with a='a'.
    jl  scTOLOWER       #byte is uppercase.
    subb    , (%eax)     #byte is lowercase, change to uppercase.
    jmp scINC           #increment pointer.
    scTOLOWER:
    addb    , (%eax)     #change to lowercase.
    jmp scINC           #increment pointer.

    scINC:
    addl    , %eax        #increment %eax to next byte
    decb    %cl         #decrement counter
    jmp scLTEST         #end current loop iteration

    scDONE:
    movl    8(%ebp), %eax       #set return value to pointer to pstring.
    movl    %ebp, %esp      #restore old stack pointer
    popl    %ebp            #restore old FP
    ret             #return to caller function

cmpb $a, (%eax) 导致错误。我猜你不想比较 a 的内存地址和 (eax) 的值。顺便说一句:内存-内存比较在 x86 中是不可能的。我猜您想将 (eax) 处的字节与直接 ASCII 字符 'a' (cmpb , (%eax)) 进行比较 您可以替换

a:      .byte   97          #a in ascii.

来自

.equ a, 97

您不必知道 ascii table。 你可以

a: .byte 'a'