汇编知识

1.ATT汇编

1.1 基础知识

  • Intel中,第一个操作数是目的操作数,第二个操作数是源操作数。

  • AT&T 中,第一个数是源操作数,第二个数是目的操作数

  • AT&T 的操作码后面有一个后缀,指出操作码的大小

    • “l”表示长整数(32 位),
    • “w”表示字(16 位),
    • “b”表示字节(8 位)
  • Intel 的语法中,在内存单元操作数的前面加上byte ptr、word ptr 和dword ptr,

    • “dword”对应“long”
  • "#"井号开头的是注释行

  • "."点开始的指令一般都是伪指令

  • "$"美元符号修饰立即数,十六进制立即数前冠以“0x”

  • "%"修饰寄存器

  • 地址或偏移 (%基址或偏移量寄存器, %索引寄存器, 比例因子)

    • 这种寻址方式常常用在访问数据结构数组中某个特定元素内的一个字段,其中,base 为数组的起始地址,scale 为每个数组元素的大小,index 为下标。如果数组元素还是一个结构,则disp 为具体字段在结构中的位移。
  • AT&T 的语法中,一个节由.section 关键词来标识

    • .section .data:包含程序已初始化的数据,也就是说,包含具有初值的那些变量
    • .section .bss:包含程序还未初始化的数据,也就是说,包含没有初值的那些变量
    • .section .text :包含程序的代码,它是只读节,而.data 和.bss 是读/写节。

1.2 常用指令

  • cmp S2,S1 计算S1 – S2,并根据执行结果修改标志位
  • mov S,D 将s放入d中
  • je Label 根据ZF标志进行条件跳转,如果发生跳转将跳转至label处
  • lea S,D 执行D = &S,将s的地址放入d中
  • nopl 用于在指令流中插入空闲nop指令,使指令之后的访问可以对齐
  • cmpxchg source, destination 将目的寄存器与eax寄存器的值进行比较
    • 如果相等则将源操作数放入目的操作数
    • 如果不等,将目的操作数放入eax寄存器
  • set

2.寄存器

2.1 intel架构寄存器作用

对于 Intel 架构,共有16个64位通用寄存器

  • %rax 作为函数返回值使用。
  • %rsp 栈指针寄存器,指向栈顶,%rbp 栈基地址寄存器,保存当前帧的栈底地址。
  • %rdi,%rsi,%rdx,%rcx,%r8,%r9 用作函数参数,依次对应第1参数,第2参数。。。
  • 其余被标识为 “miscellaneous registers” 的寄存器,属于通用性更为广泛的寄存器,编译器或汇编程序可以根据需要存储任何数据。
  • %rip 指令指针寄存器,用于存放下一个要执行的指令地址(专用的寄存器 )

3.c++执行过程中的内存使用

  • 在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。

    • 栈,在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。

    • 堆,就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去控制,一般一个new就要对应一个delete。如果程序员没有释放掉,那么在程序结束后,操作系统会自动回收。

    • 自由存储区,就是那些由malloc等分配的内存块,他和堆是十分相似的,不过它是用free来结束自己的生命的。

    • 全局/静态存储区,全局变量和静态变量被分配到同一块内存中,在以前的C语言中,全局变量又分为初始化的和未初始化的,在C++里面没有这个区分了,他们共同占用同一块内存区。

    • 常量存储区,里面存放的是常量,不允许修改。

4.objdump使用

  • 使用objdump在Linux环境下可以将二进制可执行文件转换为汇编文件

4.1 反汇编选项

  • 至少选择其中一个进行反汇编:
    • -a, --archive-headers Display archive header information
    • -f, --file-headers Display the contents of the overall file header
    • -p, --private-headers Display object format specific file header contents
    • -P, --private=OPT,OPT… Display object format specific contents
    • -h, --[section-]headers Display the contents of the section headers
    • -x, --all-headers Display the contents of all headers
    • -d, --disassemble Display assembler contents of executable sections
    • -D, --disassemble-all Display assembler contents of all sections
    • -S, --source Intermix source code with disassembly
    • -s, --full-contents Display the full contents of all sections requested
    • -g, --debugging Display debug information in object file
    • -e, --debugging-tags Display debug information using ctags style
    • -G, --stabs Display (in raw form) any STABS info in the file
    • -W[lLiaprmfFsoRt] or --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges,=pubtypes,=gdb_index,=trace_info,=trace_abbrev,=trace_aranges,=addr,=cu_index] :Display DWARF info in the file
    • -t, --syms Display the contents of the symbol table(s)
    • -T, --dynamic-syms Display the contents of the dynamic symbol table
    • -r, --reloc Display the relocation entries in the file
    • -R, --dynamic-reloc Display the dynamic relocation entries in the file @ Read options from
    • -v, --version Display this program’s version number
    • -i, --info List object formats and architectures supported
    • -H, --help Display this information
  • 下面的是可选反汇编选项:
    • -b, --target=BFDNAME Specify the target object format as BFDNAME
    • -m, --architecture=MACHINE Specify the target architecture as MACHINE
    • -j, --section=NAME Only display information for section NAME
    • -M, --disassembler-options=OPT Pass text OPT on to the disassembler
    • -EB --endian=big Assume big endian format when disassembling
    • -EL --endian=little Assume little endian format when disassembling
      • –file-start-context Include context from start of file (with -S)
    • -I, --include=DIR Add DIR to search list for source files
    • -l, --line-numbers Include line numbers and filenames in output
    • -F, --file-offsets Include file offsets when displaying information
    • -C, --demangle[=STYLE] Decode mangled/processed symbol names. The STYLE, if specified, can be auto',gnu’, lucid',arm’, hp',edg’, gnu-v3',java’ or `gnat’
    • -w, --wide Format output for more than 80 columns
    • -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling
      • –start-address=ADDR Only process data whose address is >= ADDR
      • –stop-address=ADDR Only process data whose address is <= ADDR
      • –prefix-addresses Print complete address alongside disassembly
      • –[no-]show-raw-insn Display hex alongside symbolic disassembly
      • –insn-width=WIDTH Display WIDTH bytes on a single line for -d
      • –adjust-vma=OFFSET Add OFFSET to all displayed section addresses
      • –special-syms Include special symbols in symbol dumps
      • –prefix=PREFIX Add PREFIX to absolute paths for -S
      • –prefix-strip=LEVEL Strip initial directory names for -S
      • –dwarf-depth=N Do not display DIEs at depth N or greater
      • –dwarf-start=N Display DIEs starting with N, at the same depth or deeper
      • –dwarf-check Make additional dwarf internal consistency checks.
  • 使用-M选项在i386/x86-64机器上指定特别选项,多个选项之间使用逗号隔开
    • x86-64 Disassemble in 64bit mode
    • i386 Disassemble in 32bit mode
    • i8086 Disassemble in 16bit mode
    • att Display instruction in AT&T syntax
    • intel Display instruction in Intel syntax
    • att-mnemonic Display instruction in AT&T mnemonic
    • intel-mnemonic Display instruction in Intel mnemonic
    • addr64 Assume 64bit address size
    • addr32 Assume 32bit address size
    • addr16 Assume 16bit address size
    • data32 Assume 32bit data size
    • data16 Assume 16bit data size
    • suffix Always display instruction suffix in AT&T syntax
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 首先,16位汇编和32位汇编是两种不同的汇编语言,它们的主要区别在于它们所支持的指令集的不同。 在保护模式下,操作系统将内存划分为多个段,每个段具有不同的特权级别和访问权限。在这种模式下,程序可以访问更大的内存空间,并且可以执行更复杂的操作。在保护模式下,编写汇编代码需要使用一些新的指令和数据类型,例如段选择器、描述符表等。 对于16位汇编,常用的编译器有MASM、TASM等。在编写汇编代码时需要注意以下几点: 1. 16位汇编程序是基于段的,需要定义段寄存器和偏移地址。 2. 16位汇编程序使用的指令集较为简单,只支持较少的指令集。 3. 在保护模式下,需要使用32位寄存器来访问段寄存器和描述符表。 对于32位汇编,常用的编译器有NASM、FASM等。在编写汇编代码时需要注意以下几点: 1. 32位汇编程序是基于平面模式的,不需要定义段寄存器。 2. 32位汇编程序使用的指令集更加丰富,支持更多的指令集。 3. 在保护模式下,需要使用32位寄存器来访问段寄存器和描述符表。 总的来说,在编写汇编代码时需要充分理解汇编语言的特点和保护模式下的内存管理机制,以及熟悉所使用的编译器和指令集。同时,需要注意代码的可读性和可维护性,尽量使用注释和模块化的代码结构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值