Arithmetic And Logic Instructions PDF
Document Details
STI
Tags
Summary
This document provides an overview of arithmetic and logic instructions, including add, subtract, multiply, and divide operations. Common x86 instructions (ADD, SUB, MUL, etc.) are detailed, along with logic operations for bit manipulation.
Full Transcript
IT2104 Arithmetic and Logic Instructions DEC Op This instruction subtracts 1 from the destination Arithmetic Operations operand while preserving the...
IT2104 Arithmetic and Logic Instructions DEC Op This instruction subtracts 1 from the destination Arithmetic Operations operand while preserving the state of the CF flag. Machines generally provide the basic arithmetic operations, which are add, NEG Op This replaces the value of the operand with (0 – subtract, multiply, and divide. These are invariably provided for signed integer operand), using two's complement representation. (fixed-point) numbers. Nonetheless, other possible operations include a CMP Op1, Op2 This operation compares the two operands by variety of single-operand instructions, such as the following (Stallings, 2019): subtracting the second operand from the first Absolute – This takes the absolute value of the operand. operand and sets the status flags in the register Negate – This negates the operand. according to the results. Increment – This adds 1 to the operand. Decrement – This subtracts 1 from the operand. The CMP instruction can be further described as follows (Ledin, 2020): It can be used to determine if the destination operand is greater than, equal Note that the execution of an arithmetic instruction may involve data transfer to, or less than the source operand. operations to position operands for input to the arithmetic logic unit (ALU) and It updates the status flags C, P, A, Z, S, and O. deliver the output of the ALU. It does not alter either of the operands. It may be followed by a conditional jump instruction or a set condition Common x86 arithmetic instructions: instruction. Operation Name Description It does not use decimal mode if the D flag is set. ADD Dest, Source This instruction adds the destination and the source It does not affect the value of the V flag. operand. Then, the result is stored in the destination It supports all addressing modes. operand, which can either be a register or a memory. On the other hand, the source can be a Logic and Shift Operations register, memory, or an immediate operand. Logic and shift instructions, including the rotate instructions, are commonly SUB Dest, Source This instruction subtracts the source from the referred to as bit manipulation instructions. These instructions are used to destination and stores the result in the destination perform bitwise operations that help in the improvement of bit manipulation operand. speed. Note that bits can be used to represent flags, and through specific MUL Op This performs an unsigned integer multiplication of instructions and directives, these flags can be manipulated. The following are the operand by the AL, AX, and EAX register and some characteristics of logic instructions: stores in the register. Note that the opcode indicates Logic instructions operate on a bit-by-bit basis. the size of the register. The main usage of logical instructions are to set, clear, complement or IMUL Op This performs a signed integer multiplication. invert, and isolate operands. DIV Op This instruction divides unsigned values in the AX, Some processors use the entire content of an operand as a whole flag. DX:AX, EDX:EAX, or RDX:RAX registers (dividend) All logic instructions affect flag bits except for the NOT instruction. by the source operand (divisor). Results are stored in the AX (AH:AL), DX:AX, EDX:EAX, or RDX:RAX Common x86 logic instructions: registers. Operation Name Description IDIV Op This instruction divides signed integers. NOT Op This inverts each bit of the operand. INC Op This instruction adds 1 to the destination operand AND Dest, Source This performs a bitwise AND operation on the while preserving the state of the CF flag. destination and source operands and stores the result in the destination operand. 04 Handout 1 *Property of STI [email protected] Page 1 of 2 IT2104 OR Dest, Source This performs a bitwise OR operation on the CF flag is loaded with the last bit shifted out of the destination and source operands and stores the operand. result in the destination operand. ROL Op, Qty This rotates bits to the left, with wraparound. Then, the XOR Dest Source This performs a bitwise XOR operation on the CF flag is loaded with the last bit shifted out of the destination and source operands and stores the operand. result in the destination operand. ROR Op, Qty This rotates bits to the right, with wraparound. Then, the TEST Op1, Op2 This performs a bitwise AND operation on two CF flag is loaded with the last bit shifted out of the operands and sets the S, Z, and P status flags, and operand. the operands are unchanged. RCL Op, Qty This rotates bits to the left, including the CF flag, with wraparound. This instruction treats the CF flag as a 1-bit Logical Shift. In this operation, the bits of a word are shifted left or right, extensions on the upper end of the operand. wherein on one end, the bit shifted put is lost, and on the other end, a zero (0) RCR Op, Qty This rotates bits to the right, including the CF flag, with is shifted in. Logical shifts are useful in isolating fields within a word (Stallings, wraparound. This instruction treats the CF flag as a 1-bit 2019). extension on the lower end of the operand. Arithmetic Shift. This operation treats data as a signed integer and does not Below are some examples of shift and rotate operations: shift the sign bit. On a right arithmetic shift, the sign bit is replicated into the bit Input Operation Output position to its right. On a left arithmetic shift, a logical left shift is performed on 10100110 Logical right shift (3 bits) 00010100 all bits except for the sign bit, which is retained as is. This generally helps in 10100110 Logical left shift (3 bits) 00110000 speeding up certain arithmetic operations (Stallings, 2021). 10100110 Arithmetic right shift (3 bits) 11110100 10100110 Arithmetic left shift (3 bits) 10110000 Cycling Shift (Rotate). This operation preserves all of the bits being operated 10100110 Right rotate (3 bits) 11010100 on. One use of this operation is to bring each bit successively into the leftmost 10100110 Left rotate (3 bits) 00110101 bit, where it can be identified by testing the sign of the data, treated as a number (Stallings, 2019). References: Common x86 shift and rotate instructions Ledin, J. (2020). Modern computer architecture and organization. Packt Publishing Stallings, W. (2019). Computer organization and architecture: Designing for performance (11th ed.). Pearson Education, Inc. Operation Description Name SAL Op, Qty This shifts the source operand to the left from 1 to 31 bit positions, while the empty bit positions are cleared. Then, the CF flag is loaded with the last bit shifted out of the operand. SAR Op, Qty This shifts the source operand to the right from 1 to 31 bit positions, while the empty bit positions are cleared if the operand is positive (+) and set if the operand is negative (-). Then, the CF flag is loaded with the last bit shifted out of the operand. SHR Op, Qty This shifts the source operand right by from 1 to 31 bit positions, while empty bit positions are cleared, and the 04 Handout 1 *Property of STI [email protected] Page 2 of 2