nasm x86 assembly quick reference

本文提供了一份简洁明了的x86汇编语言快速参考指南,涵盖了基本指令、寄存器使用、子程序调用及堆栈帧管理等内容,并对比了与C语言的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nasm x86 Assembly Quick Reference ("Cheat Sheet")

Instructions

MnemonicPurposeExamples
movdest,srcMove data between registers, load immediate data into registers, move data between registers and memory.mov eax,4  ; Load constant into eax
mov ebx,eax  ; Copy eax into ebx
mov [123],ebx ;  Copy ebx to memory address 123
callfuncPush the address of the next instruction and start executing func.  For local functions, you don't have to say anything special.  For functions defined in C/C++, say "extern func" first.call print_int
retPop the return program counter, and jump there.  Ends a subroutine.ret
adddest,srcdest=dest+srcadd eax,ebx ; Add ebx to eax
mul srcMultiply eax and src as unsigned integers, and put the result in eax.  High 32 bits of product go into edx.mul ebx ; Multiply eax by ebx
imuldest,srcdest=dest*srcimul ecx,3
idiv botDivide eax by bot.  Treats edx as high bits above eax, so set them to zero first!
  top = eax+(edx<<32)
  eax = top/bot
  edx = top%bot
mov eax,73; top
mov ecx,10; bot
mov edx,0
idiv ecx
jmplabelGoto the instruction label:.  Skips anything else in the way.jmp post_mem
 ...
post_mem:
cmp a,b

Compare two values.  Sets flags that are used by the conditional jumps (below).  cmp eax,10  
jl labelGoto label if previous comparison came out as less-than.  Other conditionals available are: jle (<=), je (==), jge (>=), jg (>), jne (!=), and many others.  Declare your label with a semicolon beforehand, just like in C/C++: "label:".jl loop_start  ; Jump if eax<10
push srcInsert a value onto the stack.  Useful for passing arguments, saving registers, etc.push ebp
pop destRemove topmost value from the stack.  Equivalent to "movdest,[esp]     add esp,4"pop ebp

Stack Frame

(example without ebp or local variables)
Contentsoff esp
caller's variables[esp+12]
Argument 2[esp+8]
Argument 1[esp+4]
Caller Return Address[esp]

my_sub: # Returns first argument
  mov eax,[esp+4]
  ret

(example when using ebp and two local variables)
Contentsoff ebpoff esp
caller's variables[ebp+16][esp+24]
Argument 2[ebp+12][esp+20]
Argument 1[ebp+8][esp+16]
Caller Return Address[ebp+4][esp+12]
Saved ebp[ebp][esp+8]
Local variable 1[ebp-4][esp+4]
Local variable 2[ebp-8][esp]

my_sub2: # Returns first argument
  push ebp  # Prologue
  mov ebp, esp
  mov eax, [ebp+8]
  mov esp, ebp  # Epilogue
  pop ebp
  ret

Constants, Registers, Memory

"12" means decimal 12; "0xF0" is hex.  "some_function" is the address of the first instruction of a label.
Memory access (use register as pointer): "[esp]".  Same as C "*esp".
Memory access with offset (use register + offset as pointer): "[esp+4]".  Same as C "*(esp+4)".
Memory access with scaled index (register + another register * scale): "[eax + 4*ebx]".  Same as C "*(eax+ebx*4)".

Subroutines are basically just labels.  Here's how you declare labels for the linker:
  • "extern some_function;" declares some_function as being outside the current file.  You'll get a "symbol undefined" compile error if you call or jump to a label you never declare.  In C++, be sure to declare the corresponding function as being 'extern "C"'!
  • "global my_function;" exposes the label my_function so it can be called from outside.  (In MASM, it's "PUBLIC my_function").  Again, your C++ prototype better be 'extern "C"'!
Differences with C:
  • "010" means decimal ten in NASM, but *octal* eight in C/C++!  Write octal by ending with letter 'o', like "10o".
  • In NASM, you can write binary constants by ending with the letter 'b', like "mov eax,00101111b;".
  • "1+(7<<13)/15" is evaluated at compile time, and it's a constant.  "3+eax" can't be evaluated in NASM--it's not a constant.

Registers

esp is the stack pointer
ebp is the stack frame pointer
Return value in eax
Arguments are on the stack
Free for use (no save needed):
   eax, ecx, edx
Must be saved:
   ebp, esp, esi, edi
ebx must be saved in a shared library, but is otherwise free for use.
8 bit: ah (high 8 bits) and al (low 8 bits)
16 bit: ax
32 bit: eax
64 bit: rax

Pretty much this same syntax is used by  NASM  (portable x86 assembler for Windows/Linux/whatever),  YASM  (adds 64-bit support to NASM),  MASM (the Microsoft/Macro Assembler), and the official Intel documentation below.  See the  NASM documentation  or  MASM documentation  for details on constants, labels and macros.  Paul Carter has a  good x86 assembly tutorial  using the Intel syntax.  The other, nastier syntax out there is the  AT&T/GNU syntax , which I can't recommend.  The machine code in all cases is identical.

The Intel  Software Developer's Manuals  are incredibly long, boring, and complete--they give all the nitty-gritty details.  Volume 1  lists the processor registers in Section 3.4.1.  Volume 2  lists all the x86 instructions in Section 3.2.   Volume 3  gives the performance monitoring registers in Section. For Linux, the  System V ABI  gives the calling convention on page 39. Also see the Intel  hall of fame  for historical info.   Sandpile.org  has a good opcode table.

  Ralph Brown's Interrupt List  is the aging but definitive reference for all PC software interrupt functions.  See just the  BIOS interrupts for interrupt-time code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值