assembly Mac OSX中的x86汇编错误

6tqwzwtp  于 2022-11-13  发布在  Mac
关注(0)|答案(1)|浏览(226)

我有下面的汇编代码,我从一个x86汇编教程在线:

section .text
   global _start     ;must be declared for linker (ld)

start:              ;tells linker entry point
   mov  edx,len     ;message length
   mov  ecx,msg     ;message to write
   mov  ebx,1       ;file descriptor (stdout)
   mov  eax,4       ;system call number (sys_write)
   int  0x80        ;call kernel

   mov  eax,1       ;system call number (sys_exit)
   int  0x80        ;call kernel

section .data
msg db 'Hello, world!', 0xa  ;string to be printed
len equ $ - msg     ;length of the string

我将上面的代码保存在文件“hello.asm”中
现在,当我在我的终端编译和链接它时,我得到以下错误!

root@mac:~# nasm -f macho hello.asm && gcc -o hello hello.o

ld: warning: ignoring file hello.o, file was built for i386 which is not the architecture being linked (x86_64): hello.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ogsagwnx

ogsagwnx1#

您的代码有效的Linux代码。但是,它不是有效的MacOS代码。这是因为系统中断int 0x80是用来调用Linux系统内核的,而不是MacOS内核。如果您使用的是基于Linux的旧版本MacOS,这可能仍然有效。
另一个问题是,这段代码是32位的,而你像编译64位代码一样编译它。要解决这个问题,你应该在gcc命令中添加-m32

相关问题