In assembly language, performing addition and subtraction is done using the ADD and SUB instructions, respectively. These instructions are used to modify the contents of registers (or memory) by adding or subtracting values.
The ADD instruction adds the value of the source operand to the destination operand and stores the result in the destination operand. It is commonly used for performing arithmetic operations, such as adding integers.
ADD destination, source
The ADD instruction affects several flags in the processor's Flags Register:
MOV AX, 5 ; AX = 5
MOV BX, 10 ; BX = 10
ADD AX, BX ; AX = AX + BX = 5 + 10 = 15
In this example:
5 is loaded into the AX register.10 is loaded into the BX register.ADD AX, BX instruction adds the value in BX (10) to AX (5), and the result (15) is stored back in AX.MOV AX, 20 ; AX = 20
ADD AX, 30 ; AX = 20 + 30 = 50
Here, the immediate value 30 is added to the value in AX (20), and the result 50 is stored back in AX.
MOV AX, [num] ; Load value from memory location 'num' into AX
ADD AX, BX ; AX = AX + BX (Adds value of BX to AX)
In this case:
num is loaded into the AX register.BX is then added to AX.The SUB instruction subtracts the source operand from the destination operand and stores the result in the destination operand. This is used for subtracting one number from another.
SUB destination, source
The SUB instruction also affects several flags in the Flags Register:
MOV AX, 15 ; AX = 15
MOV BX, 10 ; BX = 10
SUB AX, BX ; AX = AX - BX = 15 - 10 = 5
Here:
AX contains 15, and BX contains 10.SUB AX, BX instruction subtracts the value in BX (10) from AX (15), and the result (5) is stored in AX.MOV AX, 50 ; AX = 50
SUB AX, 20 ; AX = 50 - 20 = 30
In this example:
20 is subtracted from the value in AX (50), and the result (30) is stored back in AX.MOV AX, [num] ; Load value from memory location 'num' into AX
SUB AX, BX ; AX = AX - BX (Subtracts value of BX from AX)
In this case:
num is loaded into AX.BX is then subtracted from AX.Signed Addition/Subtraction: Both ADD and SUB can handle signed integers (positive and negative numbers). The result will be stored according to the signed interpretation of the operand.
Unsigned Addition/Subtraction: For unsigned integers, the Carry Flag (CF) will indicate whether a carry or borrow occurred.
When working with larger numbers (e.g., 32-bit or 64-bit), ADD and SUB can also operate on 32-bit or 64-bit registers. For example, in 32-bit registers like EAX, EBX, or ECX (in x86 assembly) or RAX, RBX, etc. (in x64), the same syntax applies, but the operands are typically 32-bit or 64-bit values.
MOV EAX, 5000000 ; EAX = 5000000
MOV EBX, 1000000 ; EBX = 1000000
ADD EAX, EBX ; EAX = EAX + EBX = 5000000 + 1000000 = 6000000
MOV RAX, 10000000000 ; RAX = 10000000000
MOV RBX, 5000000000 ; RBX = 5000000000
SUB RAX, RBX ; RAX = RAX - RBX = 10000000000 - 5000000000 = 5000000000
By mastering ADD and SUB, you gain the ability to manipulate and process numerical data in an efficient, low-level manner.
Open this section to load past papers