配置开发环境
方法一:
- 打开命令行,输入指令
which nasm
查看nasm的安装路径,Mac系统默认安装了nasm.一般默认返回的路径是/usr/bin/nasm - 接着输入指令
alias nasm=/usr/bin/nasm
- 输入指令
nasm -v
如果配置成功的话,就会返回nasm的版本号NASM version 2.14.02 compiled on Dec 27 2018
,
如果配置失败就会返回:macs-MacBook-Pro:~ mac$ nasm nasm: error: unable to find utility "nasm", not a developer tool or in PATH
方法二:
如果第一种方法配置失败了,那么考虑第二种方法。重装nasm
brew reinstall nasm
测试helloworld代码
- 命令行输入指令
vim helloworld.asm
,新建helloworld文件,文件类型为asm
helloworld汇编代码
SECTION .data
msg: db "Hello World!", 0x0a
len: equ $-msg
SECTION .text
global _main
kernel:
syscall
ret
_main:
mov rax,0x2000004
mov rdi,1
mov rsi,msg
mov rdx,len
call kernel
mov rax,0x2000001
mov rdi,0
call kernel
然后输入如下指令
nasm -f macho64 -o helloworld.o helloworld.asm
ld -o helloworld -e _main helloworld.o -macosx_version_min 10.13 -lSystem
./helloworld
运行结果
Hello World!
参考文献
- https://www.zhihu.com/question/29960200
- http://type.so/assembly/mac-nasm-helloworld.html
- https://*.com/questions/52830484/nasm-cant-link-object-file-with-ld-on-macos-mojave
- https://*.com/questions/53974320/how-can-i-get-nasm-to-work-as-a-command-in-terminal-in-macos
- http://caswenson.com/2009_09_26_assembly_language_programming_under_os_x_with_nasm
- http://www.ruanyifeng.com/blog/2018/01/assembly-language-primer.html