2022年识别编译后的算术操作终稿 .pdf





《2022年识别编译后的算术操作终稿 .pdf》由会员分享,可在线阅读,更多相关《2022年识别编译后的算术操作终稿 .pdf(25页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、附录二:识别编译后的算术操作 This appendix explains the basics of how arithmetic is implemented in assembly language, and demonstrates some basic arithmetic sequences and what they look like while reversing. Arithmetic is one of the basic pillars that make up any program, along with control flow and data manageme
2、nt. Some arithmetic sequences are plain and straightforward to decipher while reversing, but in other cases they can be slightly difficult to read because of the various compiler optimizations performed. 本章介绍基本的算术操作是怎样在汇编语言中实现的一些基础知识,并演示一些可能在逆向工程中经常遇到的基本的算术操作的实现指令流。算术操作和控制流、数据管理一起构成了解决任何问题的支柱。在逆向工程中
3、,某些算术操作指令流的意思是显而易见的,但要解读出另一些经过了编译器优化处理后的算术操作指令流就显得比较困难了。 This appendix opens with a description of the basic IA-32 flags used for arithmetic and proceeds to demonstrate a variety of arithmetic sequences commonly found in compiler-generated IA-32 assembly language code. 本附录从介绍最基本的IA-32标志位开始,然后介绍一些算术操
4、作在 IA-32编译器生成的汇编语言中表现的代码 Arithmetic Flags 算术标志位 To understand the details of how arithmetic and logic are implemented in assembly language, you must fully understand flags and how they re used. Flags are used in almost every arithmetic instruction in the instruction set, and to truly understand the
5、meaning of arithmetic sequences in assembly language you must understand the meanings of the individual flags and how they are used by the arithmetic instructions. 译名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 25 页 - - - - - - - - - 为了详细理解算术和逻辑指令是如何在汇编语言中实现的,
6、你应该很好的理解所有的标志位及其用途。几乎指令集中每一个算术指令都要用到标志位。而且为了真正理解汇编语言中算术指令流的真正含义,你必须理解每一个标志位及其是怎样被算术指令使用的。 Flags in IA-32 processors are stored in the EFLAGS register, which is a 32-bit register that is managed by the processor and is rarely accessed directly by program code. Many of the flags in EFLAGS are system f
7、lags that determine the current state of the processor. Other than these system flags, there are also eight status flags, which represent the current state of the processor, usually with regards to the result of the last arithmetic operation performed. The following sections describe the most import
8、ant status flags used in IA-32. IA32体系结构的计算机中,所有的标志位都被集中存放在一个 32位的“标志位寄存器”中。该寄存器是直接由处理器进行管理,并极少被程序直接访问的。“标志位寄存器”中的大多数标志位是系统标志位,它们的状态决定了处理器的当前的状态。此外还有8个(非系统的)状态标志位,这8个标志位代表了处理机当前的状态。通常这些标志位的标志着上一个算术操作的结果。下一节我们介绍IA32体系结构的计算机中最重要的状态寄存器。 The Overflow Flags (CF and OF) 溢出标志位(CF 或者OF ) The carry flag (CF)
9、 and overflow flag (OF) are two important elements in arithmetical and logical assembly language. Their function and the differences between them aren t immediately obvious, so here is a brief overview. 进位标志位(CF )和溢出标志位(OF )是在算法和逻辑汇编语言中两个重要的元素。它们之间的功能上区别不是那么显而易见,所以下面我们简单介绍一下。The CF and OF are both o
10、verflow indicators, meaning that they are used to notify the program of any arithmetical operation that generates a result that is too large in order to be fully represented by the destination operand. The difference between the two is related to the data types that the program is dealing with. CF 和
11、OF 都是溢出指示器,这就是说这两个标志位的作用是:通知程序或算术指令,其执行后得到的结果太大了,无法记录在目标操作数中。这两个标志位使用上的区别在于CF 是针对无符号数的,而OF 是针对有符号数的。 Unlike most high-level languages, assembly language programs don t explicitly specify the details of the data types they deal with. Some arithmetical instructions such as ADD (Add) and SUB (Subtract)
12、 aren t even aware of whether the operands they are working with are signed or unsigned because it just doesn t matterthe binary result is the same. Other instructions, such as MUL (Multiply) and DIV (Divide) have different versions for signed and unsigned operands because multiplication and divisio
13、n actually produce different binary outputs depending on the exact data type. 不像高级语言那样,汇编语言程序员不必显式的指明和关心当前处理的数据是什么数据类型的。一些算术指令如:ADD 、SUB 也不关心操作数到底是有符号数还是无符号数,因为这对指令执行的结果毫无影响二进制结果是一样的。但另一些指令如MUL 、DIV对有符号数和无符号数有不同的版本因为这时不同的数据类型会对指令执行的结果有影响。 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心
14、整理 - - - - - - - 第 2 页,共 25 页 - - - - - - - - - One area where signed or unsigned representation always matters is overflows. Because signed integers are one bit smaller than their equivalent-sized unsigned counterparts (because of the extra bit that holds the sign), overflows are triggered differen
15、tly for signed and unsigned integers. This is where the carry flag and the overflow flag come into play. Instead of having separate signed and unsigned versions of arithmetic instructions, the problem of correctly reporting overflows is addressed by simply having two overflow flags: one for signed o
16、perands and one for unsigned operands. Operations such as addition and subtraction are performed using the same instruction for either signed or unsigned operands, and such instructions set both groups of flags and leave it up to the following instructions to regard the relevant one. 有符号数和无符号数操作必须面对
17、的一个相同的问题就是溢出,但是因为有符号数要比同样长度的无符号数少一个bit (这个bit 用于表示符号),所以有符号数和无符号数的溢出条件是不一样的。所以我们就需要两个溢出标志位 OF 和CF ,一个来表示有符号数(第7位)的溢出(OF )一个来表示无符号数(第8位)的溢出(CF )(设操作数是8位的)。这样在ADD 或者是SUB 指令针对有符号操作数和无符号操作数就可以用一样的指令,一样的执行部件,一样的设置标志位的方法来实现了。而判断是否溢出时就只要去看相应的标志位就可以了。 For example, consider the following arithmetic sample an
18、d how it affects the overflow flags: 见下例,我们观察一下执行了下面的指令后,会对标志位产生什么样的影响: mov ax, 0 x1126 ; (4390 in decimal) mov bx, 0 x7200 ; (29184 in decimal) add ax, bx The above addition will produce different results, depending on whether the destination operand is treated as signed or unsigned. When presented
19、 in hexadecimal form, the result is 0 x8326 , which is equivalent to 33574assuming that AX is considered to be an unsigned operand. If you re treating AX as a signed operand, you will see that an overflow has occurred. Because any signed number that has the most significant bit set is considered neg
20、ative, 0 x8326 becomes 31962. It is obvious that because a signed 16-bit operand can only represent values up to 32767, adding 4390 and 29184 would produce an overflow, and AX would wraparound to a negative number. Therefore, from an unsigned perspective no overflow has occurred, but if you consider
21、 the destination operand to be signed, an overflow has occurred. Because of this, the preceding code would result in OF (representing overflows in signed operands) being set and in CF (representing overflows in unsigned operands) being cleared. 上面的加法指令因为目的操作数是不是有符号数而产生不同的结果。我们从头来说:如果我们设AX 中是一个无符号数,
22、那么加法的结果应该是0 x8326 (十六进制,即十进制的33574 ),但如果假设AX 是一个有符号数,我们将到一个溢出的结果。因为有符号数的最高位是用来表示正负的,所以 0 x8326 就变成了-31962了,这是因为一个16位的有符号数最多只能表示到32767 ,把4390 与29184 相加就会产生溢出,于是AX 中的数就变成一个负数了。所以执行上面的代码后, OF将会被设成1(标志有符号数溢出),而CF 将被清零(标志无符号数无溢出)。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
23、 - 第 3 页,共 25 页 - - - - - - - - - The Zero Flag (ZF) 零标志位( ZF) The zero flag is set when the result of an arithmetic operation is zero, and it is cleared if the result is nonzero. ZF is used in quite a few different situations in IA-32 code, but probably one of the most common uses it has is for com
24、paring two operands and testing whether they are equal. The CMP instruction subtracts one operand from the other and sets ZF if the pseudo result of the subtraction operation is zero, which indicates that the operands are equal. If the operands are unequal, ZF is set to zero. 当算术操作的结果为0时ZF 将置1;如果结果不
25、为0,则ZF 将被清零。ZF 标志位在IA-32汇编语言中有几种不同的用法,但最常用的一个用法大概就是比较两个操作数并测试它们是否相等。如CMP 指令进行一个将两个操作数相减的伪操作,并设置ZF 标志位。如果指令执行完毕后ZF 为0则两个操作数相等,否则,两个操作数不相等。 The Sign Flag (SF) 符号标志位( SF ) The sign flag receives the value of the most significant bit of the result (regardless of whether the result is signed or unsigned)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年识别编译后的算术操作终稿 2022 识别 编译 算术 操作

限制150内