您的位置:首页 > 其它

Chapter 3-02

2015-10-10 00:32 281 查看
Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1.

3.5 Arithmetic and Logical Operations

Each of the instruction classes shown has instructions for operating on byte, word, and double-word data except leal has no other size variants.

The operations are divided into four groups: load effective address, unary, binary, and shifts.

3.5.1 Load Effective Address

leal has the form of an instruction that reads from memory to a register, but it does not reference memory at all. It copies the effective address to the destination. This instruction can be used to generate pointers for later memory references.

For example, if register %edx contains value x, then the instruction “ leal 7(%edx,%edx,4), %eax” will set register %eax to 5x + 7.

The destination operand must be a register.

3.5.2 Unary and Binary Operations

Unary operand can be either a register or a memory location. For example, the instruction incl (%esp) causes the 4-byte element on the top of the stack to be incremented.

Binary operations: the second operand is used as both a source and a destination. The first operand can be either an immediate value, a register, or a memory location. The second can be either a register or a memory location. The two operands cannot both be memory locations.

3.5.3 Shift Operations

The shift amount is encoded as a single byte, because only shift amounts between 0 and 31 are possible, so only the low-order 5 bits of the shift amount are considered. The shift amount is given either as an immediate or in the single-byte register element %cl.

The destination operand of a shift operation can be either a register or a memory location.

3.5.4 Discussion

The assembly code instructions occur in a different order than in the C source code. Instructions 2 and 3 compute the expression z*48 by a combination of leal and shift instructions.

3.5.5 Special Arithmetic Operations

The imull instruction generates a 32-bit product from two 32-bit operands.

IA32 provides two different “one-operand” multiply instructions to compute the full 64-bit product of two 32-bit values—one for unsigned (mull), and one for two’s-complement (imull) multiplication. For both of these, one argument must be in register %eax, and the other is given as the instruction source operand. The product is then stored in registers %edx (high-order 32 bits) and %eax (low-order 32 bits). Although the name imull is used for two distinct multiplication operations, the assembler can tell which one is intended by counting the number of operands.

Suppose we have signed numbers x and y stored at positions 8 and 12 relative to %ebp, and we want to store their full 64-bit product as 8 bytes on top of the stack.

idivl takes as dividend the 64-bit quantity in registers %edx (high-order 32 bits) and %eax (low-order 32 bits). The divisor is given as the instruction operand. The instruction stores the quotient in register %eax and the remainder in register %edx.

Suppose we have signed numbers x and y stored at positions 8 and 12 relative to %ebp, and we want to store values x/y and x mod y on the stack.

The move instruction on line 1 and the arithmetic shift on line 3 have the combined effect of setting register %edx to either all zeros or all ones depending on the sign of x, while the move instruction on line 2 copies x into %eax. Thus, we have the combined registers %edx and %eax storing a 64-bit, sign-extended version of x.

cltd instruction sign extends %eax into %edx. With this instruction, the code sequence shown above becomes

Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: