CentOS7写汇编并编译运行汇编代码

1.下载nasm编译器

下载地址是https://www.nasm.us/pub/nasm/releasebuilds/

wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz

2.解压安装nasm

tar -xvzf nasm-2.14.tar.gz 

3.进入到nasm的解压目录中编译并安装nasm

cd nasm-2.14         #进入nasm的解压目录

./configure             #配置

make       #编译

make install   #安装
    

4.一段可以输出Hello World的汇编代码

section .data                           ;section declaration
msg     db      "Hello, world!",0xA     ;our dear string
len     equ     $ - msg                 ;length of our dear string
section .text                           ;section declaration
                       ;we must export the entry point to the ELF linker or
   global _start       ;loader. They conventionally recognize _start as their
                       ;entry point. Use ld -e foo to override the default.
_start:
;write our string to stdout
       mov     eax,4   ;system call number (sys_write)
       mov     ebx,1   ;first argument: file handle (stdout)
       mov     ecx,msg ;second argument: pointer to message to write
       mov     edx,len ;third argument: message length
       int     0x80    ;call kernel
;and exit
       mov     eax,1   ;system call number (sys_exit)
       xor     ebx,ebx ;first syscall argument: exit code
       int     0x80    ;call kernel

  将它保存为HelloWorld.s文件。

5.编译该汇编代码

nasm -f elf64 HelloWorld.s -o HelloWorld.o

6.链接生成可执行文件

ld -s HelloWorld.o -o HelloWorld.out

7.执行程序

./HelloWorld.out

8.执行结果如下

[root@CentOs64-7 Assembly]# ./HelloWorld.out 
Hello, world!

  转载自:https://blog.csdn.net/weiyuanzhuo/article/details/52382611

你可能感兴趣的:(Linux)