In the context of Computer Organization and Assembly Language, arithmetic and logical operations form the basis of low-level programming and how a computer performs calculations and makes decisions. These operations are handled at the hardware level (i.e., by the Arithmetic and Logic Unit, or ALU) and are implemented using assembly language instructions.
Arithmetic operations are basic mathematical operations that a processor can perform on numbers. In computer organization, these operations are performed using binary numbers, and assembly language provides instructions to perform these operations.
ADD): Adds two operands.SUB): Subtracts one operand from another.MUL): Multiplies two operands.DIV): Divides one operand by another.ADD)The ADD instruction performs the addition of two operands and stores the result in a register or memory.
Example in Assembly (x86 architecture):
ADD AX, BX ; Adds the contents of register BX to AX, result stored in AX
Flags affected:
SUB)The SUB instruction subtracts the second operand from the first operand and stores the result in the destination operand.
Example in Assembly (x86 architecture):
SUB AX, BX ; Subtracts the contents of BX from AX, result stored in AX
Flags affected:
MUL)The MUL instruction multiplies two unsigned integers and stores the result in registers.
Example in Assembly (x86 architecture):
MUL BX ; Multiplies AX by BX and stores the result in DX:AX (for 16-bit multiplication)
Flags affected:
DIV)The DIV instruction divides an unsigned integer (usually stored in a combination of registers) by another operand.
Example in Assembly (x86 architecture):
DIV BX ; Divides DX:AX by BX, quotient in AX, remainder in DX
Flags affected:
INC)Increments the value of an operand (adds 1).
Example in Assembly (x86 architecture):
INC AX ; Increments the value in AX by 1
DEC)Decrements the value of an operand (subtracts 1).
Example in Assembly (x86 architecture):
DEC AX ; Decrements the value in AX by 1
Logical operations are operations that manipulate binary values, performing bitwise operations. These operations are essential for making decisions based on conditions, comparisons, and controlling the flow of programs.
AND): Performs a bitwise AND on two operands.OR): Performs a bitwise OR on two operands.XOR): Performs a bitwise XOR (exclusive OR) on two operands.NOT): Performs a bitwise NOT (inversion) on an operand.SHL, SHR): Shifts the bits of an operand left or right.AND)The AND instruction compares corresponding bits of two operands and sets the result bit to 1 if both bits are 1; otherwise, the result bit is 0.
Example in Assembly (x86 architecture):
AND AX, BX ; Performs bitwise AND between AX and BX, result stored in AX
Flags affected:
OR)The OR instruction compares corresponding bits of two operands and sets the result bit to 1 if either bit is 1; otherwise, the result bit is 0.
Example in Assembly (x86 architecture):
OR AX, BX ; Performs bitwise OR between AX and BX, result stored in AX
Flags affected:
XOR)The XOR instruction compares corresponding bits of two operands and sets the result bit to 1 if the bits are different (1 and 0, or 0 and 1), and 0 if the bits are the same.
Example in Assembly (x86 architecture):
XOR AX, BX ; Performs bitwise XOR between AX and BX, result stored in AX
Flags affected:
NOT)The NOT instruction inverts each bit of the operand (also known as a bitwise negation).
Example in Assembly (x86 architecture):
NOT AX ; Inverts each bit of AX
Flags affected:
Shift Left (SHL): The SHL instruction shifts the bits of the operand to the left by a specified number of positions. This operation effectively multiplies the number by 2 for each shift.
Example in Assembly (x86 architecture):
SHL AX, 1 ; Shifts the bits of AX left by 1 position (multiplies AX by 2)
Shift Right (SHR): The SHR instruction shifts the bits of the operand to the right by a specified number of positions. This operation effectively divides the number by 2 for each shift (ignoring the remainder).
Example in Assembly (x86 architecture):
SHR AX, 1 ; Shifts the bits of AX right by 1 position (divides AX by 2)
Flags affected:
Rotate Left (ROL): The ROL instruction rotates the bits of the operand to the left, meaning the most significant bit is moved to the least significant bit position.
Example in Assembly (x86 architecture):
ROL AX, 1 ; Rotates the bits of AX left by 1 position
Rotate Right (ROR): The ROR instruction rotates the bits of the operand to the right, meaning the least significant bit is moved to the most significant bit position.
Example in Assembly (x86 architecture):
ROR AX, 1 ; Rotates the bits of AX right by 1 position
Although not strictly arithmetic or logical operations, comparison operations are critical for decision-making in programming. These operations often set flags in the processor, which are later used by conditional jump instructions (like JZ, JNE, etc.) to control the flow of a program.
CMP: Compares two operands by subtracting the second operand from the first, without storing the result. It affects the flags and is used for decision-making.
Example:
CMP AX, BX ; Compares AX and BX by subtracting BX from AX
TEST: Performs a bitwise AND between two operands and updates the flags based on the result. It's often used for testing specific bits in a register or memory.
Example:
TEST AX, BX ; Performs bitwise AND on AX and BX and updates the flags
Flags are special bits in the CPU that indicate the results of arithmetic and logical operations. The Flag Register holds several individual flags that are affected by various operations:
Zero Flag (ZF):
Set if the result of the operation is zero.
Carry Flag (CF):
Set if there is a carry out or borrow in arithmetic operations (e.g., in addition or subtraction).
Sign Flag (SF):
Set if the result is negative (for signed operations).
Overflow Flag (OF):
Set if an overflow occurs during an arithmetic operation (i.e., when the result cannot be represented with the given number of bits).
Parity Flag (PF):
Set if the number of 1s in the result is even.
Many programs use conditional statements to decide what action to take based on the results of arithmetic and logical operations. For example:
Jump Instructions:
Based on the state of the flags (such as zero, carry, overflow), the CPU can jump to a different part of the program.
JE label (Jump if Equal, i.e., Zero flag is set)Looping and Conditional Execution:
The results of logical comparisons can control whether a loop continues or if a certain block of code is executed.
| Operation | Description | Example |
|---|---|---|
| Addition (ADD) | Adds two numbers together. | 5 + 3 = 8 |
| Subtraction (SUB) | Subtracts one number from another. | 5 - 3 = 2 |
| Multiplication (MUL) | Multiplies two numbers together. | 5 * 3 = 15 |
| Division (DIV) | Divides one number by another. | 6 / 3 = 2 |
| AND | Performs bitwise AND between two values. | 5 AND 3 = 1 |
| OR | Performs bitwise OR between two values. | 5 OR 3 = 7 |
| XOR | Performs bitwise XOR between two values. | 5 XOR 3 = 6 |
| NOT | Inverts all the bits in a value. | NOT 5 = -6 |
| Shift Left (SHL) | Shifts bits left, inserts zeros at the right end. | 5 << 1 = 10 |
| Shift Right (SHR) | Shifts bits right, inserts zeros at the left end. | 10 >> 1 = 5 |
| Compare (CMP) | Compares two values by subtracting them (flags set). | CMP AX, BX |
| Test (TEST) | Performs a bitwise AND and sets flags based on the result. | TEST AX, BX |
Arithmetic and logical operations are essential components of Computer Organization and Assembly Language, enabling the processor to perform calculations, make decisions, and control data flow. These operations are executed by the Arithmetic and Logic Unit (ALU) and are accessed using assembly language instructions that directly manipulate data at the binary level. Understanding these operations is key to working with low-level programming and optimizing machine-level performance.
Open this section to load past papers