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键

效果:

相关推荐
2401_834636994 分钟前
Linux 负载均衡全实战:Nginx+HAProxy+LVS 从原理到落地
linux·nginx·负载均衡
鹏大师运维5 小时前
为什么信创电脑装软件总提示“软件包架构不匹配”?
linux·运维·架构·国产化·麒麟·deb·统信uos
鹤落晴春7 小时前
【Linux复习】管理SELinux安全性
linux·运维·服务器
yz_aiks7 小时前
Linux Jar包配置Systemd自启动实战:从排查到配置全流程
linux·python·jar·自启动·systemd
bjzhang759 小时前
CentOS下安装MySQL详解
linux·mysql·centos
Jason_chen10 小时前
Linux 6.2 音频机制深度解析:AI驱动的低延迟音频与零信任音频安全架构
linux
下午写HelloWorld10 小时前
Linux系统及Ubuntu常用指令
linux·ubuntu·操作系统
weixin_5231853212 小时前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
凡人叶枫13 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
云栖梦泽13 小时前
玩转RK3506SDK
linux·嵌入式硬件