Adding and subtracting integers are fundamental operations in almost all computer programs, and assembly language provides instructions to perform these tasks directly at the machine level. These operations are typically done using registers or memory addresses as operands. Depending on the architecture (e.g., x86, ARM, MIPS), the specific instructions and the syntax may vary slightly, but the basic concepts remain the same.
Let's explore how addition and subtraction are handled in assembly language, specifically using x86 (Intel syntax) as an example.
Addition is one of the simplest arithmetic operations. In assembly language, the ADD instruction is used to add two operands (which can be immediate values, registers, or memory locations).
ADD destination, source
The ADD instruction adds the value in the source operand to the destination operand and stores the result in destination.
MOV AX, 5 ; Load the value 5 into register AX
MOV BX, 3 ; Load the value 3 into register BX
ADD AX, BX ; Add the value in BX to AX (AX = AX + BX)
; After this, AX = 8
In this example:
AX.BX.ADD instruction adds the value in BX (which is 3) to the value in AX (which is 5), and the result (8) is stored in AX.MOV AX, 10 ; Load the value 10 into register AX
ADD AX, 5 ; Add the immediate value 5 to AX (AX = AX + 5)
; After this, AX = 15
Here, the immediate value 5 is added directly to AX, so the result stored in AX is 15.
MOV AX, 10 ; Load the value 10 into AX
MOV BX, [num] ; Load the value from memory location 'num' into BX
ADD AX, BX ; Add the value in BX to AX (AX = AX + [num])
In this case, the value stored at the memory address labeled num is loaded into the BX register, and then added to AX.
Subtraction is also a basic operation, and it is performed using the SUB instruction in assembly language.
SUB destination, source
The SUB instruction subtracts the source operand from the destination operand and stores the result in destination.
MOV AX, 10 ; Load the value 10 into AX
MOV BX, 3 ; Load the value 3 into BX
SUB AX, BX ; Subtract the value in BX from AX (AX = AX - BX)
; After this, AX = 7
In this example:
AX.BX.SUB instruction subtracts the value in BX (which is 3) from the value in AX (which is 10), and the result (7) is stored in AX.MOV AX, 20 ; Load the value 20 into AX
SUB AX, 5 ; Subtract the immediate value 5 from AX (AX = AX - 5)
; After this, AX = 15
Here, the immediate value 5 is subtracted directly from AX, so the result stored in AX is 15.
MOV AX, 20 ; Load the value 20 into AX
MOV BX, [num] ; Load the value from memory location 'num' into BX
SUB AX, BX ; Subtract the value in BX from AX (AX = AX - [num])
Here, the value stored at the memory address labeled num is loaded into the BX register, and then subtracted from AX.
When performing addition and subtraction, the processor’s flags (specifically the carry flag and borrow flag) are updated. These flags can be used for further decision-making or checking for overflow/underflow.
Carry Flag (CF): This flag is set when there is a carry out of the most significant bit during addition, or a borrow in subtraction.
Overflow Flag (OF): The OF is set when the result of an operation overflows the range of the signed numbers that can be represented in the register. It is used primarily for signed integer operations.
MOV AX, 0xFFFF ; AX = 65535 (maximum value for 16-bit register)
ADD AX, 1 ; AX = 65536, which overflows (carry flag will be set)
In this example, adding 1 to AX (which already holds the largest 16-bit value 0xFFFF) causes an overflow, and the carry flag will be set.
MOV AX, 5 ; AX = 5
MOV BX, 10 ; BX = 10
SUB AX, BX ; AX = -5 (borrow flag will be set)
In this example, subtracting 10 from 5 results in a negative value, and the borrow flag will be set to indicate that the subtraction couldn't be performed without a borrow.
In assembly, you may need to be mindful of whether the integers are signed or unsigned because the handling of overflow and carry differs.
Here is a simple example program that demonstrates both addition and subtraction of integers in x86 assembly:
section .data
num1 db 10 ; First number
num2 db 5 ; Second number
section .text
global _start
_start:
; Load values into registers
MOV AL, [num1] ; AL = 10 (load num1 into AL)
MOV BL, [num2] ; BL = 5 (load num2 into BL)
; Add num1 and num2
ADD AL, BL ; AL = AL + BL = 10 + 5 = 15
; Now AL = 15, do something with the result (like storing it or printing)
; Subtract num2 from num1
MOV AL, [num1] ; Reload AL with num1 (10)
SUB AL, [num2] ; AL = AL - num2 = 10 - 5 = 5
; End program (this is platform-specific, e.g., for Linux or Windows)
MOV AX, 1 ; Exit system call number
INT 0x80 ; Make the system call to exit
In this example:
num1 and num2) into the registers AL and BL.ADD AL, BL) and stores the result in AL.num1 into AL and performs subtraction (SUB AL, [num2]).Adding and subtracting integers in assembly language is done using simple instructions like ADD and SUB. These instructions work on registers, memory locations, or immediate values. Flags like the carry flag and overflow flag are updated based on the results of the operations, and they can be used to handle special cases like overflow or underflow. Understanding how to use these instructions
Open this section to load past papers