Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

Create Division by a Power of Two.md

Abdul Wahab Junaid80ab1a97115e
diff --git a/Assembly language/New Assembly Work/Assembly Program/Division by a Power of Two.md b/Assembly language/New Assembly Work/Assembly Program/Division by a Power of Two.md
new file mode 100644
index 0000000..17b6b19
--- /dev/null
+++ b/Assembly language/New Assembly Work/Assembly Program/Division by a Power of Two.md
@@ -0,0 +1,95 @@
+In Assembly Language (and low-level programming in general), division by a power of two is often performed using **bitwise right shifts**. This is because shifting bits to the right by `n` positions divides a number by \(2^n\).
+
+### **Basic Idea:**
+- **Right shift**: Shifting a number to the right by `n` bits effectively divides the number by \(2^n\).
+ - Example: $\( x >> 1 \) divides \( x \) by 2$.
+ - Example: $\( x >> 3 \) divides \( x \) by \(2^3 = 8\)$.
+
+---
+
+### **Unsigned Division by a Power of Two**
+
+When working with **unsigned integers**, division by powers of two is straightforward with the **`SHR` (Shift Right)** instruction.
+
+#### **Example 1: Dividing by 2**
+
+```assembly
+section .text
+ global _start
+
+_start:
+ MOV EAX, 16 ; Load 16 into the EAX register
+ SHR EAX, 1 ; Divide EAX by 2 (16 / 2 = 8)
+
+ ; Exit the program (Linux syscall example)
+ MOV EAX, 60 ; Syscall number for exit
+ XOR EDI, EDI ; Exit code 0
+ syscall
+```
+
+In this example:
+- **`SHR EAX, 1`** shifts the value in **EAX** (which is 16) to the right by 1 bit, effectively dividing it by \(2^1 = 2\), resulting in **EAX = 8**.
+
+#### **Example 2: Dividing by 8**
+
+```assembly
+section .text
+ global _start
+
+_start:
+ MOV EAX, 64 ; Load 64 into the EAX register
+ SHR EAX, 3 ; Divide EAX by 8 (64 / 8 = 8)
+
+ ; Exit the program (Linux syscall example)
+ MOV EAX, 60 ; Syscall number for exit
+ XOR EDI, EDI ; Exit code 0
+ syscall
+```
+
+In this example:
+- **`SHR EAX, 3`** shifts the value in **EAX** (which is 64) to the right by 3 bits, effectively dividing it by \(2^3 = 8\), resulting in **EAX = 8**.
+
+---
+
+### **Signed Division by a Power of Two**
+
+For **signed integers**, you need to use **arithmetic right shift** (`SAR`), which preserves the sign bit.
+
+#### **Example 3: Signed Division by 4**
+
+```assembly
+section .text
+ global _start
+
+_start:
+ MOV EAX, -32 ; Load -32 into the EAX register
+ SAR EAX, 2 ; Divide EAX by 4 (-32 / 4 = -8)
+
+ ; Exit the program (Linux syscall example)
+ MOV EAX, 60 ; Syscall number for exit
+ XOR EDI, EDI ; Exit code 0
+ syscall
+```
+
+In this example:
+- **`SAR EAX, 2`** shifts the value in **EAX** to the right by 2 bits, effectively dividing it by \(2^2 = 4\). Since **EAX** contains **-32**, the result will be **-8** after the shift, and the **sign bit** is preserved.
+
+---
+
+### **Why Bit Shifting Is Efficient for Division by Powers of Two:**
+
+1. **Speed**: Bit shifts are faster than division instructions because they are simpler operations that CPUs can perform in fewer clock cycles.
+
+2. **Optimized Instructions**: Most modern processors optimize shifting operations, making them much faster than conventional division.
+
+3. **No Remainder**: When dividing by a power of two using shifts, you effectively truncate any remainder (integer division).
+
+---
+
+### **Summary:**
+- **`SHR` (Shift Right)** is used for unsigned division by a power of two.
+- **`SAR` (Arithmetic Shift Right)** is used for signed division by a power of two.
+
+- **Right-shifting by `n` positions** divides the number by $\(2^n\)$.
+
+---