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键

效果:

相关推荐
likfishdn3 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean23 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=25 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
sz66cm2 小时前
Linux基础 -- ARM 32 位架构动态注入代码技术文档
linux·arm开发·架构
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux