x86 汇编语言介绍001

1,搭建编程环境

1.1 NASM 基本信息

示例使用的汇编器为 nasm

主页:

https://www.nasm.us/https://www.nasm.us/

下载最新的稳定版源代码

复制代码
wget https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/nasm-2.16.01.tar.gz

1.2解压并编译安装

复制代码
tar zxf nasm-2.16.01.tar.gz
cd cd nasm-2.16.01/
./configure
make -j
sudo make install

执行make -j前的文件夹内容:

执行make -j 后的文件夹内容:

安装

不安装的话也可以使用,只是每次需要跑到这个nasm的文件夹里来使用它。

简单测试:

2,示例测试 Hello World x86

2.1 源代码 及说明:

源码文件名可能有所改动,可以调整回来。

复制代码
;assembly language hello world
;hello_asm_lan_world.asm
section .data
        msg db "hello asm lan world!", 0
section .bss
section .text
        global main
main:
        mov rax, 1      ;
        mov rdi, 1      ;
        mov rsi, msg    ;
        mov rdx, 20     ;
        syscall
        mov rax, 60     ;
        mov rdi, 0      ;
        syscall

2.2 汇编,链接并执行

汇编命令:

复制代码
nasm hello_asm_lan_world.asm -f elf64 -g -l hello_asm_lan_world.lst

由于nasm只是个汇编器,会把汇编语言翻译成机器码,生成 *.o 文件,还没有链接,于是不能执行;

链接:

复制代码
$ gcc -o hello_asm_lan_world hello_asm_lan_world.o -no-pie

运行:

写一个Makefile:

复制代码
all: hello_asm_lan_world

hello_asm_lan_world: hello_asm_lan_world.o
        gcc -o $@ $< -no-pie

hello_asm_lan_world.o: hello_asm_lan_world.asm
        nasm $< -f elf64 -g -l hello_asm_lan_world.lst -o $@

.PHONY: clean

clean:
        -rm -f hello_asm_lan_world *.o *.lst

规则行的起始处要用tab键

效果:

相关推荐
Rabbit_QL1 小时前
【ln -s】Linux 软链接在大模型部署中的应用
linux·运维·服务器
坤昱2 小时前
cfs调度类深入解刨——核心结构细节分析
linux·cfs调度·eevdf调度·linux调度·linux技术
枳实-叶2 小时前
【Linux驱动开发】第12天:Linux设备树核心:树形结构+节点+属性 完整全解
linux·运维·驱动开发
Yeats_Liao2 小时前
物联网接入层技术剖析(三):epoll在JVM中的映射
java·linux·jvm·人工智能·物联网
小贾要学习2 小时前
【Linux】基于自定义TCP协议的日期计算器
linux·网络·c++·网络协议·tcp/ip
2501_920047033 小时前
iptables防火墙
linux·运维·网络安全
带土13 小时前
7. 线程编程(线程概念和创建)
linux
华清远见IT开放实验室3 小时前
硬核根基,智能载体:华清远见嵌入式“硬件+仿真+课程+师资”产教融合与实践教学方案
linux·人工智能·stm32·物联网·嵌入式·虚拟仿真
Anthony_2313 小时前
Linux 防火墙完全指南:从 iptables 到 firewalld
linux·运维·服务器
月走乂山3 小时前
Linux 服务器安装 CC Switch GUI 工具 + VNC 远程桌面完整教程
linux·运维·服务器