1.vim编写hello.asm
[section .data] ; // 数据在此
strHello db "Hello,world!", 0Ah
STRLEN equ $ - strHello
[section .text] ; // 代码在此
global _start ; // 我们必须导出 _start这个入口,以便让链接器识别
_start:
mov edx, STRLEN
mov ecx, strHello
mov ebx, 1
mov eax, 4 ; sys_write
int 0x80 ; 系统调用
mov ebx, 0
mov eax, 1 ; sys_exit
int 0x80 ; 系统调用
2.安装nasm汇编编译器
yum install nasm
3.针对不同平台进行编译连接
nasm -f elf64 hello.asm -o hello.o
4.生成可执行文件:
ld -o hello hello.o
5.运行文件:
6.解决问题:
ld: i386 架构于输入文件 hello.o 与 i386:x86-64 输出不兼容
电脑的架构是x86_64,即是64位的电脑,我-f elf 默认32位命令去编译,改成 -f elf64即可
7.相关参数说明:
-f elf【64,32】以32位,还是64位编译输出
我的系统架构是64位的
-o hello.o 指定输出的文件名是hello.o
-g 编译时生成调试信息
使用ld链接生成可执行文件
番外:
内存的最小单位是字节 byte=8bit
在内存中,指令和数据是没有任何取别的,都是二进制信息,cpu只有在工作的时候才将有的信息当作指令
有的信息当作数据,cpu根据什么将内存中的信息当作指令?cpu将cs:ip指向的内存单元中的内容当作指令。
cs:ip决定了cpu从哪里开始读取指令。