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键

效果:

相关推荐
爱和冰阔落8 小时前
【Linux网络】GET 和 POST 真的只是参数位置不同吗?从 HTTP 请求方法、抓包分析到长连接与路由实战
linux·c++·http·网络编程·get与post
无足鸟ICT8 小时前
【RHCA+】_proc_pid_attr_exec
linux·运维·服务器
牙牙7058 小时前
接口一直超时,后端却查不出问题:TIME_WAIT 把本地端口耗尽了
linux·网络·tcp·连接池·故障排查·time_wait
智商偏低9 小时前
创建的 conda 环境如何迁移到领一个离线电脑上,后面随着开发需要往离线电脑上拷贝包
linux·人工智能·conda
Johny_Zhao20 小时前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
FlightYe1 天前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
by209991 天前
Linux 文件描述符与重定向
linux·服务器
橙橙笔记1 天前
Linux基础与命令的学习
linux·运维·服务器·shell
无奈笑天下1 天前
银河麒麟桌面操作系统【命令行配置文件与图片搜索功能】操作方法
linux·运维·服务器
飞花漫卷,情随岁迁1 天前
网络数据包转发与内核机制:小白概念科普
linux·运维·网络·数据库·mysql·oracle·智能路由器