Arithmetic Instructions Illustration- The Addition Instructions: INC and XADD Instruction
The following section explains the Addition Instructions: INC and XADD Instruction in details.
The INC Instruction
- The inc (increment) instruction adds one to its operand. Except for the carry flag, inc sets the flags the same way as add operand, 1 would.
- Note that there are two forms of inc for 16 or 32 bit registers. They are the inc reg and inc reg16 instructions.
- The inc reg and inc mem instructions are the same.
- This instruction consists of an opcode byte followed by a mod-reg-r/m byte The inc reg16 instruction has a single byte opcode. Therefore, it is shorter and usually faster.
- The inc operand may be an eight bit, sixteen bit, or (on the 80386 and later) thirty-two bit register or memory location.
- The inc instruction is more compact and often faster than the comparable add reg, 1 or add mem, 1 instruction. Indeed, the inc reg16 instruction is one byte long, so it turns out that two such instructions are shorter than the comparable add reg, 1 instruction; however, the two increment instructions will run slower on most modern members of the 80x86 family.
- The inc instruction is very important because adding one to a register is a very common operation.
- Incrementing loop control variables or indices into an array is a very common operation, perfect for the inc instruction.
- The fact that inc does not affect the carry flag is very important.
- This allows you to increment array indices without affecting the result of a multiprecision arithmetic operation
The XADD Instruction:
- Xadd (Exchange and Add) is another 80486 (and later) instruction.
- It does not appear on the 80386 and earlier processors.
- This instruction adds the source operand to the destination operand and stores the sum in the destination operand.
- However, just before storing the sum, it copies the original value of the destination operand into the source operand.
- The following algorithm describes this operation:
xadd dest, source
temp := dest
dest := dest source
source := temp
- The xadd sets the flags just as the add instruction would.
- The xadd instruction allows eight, sixteen, and thirty-two bit operands. Both source and destination operands must be the same size.