1
[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
4

2 回答 2

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

于 2015-09-17T20:57:45.157 回答
0

由于您处于 64 位模式,您将无法使用 BIOS 功能(即int 10h指令)。即使可以,BIOS 也使用不同的寻址机制,因此尝试使用的地址Message无论如何都行不通。

另外,函数的前 3 行不会DispStr归零ax吗?(因为你乘以dh,它刚刚设置为零)

于 2013-06-27T14:27:43.847 回答