[section .data]
strHello db "Hello World"
STRLEN equ $-strHello
MessageLength equ 9
Message db "hi!!!! "
[section .text]
global main
main:
mov edx,STRLEN;
mov ecx,strHello;
mov ebx,1
mov eax,4
int 0x80
call DispStr
mov ebx,0
mov eax,1
int 0x80
DispStr:
mov ax,MessageLength
mov dh,0
mul dh
add ax,Message
mov bp,ax
mov ax,ds
mov es,ax
mov cx,MessageLength
mov ax,01301h
mov bx,0007h
mov dl,0
int 10h
ret
编译运行:
$ nasm -f elf64 helloworld.asm -o helloworld.o
$ gcc -s -o helloworld helloworld.o
helloworld.o: In function `DispStr':
helloworld.asm:(.text+0x31): relocation truncated to fit: R_X86_64_16 against `.data'
collect2: ld return 1
最佳答案
这个确切的错误发生是因为:
add ax,Message
ax
只有 16 位宽,但是 Message
是一个 64 位宽的地址,因此在重定位期间它不适合。
我已经在以下位置详细解释了这个错误:https://stackoverflow.com/a/32639540/895245
这种情况下的解决方案是使用链接描述文件,如:Using .org directive with data in .data section: In connection with ld
此存储库包含引导扇区和 BIOS 的工作示例:https://github.com/cirosantilli/x86-bare-metal-examples/tree/d217b180be4220a0b4a453f31275d38e697a99e0
关于linux - nasm,86_64,linux ,"hello world"程序。当链接时,它说 "relocation truncated to fit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17318924/