Linux搭建汇编环境

yantao@ubuntu20:~$ sudo apt install nasm -y				#安装nasm
yantao@ubuntu20:~$ nasm -v
NASM version 2.14.02

yantao@ubuntu20:~$ mkdir asm							#创建一个工作目录
yantao@ubuntu20:~$ cd asm/

yantao@ubuntu20:~/asm$ mkdir hello						#创建hello项目
yantao@ubuntu20:~/asm$ cd hello/
yantao@ubuntu20:~/asm/hello$ vim hello.asm				#创建hello.asm文件
yantao@ubuntu20:~/asm/hello$ nasm -f elf64 hello.asm 	#编译
yantao@ubuntu20:~/asm/hello$ ld -s -o hello hello.o		#链接
yantao@ubuntu20:~/asm/hello$ ls
hello  hello.asm  hello.o
yantao@ubuntu20:~/asm/hello$ ./hello 					#运行
Hello world!
yantao@ubuntu20:~/asm/hello$ 

hello.asm的内容

section .data
  hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
  helloLen:  equ $-hello             ; Length of the 'Hello world!' string
                                     ; (I'll explain soon)
 
section .text
  global _start
 
_start:
  mov eax,4            ; The system call for write (sys_write)
  mov ebx,1            ; File descriptor 1 - standard output
  mov ecx,hello        ; Put the offset of hello in ecx
  mov edx,helloLen     ; helloLen is a constant, so we don't need to say
                       ;  mov edx,[helloLen] to get it's actual value
  int 80h              ; Call the kernel
 
  mov eax,1            ; The system call for exit (sys_exit)
  mov ebx,0            ; Exit with return code of 0 (no error)
  int 80h

你可能感兴趣的:(linux,汇编,运维)