Arithmetic Operations in Assembly Language

Arithmetic instructions in assembly language perform basic mathematical operations on data in registers or memory.

Addition (add)

Adds the values of two operands and stores the result.

mov eax, 5 ; EAX = 5 add eax, 3 ; EAX = EAX + 3 (result: 8)

Subtraction (sub)

Subtracts the second operand from the first and stores the result.

mov eax, 10 ; EAX = 10 sub eax, 4 ; EAX = EAX - 4 (result: 6)

Multiplication (imul)

Multiplies two operands and stores the result. The imul instruction is used for signed integers.

mov eax, 3 ; EAX = 3 imul eax, 4 ; EAX = EAX * 4 (result: 12)

Division (idiv)

Divides the contents of a register by a specified value. The quotient is stored in the register, and the remainder in the special register edx.

mov eax, 10 ; EAX = 10 mov ecx, 2 ; divisor = 2 idiv ecx ; EAX = EAX / 2 (result: 5, remainder: 0)

Increment (inc) and Decrement (dec)

Increments or decrements the value of a register or memory location by 1.

mov eax, 7 ; EAX = 7 inc eax ; EAX = EAX + 1 (result: 8) mov ebx, 20 ; EBX = 20 dec ebx ; EBX = EBX - 1 (result: 19)

Negation (neg)

Changes the sign of a value (multiplies it by -1).

mov eax, 5 ; EAX = 5 neg eax ; EAX = -5

Absolute Value (abs)

Computes the absolute value of a signed integer.

mov eax, -8 ; EAX = -8 abs eax ; EAX = 8

Arithmetic instructions can be used to perform a wide variety of tasks, such as:

  1. Calculating the sum of two numbers
  2. Finding the difference between two numbers
  3. Calculating the product of two numbers
  4. Calculating the quotient of two numbers
  5. Converting a number from one base to another
  6. Generating random numbers
  7. Performing statistical calculations

Conclusion

Arithmetic instructions facilitate fundamental mathematical operations on data, including addition, subtraction, multiplication, and division. These instructions directly manipulate registers or memory locations to perform computations and are essential for numerical data manipulation in low-level programming.