mac下安装nasm编译汇编语言

1. 下载nasm

brew install nasm

2. 检查是否下载成功

nasm -version
返回:NASM version 2.15.05 compiled on Nov 14 2020        

3. 编写打印helloworld汇编语言

section .data
    ;(数据段)放置变量
    ;字符串常量
    string DB 'Hello World!'
    ;字符串长度,$表示string_len的偏移地址-string的偏移地址,就是string长度
    string_len DB $ - string

section .bss
    ;bss段(也是存放变量,不过不知道与data有何区别)

section .text
    ;(代码段)放置代码

;定义入口函数
global _MAIN

_MAIN:
	;rax、rdi、rsi、rdx分别存放系统调用号、类型(输出)、字符串地址、字符串长度
    MOV rax,0x2000004
    MOV rdi,1
    MOV rsi,string
    MOV rdx,string_len
    syscall
    ;退出,rax、rdi分别存放系统调用号、类型(退出)
    mov rax,0x2000001
    mov rdi,0
    syscall

4. 汇编代码

nasm -f macho64 -o helloworld.o helloworld.asm

5.链接动态库

ld -macosx_version_min 11.0.1 -o helloworld -e _main helloworld.o -lSystem

  • mac bigsur下链接动态库与普通方式不同

ld -o helloworld -e _main helloworld.o -macosx_version_min 11.0.1 -L
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem

6. 运行编译后的代码

./helloworld

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值