NASM语法

首先,nasm相关一定要多看英文手册和stackoverflow,内网啥都学不到的……

NASM Tutorial (lmu.edu)

程序结构

NASM is line-based. Most programs consist of directives followed by one or more sections. Lines can have an optional label. Most lines have an instruction followed by zero or more operands.

一般来说,代码写在.text段,全局使用的数据写在.data

常用指令

movx,y:     x←y

andx,y: x←x∧y

orx,y:  x←x∨y

xorx,y: x←x⨁y

addx,y: x←x+y

subx,y: x←x−y

incx:   x←x+1

decx:   x←x−1

syscall n:  Invoke operating system routine n

db: A pseudo-instruction that declares bytes that will be in memory when the program runs

三类操作数

寄存器操作数

x86-84架构下的16个64位integer registers:

也可以用下面的名字操作各寄存器的低32位,视作一个32位寄存器:

低16、8位也有相应的名称:

存储操作数

有如下几种寻址:

  • [ number ]
  • [ reg ]
  • [ reg + reg*scale ]      scale 只有1, 2, 4和8
  • [ reg + number ]
  • [ reg + reg*scale + number ]

The number is called the displacement(偏移); the plain register is called the base(基地址); the register with the scale is called the index.

立即操作数

1