0% found this document useful (0 votes)
0 views6 pages

Algorithms in Assembly Programming

Uploaded by

raphaelvicuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Algorithms in Assembly Programming

Uploaded by

raphaelvicuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

💻 Algorithms in Assembly Language – Detailed Notes

📌 What Is Assembly Language?


●​ Low-level programming language that provides a symbolic representation of a
machine's instructions.​

●​ Each instruction maps closely to a CPU operation.​

●​ Often used for performance-critical code or direct hardware control.​

🧠 Understanding Assembly and Algorithms


An algorithm in Assembly is implemented through:

●​ Registers (for temporary data storage)​

●​ Instructions (like MOV, ADD, SUB, MUL, DIV)​

●​ Jumps and Loops (e.g., JMP, JE, JNE, LOOP)​

●​ Stack operations (PUSH, POP)​

●​ Conditional logic (CMP, JL, JG, JZ)​

🔢 Example Algorithms in Assembly (x86 NASM Style)


✅ 1. GCD using Euclidean Algorithm
📖 Logic:
C/C++‎

●​ while (b ≠ 0) {
●​ temp = b;
●​ b = a % b;
●​ a = temp;
●​ }
●​ return a;

🧾 Assembly (NASM-like):
None

●​ section .text
●​ global main
●​ main:
●​ mov eax, 48 ; a = 48
●​ mov ebx, 18 ; b = 18
●​
●​ gcd_loop:
●​ cmp ebx, 0
●​ je done
●​ xor edx, edx ; clear edx before division
●​ div ebx ; eax / ebx → quotient in eax, remainder
in edx
●​ mov eax, ebx
●​ mov ebx, edx
●​ jmp gcd_loop
●​
●​ done:
●​ ; GCD is now in EAX

✅ 2. Factorial (n!) – Iterative Approach


📖 Logic:
C/C++‎

●​ int fact = 1;
●​ for (int i = n; i > 1; i--) {
●​ fact *= i;
●​ }

🧾 Assembly:
None

●​ section .text
●​ global main
●​ main:
●​ mov ecx, 5 ; n = 5
●​ mov eax, 1 ; result = 1
●​
●​ fact_loop:
●​ mul ecx ; eax *= ecx
●​ loop fact_loop ; ecx-- and jump if ecx ≠ 0
●​
●​ ; Result in EAX

✅ 3. Sum of First N Natural Numbers


📖 Formula:
C/C++‎

●​ sum = n * (n + 1) / 2

🧾 Assembly:
None

●​ section .text
●​ global main
●​ main:
●​ mov eax, 10 ; n = 10
●​ mov ebx, eax ; copy n to ebx
●​ inc eax ; eax = n + 1
●​ mul ebx ; eax *= ebx (eax = n * (n + 1))
●​ shr eax, 1 ; divide by 2 using shift right (eax =
eax / 2)
●​
●​ ; Result in EAX

✅ 4. Prime Checker
📖 Logic:
C/C++‎

●​ for (i = 2; i <= n/2; i++) {


●​ if (n % i == 0) not prime;
●​ }

Note: In Assembly, this needs loops and modulus calculation.

🔍 Key Assembly Concepts for Algorithms


Concep Description
t

MOV Data transfer (e.g., MOV eax, 5)


ADD, Arithmetic
SUB

MUL, Multiplication and division


DIV (unsigned)

CMP, Comparison and conditional


JZ, jumps
etc.

LOOP Decrement ECX and jump if not


zero

PUSH / Stack operations


POP

CALL / Function calls


RET

🧰 Tips for Writing Algorithms in Assembly


●​ Plan with pseudocode first.​

●​ Use registers wisely: EAX, EBX, ECX, EDX.​

●​ Avoid deep recursion in Assembly (stack is manual).​

●​ Keep track of which registers are volatile (e.g., EAX, ECX, EDX are often changed during
function calls).​
●​ Use comments generously for clarity.​

📘 Practice Ideas
Try implementing these in x86 Assembly:

1.​ Fibonacci sequence (iterative or recursive)​

2.​ Reverse a string​

3.​ Integer square root​

4.​ Exponentiation (e.g., a^b)​

5.​ Bubble sort (on an array in memory)​

●​

You might also like