跳到主要内容

汇编语言

Core

  • Instruction
  • Register
  • Memory
  • Assembler

Assembly Language Syntax

  • Intel
  • AT&T

Assembler

  • GAS(AT&T)
  • NASM(Intel)
  • MASM(Intel)

Linux Include

  • /usr/include/asm/
    • /usr/include/asm/unistd.h
    • /usr/include/asm/unistd_32.h
    • /usr/include/asm/unistd_64.h
  • /usr/include/asm-generic

Calling Conventions

Instruction Type

  • Arithmetic/Logic
  • Control-Flow
  • Data Movement

Instruction

Instruction=Action
SYSCALLx86-64系统调用指令
SYSENTERx86-32系统调用指令
INT 0x80
CALLpush [return address];jmp [function address]将返回地址压入栈,并将调用的地址传给EIP
RETpop EIP将栈中的返回地址传给EIP
JMP(Jump)
CMP(Compare)
JCONDITION(Conditional Jump):JE/JNE/JZ/JG/JGE/JL/JLE
PUSH(push stack)add ESP,-4;mov ESP op;
POP(pop stack)mov location ESP;add ESP,4;
MOV(move)
LEA(load effictive address)
ADD(Integer Addition)
SUB(Integer Subtraction)
INC(Increment)
DEC(Decrement)
IMUL(Integer Multiplication)
IDIV(Integer Division)
AND/OR/XOR
NOT(Bitwise Logical Not)
NEG
SHL/SHR

NASM

Data Type

NameSize
BYTE8Bits
WORD2Bytes
DWORD4Bytes
QWORD8Bytes
TWORD
OWORD
YWORK
ZWORD

Pseudo-Instructions

InstructionType
DBBYTE
DWWORD
DDDWORD
DQQWORD
DTTWORD
DOOWORD
DYYWORD
DZZWORD
RESB
RESW
RESD
RESQ
REST
RESO
RESY
RESZ
INCBIN
EQU
TIMES/DUP
?

Template

NASM

;[label:] instruction [operands] [; comment]
section .text
global _start
_start:

section .data
section .bss
nasm -f elf64 -o app.o app.asm
ld -o app app.o

Reference

Hello World

; filename: hello.asm
section .text
global _start ;must be declared for linker (ld)
_start:
mov edx,len
mov ecx,msg
mov ebx,1 ; file descriptor
mov eax,4 ; syscall sys_write
int 0x80 ; call kernel
mov ebx,0 ; process's exit code
mov eax,1 ; syscall sys_exit
int 0x80 ; call kernel

section .data
msg db 'Hello, world!', 0xa ; msg是一个字符串常量,值为”Hello,world\n”
len equ $ - msg ; len是一个数字常量,值为msg的长度