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键

效果:

相关推荐
kebidaixu6 小时前
BCU 平台 RS485 驱动适配:从 THVD1406 到 ISO3082
linux
谢平康9 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
hj28625110 小时前
Linux 网络服务综合笔记(概念 + 命令 + 实操案例)2
linux·运维·网络
what_201810 小时前
Linux 磁盘 (查看、划分、inode)
linux·运维·服务器
27399202911 小时前
GDB调试(Linux)
linux
凡人叶枫11 小时前
Effective C++ 条款23:宁以 non-member、non-friend 替换 member 函数
linux·开发语言·c++·嵌入式开发
不会C语言的男孩11 小时前
Linux 系统编程 · 第 4 章:文件属性与元数据
linux·c语言·开发语言
小生不才yz12 小时前
Shell脚本精读 · S02-03 | 词拆分、通配符与未加引号的变量
linux
2601_9618454212 小时前
法考真题及答案解析|历年真题|资料已整理
linux·windows·ubuntu·macos·centos·gnu
A_humble_scholar12 小时前
Linux(七)调度器:从硬件矛盾到进程切换的底层逻辑
linux·服务器·网络