selph
selph
发布于 2021-12-30 / 726 阅读
0
1

《xchg rax,rax》片段分析0x00--清空寄存器的方法

前言

今天开始,要开始更新一个新的系列了,也是一个新的小目标---分析《xchg rax,rax》中的代码片段

这个《xchg rax,rax》是一本书,web版本是免费的,当然, 也有纸质版本,在amazon有卖

这个书的介绍是:

; 0x40 assembly riddles
0x40个汇编片段

"xchg rax,rax" is a collection of assembly gems and riddles I found over many years of reversing and writing assembly code. The book contains 0x40 short assembly snippets, each built to teach you one concept about assembly, math or life in general.
xchg rax,rax收集了我很多年来逆向或者编写过的汇编代码,这本书总共0x40个简短的汇编片段,每一个都能教会你一个关于汇编、数学或生活的概念(观念?理念?)

Be warned - This book is not for beginners. It doesn't contain anything besides assembly code, and therefore some x86_64 assembly knowledge is required.
【警告】这本书不是给初学者的,这除了代码以外没有任何东西,因此你至少要有一些x86/64汇编基础

How to use this book? Get an assembler (Yasm or Nasm is recommended), and obtain the x86_64 instruction set. Then for every snippet, try to understand what it does. Try to run it with different inputs if you don't understand it in the beginning. Look up for instructions you don't fully know in the Instruction sets PDF. Start from the beginning. The order has meaning.
如何使用本书?找一个包含x86/64指令集的汇编器(推荐Yasm或Nasm),然后对于每一个片段,试图去理解它在做什么,如果刚开始不能理解,可以尝试去用不同的输入去运行它,去查询你不太了解的指令。从头开始学习,你会发现本书的顺序是有意义的。

As a final note, the full contents of the book could be viewed for free on my website (Just google "xchg rax,rax").
最后,本书的全部内容可以在我的网站免费阅读(只需要google “xchg rax,rax”)

简单来说,就是给了0x40个片段,没有任何说明,具体啥功能要读者自己分析查阅学习来得知(还挺有趣,也许是一个不错的学习汇编的资料)


实验环境:

  • Windows10 + VS2022 + masm + x64dbg

0x00

今天开始第一个片段0x00,片段代码如下:(片段链接:xorpd | xchg rax,rax 0x00

xor      eax,eax
lea      rbx,[0]
loop     $
mov      rdx,0
and      esi,0
sub      edi,edi
push     0
pop      rbp

代码分析

loop指令

这里面有个不熟悉的指令:loop $,经查阅资料,了解到loop指令的功能:

loop后面跟一个标号,会跳转到该标号处进行执行,每次执行loop的时候会自动把rcx-1,若结果为0,则跳出循环

这里的$指的是当前指令,这里则会不停的loop直到rcx变成0,可以用于循环也可以用于延时

代码片段功能

这里排除了loop这个指令的功能外,其他指令都很好理解了

这里0x00片段的目的是展示使用7种不同的方法将寄存器清零

完整测试代码:

.code
main proc
	; 给常用寄存器赋值
	inc eax
	inc ebx
	xor	rcx, rcx
	add rcx, 10
	inc edx
	inc esi
	inc edi

	; 代码片段 0x00
	xor      eax,eax	;eax = 0,使用异或指令清空寄存器
	lea      rbx,[0]	;rbx = 0,使用lea指令清空寄存器
	loop     $			;空转循环,清空ecx寄存器
	mov      rdx,0		;rdx = 0,使用mov指令清空寄存器
	and      esi,0		;esi = 0,使用and操作清空寄存器
	sub      edi,edi	;edi = 0,使用sub操作清空寄存器
	push     0			
	pop      rbp		;rbp = 0,使用push/pop组合进行清空寄存器

	ret
main ENDP
END

参考资料

loop $作用_李洙赫老婆的博客-CSDN博客_loop命令的作用是


评论