首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重定位截断为fit: R_386_16 against `.bss‘-错误

重定位截断为fit: R_386_16 against `.bss‘-错误
EN

Stack Overflow用户
提问于 2022-06-11 13:27:56
回答 1查看 55关注 0票数 1

当我试图构建程序集代码时,会遇到以下错误:

代码语言:javascript
复制
App.o: in function `_start':
App.asm:(.text+0x8c): relocation truncated to fit: R_386_16 against `.bss'

我使用以下2命令编译它:

nasm -f elf32 App.asm ld -m elf_i386 App.o /usr/local/bin/utils.o -o应用程序

然后运行App文件。

这是我的密码:

App.asm

代码语言:javascript
复制
%include 'Fun.asm'
%include 'utils.nasm'

section .data
    V   dw  2,-10,13,5,0,1,-9,3
    n   equ ($-V)/2
    d   dw  7

section .bss
    r   resw    1

section .text
global  _start
_start:
    push    V
    push    dword n
    push    word [d]
    push    r
    call    fun
    printw  r
    exit    0

Fun.asm

代码语言:javascript
复制
section .data
    VV  equ 18
    nn  equ 14
    dv  equ 12
    rr  equ 8

section .bss
    g   resb    1

section .text
global  fun
fun:
    push    ebp
    mov     ebp, esp
    pushad                  ;Inizio procedura

    mov     byte [g], 1

    mov     eax, [ebp+VV]   ;EAX(V)
    mov     bx, [ebp+dv]    ;BX(d)
    mov     edi, [ebp+nn]   ;EDI(n)
    xor     dx, dx          ;DX(Somma)

loop1:
    cmp     edi, 0          ;If n<=0 Fine
    jle     fine

    cmp     word [eax], 0   ;If V[i]==0 Cambia gruppo
    je      cmb_grp

    cmp     [eax], bx       ;If V[i]>d Esterno
    jg      esterno
    neg     bx              ;BX=-d
    cmp     [eax], bx       ;If V[i]<-d Esterno
    jl      esterno

    cmp     byte [g], 1     ;If g==1 Avanti
    je      avanti

    add     dx, [eax]       ;Somma+=V[i]
    jmp     avanti

esterno:
    cmp     byte [g], 2     ;If g==2 Avanti
    je      avanti

    add     dx, [eax]       ;Somma+=V[i]
    jmp     avanti

cmb_grp:
    mov     byte [g], 2     ;Passimo a gruppo 2
    mov     [ebp+rr], dx    ;Salviamo somma gruppo 1
    xor     dx, dx          ;Somma=0
    jmp     avanti_

avanti:
    neg     bx              ;BX=d
avanti_:
    add     eax, 2          ;i++
    dec     edi             ;n--

    jmp     loop1

fine:
    sub     [ebp+rr], dx    ;Calcolo risultato

    popad                   ;Fine procedura
    pop     ebp
    ret     14

utils.nasm

代码语言:javascript
复制
; ===============================================================================================
;
; utils.nasm
;
; ===============================================================================================
;
; Libreria di macro di utilità generale. Di seguito si riporta l'elenco dei comandi 
; disponibili, la sintassi e la relativa descrizione.
;
; -----------------------------------------------------------------------------------------------
;   printd <dword>      stampa a video una double word (registro, memoria, immediato)
;               Sintassi specifica:
;               printd dword <immediato_32_bit>
;               printd dword [<indirizzo>]
;               printd <registro_32_bit>
; -----------------------------------------------------------------------------------------------
;   printw <word>       stampa a video una word (reg, mem, imm)
;               Sintassi specifica:
;               printw word <immediato_16_bit>
;               printw word [<indirizzo>]
;               printw <registro_16_bit>
; -----------------------------------------------------------------------------------------------
;   printb <byte>       stampa a video un bye (reg, mem, imm)
;               Sintassi specifica:
;               printb byte <immediato_8_bit>
;               printb dword [<indirizzo>]
;               printb <registro_8_bit>
; -----------------------------------------------------------------------------------------------
;   prints <str>,<len>  stampa a video la stringa di lunghezza <len> 
;               posta in memoria a partire dall'indirizzo <str>
; -----------------------------------------------------------------------------------------------
;   printregs       mostra il contenuto dei registri generali a 32 bit
; -----------------------------------------------------------------------------------------------
;   print<reg>      mostra il contenuto del registro a 32 bit <reg>
;               dove <reg> in {eax, ebx, ecx, edx, esi, edi, ebp, esp}
; -----------------------------------------------------------------------------------------------
;   format_dec      imposta il formato di output a DECIMAL (default)
; -----------------------------------------------------------------------------------------------
;   format_udec     imposta il formato di output a UNSIGNE DECIMAL
; -----------------------------------------------------------------------------------------------
;   format_bin      imposta il formato di output a BINARY
; -----------------------------------------------------------------------------------------------
;   format_hex      imposta il formato di output a HEXADECIMAL
; -----------------------------------------------------------------------------------------------
;   cr_on           abilita a capo nelle stampe (default)
; -----------------------------------------------------------------------------------------------
;   cr_off          disabilita a capo nelle stampe 
; -----------------------------------------------------------------------------------------------
;   exit <code>     termina il programma con codice d'uscita <code>
; ===============================================================================================
;
; Per utilizzare i comandi nel proprio sorgente assembly:
;   1) Includere utils.nasm nel proprio file sorgente con la direttiva:
;       %sseutils "utils.nasm"
;   2) Includere nel link il file oggetto utils.o:
;       ld -m elf_i386 <file>.o utils.o -o <file>
;

section .data

seax    db  'eax = ['
leax    equ $-seax
sebx    db  'ebx = ['
lebx    equ $-sebx
secx    db  'ecx = ['
lecx    equ $-secx
sedx    db  'edx = ['
ledx    equ $-sedx
sesi    db  'esi = ['
lesi    equ $-sesi
sedi    db  'edi = ['
ledi    equ $-sedi
sebp    db  'ebp = ['
lebp    equ $-sebp
sesp    db  'esp = ['
lesp    equ $-sesp
send    db  ']'
scr     db  10
lend    equ 2


oldcr   db  1


extern format
extern cr
extern sprintd
extern buf
extern buflen


%macro  exit    1
    mov eax, 1
    mov ebx, %1
    int 80h
%endmacro


%macro  prints  2
    pushfd
    pushad
    mov eax, 4
    mov ebx, 1
    mov ecx, %1
    mov edx, %2
    int 80h
    popad
    popfd
%endmacro


%macro  printcr 0
    prints      scr, 1
%endmacro


%macro  printbuf    0
    prints  buf, [buflen]
%endmacro


%macro  cr_on   0
    mov [cr], byte 1
%endmacro


%macro  cr_off  0
    mov [cr], byte 0
%endmacro


%macro  cr_save 0
    push    eax
    mov al, [cr]
    mov [oldcr], al
    pop eax
%endmacro


%macro  cr_restore 0
    push    eax
    mov al, [oldcr]
    mov [cr], al
    pop eax
%endmacro


%macro  format_dec  0
    mov [format], byte 0
%endmacro


%macro  format_udec 0
    mov [format], byte 1
%endmacro


%macro  format_bin  0
    mov [format], byte 2
%endmacro


%macro  format_hex  0
    mov [format], byte 3
%endmacro


%macro  printd  1
    pushfd
    pushad
    push    dword 32
    push    %1
    call    sprintd
    add esp, 8
    printbuf
    popad
    popfd
%endmacro


%macro  printw  1
    pushfd
    pushad
    push    dword 16
    mov ax, %1
    movsx   eax, ax
    push    eax
    call    sprintd
    add esp, 8
    printbuf
    popad
    popfd
%endmacro


%macro  printb  1
    pushfd
    pushad
    push    dword 8
    mov al, %1
    movsx   eax, al
    push    eax
    call    sprintd
    add esp, 8
    printbuf
    popad
    popfd
%endmacro


%macro  printeax    0
    cr_save
    cr_off
    prints      seax, leax
    printd      eax
    prints      send, lend
    cr_restore
%endmacro


%macro  printebx    0
    cr_save
    cr_off
    prints      sebx, lebx
    printd      ebx
    prints      send, lend
    cr_restore
%endmacro


%macro  printecx    0
    cr_save
    cr_off
    prints      secx, lecx
    printd      ecx
    prints      send, lend
    cr_restore
%endmacro


%macro  printedx    0
    cr_save
    cr_off
    prints      sedx, ledx
    printd      edx
    prints      send, lend
    cr_restore
%endmacro


%macro  printesi    0
    cr_save
    cr_off
    prints      sesi, lesi
    printd      esi
    prints      send, lend
    cr_restore
%endmacro


%macro  printedi    0
    cr_save
    cr_off
    prints      sedi, ledi
    printd      edi
    prints      send, lend
    cr_restore
%endmacro


%macro  printebp    0
    cr_save
    cr_off
    prints      sebp, lebp
    printd      ebp
    prints      send, lend
    cr_restore
%endmacro


%macro  printesp    0
    cr_save
    cr_off
    prints      sesp, lesp
    printd      esp
    prints      send, lend
    cr_restore
%endmacro


%macro  printregs   0
    cr_save
    printcr
    printeax
    printebx
    printecx
    printedx
    printesi
    printedi
;   printebp
;   printesp
    cr_restore
%endmacro

会是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-11 13:38:38

问题是这条线:

代码语言:javascript
复制
printw  r

由于utils.asm中的宏utils.asm,该行实际上将导致以下代码:

代码语言:javascript
复制
...
mov ax, r
...

我不使用nasm,但我假设nasm将这一行解释为:

“将r地址加载到寄存器ax中。”

这是不可能的,因为地址是32位值,但是ax只有16位宽。

正如我已经写过的,我不知道nasm。因此,我无法告诉您如何将存储在r的值加载到寄存器ax中。

但是,在我所知道的其他汇编程序中,下面一行应该完成此工作:

代码语言:javascript
复制
printw [r]
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72584739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档