From 2ff6f4560eee8b596d9b0e7e1322ac90c9dcb4b5 Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Tue, 26 Aug 2025 06:42:28 -0700 Subject: [PATCH 1/6] LAB#01 ALL TASKS ADDED --- Lab01/lab1_solution.txt | 288 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 Lab01/lab1_solution.txt diff --git a/Lab01/lab1_solution.txt b/Lab01/lab1_solution.txt new file mode 100644 index 0000000..203a80f --- /dev/null +++ b/Lab01/lab1_solution.txt @@ -0,0 +1,288 @@ +#include +#include +#include +#include +#include + +/////////////////////////////////////////////////// Task#01 /////////////////////////////////////////////////////// +void task01_datatypes() { + printf("Task 01: Data Types and Sizes\n"); + printf("size of int is %ld\n", sizeof(int)); + printf("size of float is %ld\n", sizeof(float)); + printf("size of double is %ld\n", sizeof(double)); + printf("size of char is %ld\n\n", sizeof(char)); +} + +/////////////////////////////////////////////////// Task#02 /////////////////////////////////////////////////////// +void arthmatic_operations() { + printf("Task 02: Arithmetic Operations\n"); + int x, y; + printf("Enter 1st positive number: "); + scanf("%d", &x); + printf("Enter 2nd positive number: "); + scanf("%d", &y); + + int add = x + y; + int sub = x - y; + int mul = x * y; + double div = (double)x / y; + int mod = x % y; + + printf("sum=%d sub=%d mul=%d div=%.2f mod=%d\n\n", add, sub, mul, div, mod); +} + +void switch_operation() { + printf("Task 02: Switch Operation\n"); + int a, b, operation; + printf("Enter a: "); + scanf("%d", &a); + printf("Enter b: "); + scanf("%d", &b); + printf("Choose operation (0-add, 1-sub, 2-mul, 3-div, 4-mod): "); + scanf("%d", &operation); + + switch (operation) { + case 0: printf("Sum = %d\n\n", a + b); break; + case 1: printf("Subtraction = %d\n\n", a - b); break; + case 2: printf("Multiplication = %d\n\n", a * b); break; + case 3: printf("Division = %.2f\n\n", (double)a / b); break; + case 4: printf("Modulus = %d\n\n", a % b); break; + default: printf("Invalid operation\n\n"); + } +} + +/////////////////////////////////////////////////// Task#03 /////////////////////////////////////////////////////// +void fibonaci_sequence() { + printf("Task 03.1: Fibonacci Sequence\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + int a = 0, b = 1, result; + printf("%d ", a); + + for (int i = 2; i <= n; i++) { + result = a + b; + a = b; + b = result; + printf("%d ", a); + } + printf("\n\n"); +} + +void guessing_game() { + printf("Task 03.2: Guessing Game\n"); + srand(time(0)); + int random_number = (rand() % 100) + 1; + int guess; + printf("Guess a number between 1 to 100: "); + scanf("%d", &guess); + + if (random_number > guess) + printf("YOU FAIL! Your guess is less than the random number (%d)\n\n", random_number); + else if (random_number < guess) + printf("YOU FAIL! Your guess is greater than the random number (%d)\n\n", random_number); + else + printf("Congratulations! Your guess matches the random number\n\n"); +} + +/////////////////////////////////////////////////// Task#04 /////////////////////////////////////////////////////// +void Is_prime() { + printf("Task 04.1: Check Prime\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + if (n <= 1) { + printf("Not a prime number\n\n"); + return; + } + + int isprime = 1; + for (int i = 2; i <= n / 2; i++) { + if (n % i == 0) { + isprime = 0; + break; + } + } + if (isprime) + printf("Yes, it's a prime number\n\n"); + else + printf("Not a prime number\n\n"); +} + +void prime_numbers() { + printf("Task 04.2: Prime Numbers up to 100\n"); + for (int i = 2; i <= 100; i++) { + int isprime = 1; + for (int j = 2; j * j <= i; j++) { + if (i % j == 0) { + isprime = 0; + break; + } + } + if (isprime) { + printf("%d ", i); + } + } + printf("\n\n"); +} + +int fictorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * fictorial(n - 1); +} + +/////////////////////////////////////////////////// Task#05 /////////////////////////////////////////////////////// +void reverse_string() { + printf("Task 05.1: Reverse String\n"); + char arr[] = "Hello"; + int i = strlen(arr) - 1; + while (i >= 0) { + printf("%c", arr[i]); + i--; + } + printf("\n\n"); +} + +void second_highest() { + printf("Task 05.2: Second Highest Element\n"); + int arr[] = {1, 2, 3, 4, 5}; + int first = arr[0], second = -1; + for (int i = 1; i < 5; i++) { + if (arr[i] > first) { + second = first; + first = arr[i]; + } else if (arr[i] > second && arr[i] < first) { + second = arr[i]; + } + } + printf("Second highest is %d\n\n", second); +} + +/////////////////////////////////////////////////// Task#06 /////////////////////////////////////////////////////// +void file_system() { + printf("Task 06: File System\n"); + int number[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + FILE *ptr = fopen("numbers.txt", "w"); + for (int i = 0; i < 10; i++) { + fprintf(ptr, "%d\n", number[i]); + } + fclose(ptr); + + FILE *ptr1 = fopen("numbers.txt", "r"); + int n[50]; + for (int i = 0; i < 10; i++) { + fscanf(ptr1, "%d", &n[i]); + printf("%d ", n[i]); + } + fclose(ptr1); + printf("\n\n"); +} + +/////////////////////////////////////////////////// Task#07 /////////////////////////////////////////////////////// +void bitwise_operations() { + printf("Task 07.1: Bitwise Operations\n"); + int x, y; + printf("Enter x: "); + scanf("%d", &x); + printf("Enter y: "); + scanf("%d", &y); + + printf("AND = %d\n", x & y); + printf("OR = %d\n", x | y); + printf("XOR = %d\n", x ^ y); + printf("Shift Left = %d\n", x << y); + printf("Shift Right = %d\n\n", x >> y); +} + +void ispower_of2() { + printf("Task 07.2: Power of 2 Check\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + if ((n > 0) && ((n & (n - 1)) == 0)) { + printf("Yes, %d is the power of 2\n\n", n); + } else { + printf("No, %d is not the power of 2\n\n", n); + } +} + +/////////////////////////////////////////////////// Task#08 /////////////////////////////////////////////////////// +void enumeration() { + printf("Task 08: Enumeration\n"); + enum WEEKDAYS {MON = 1, TUE, WED, THR, FRI, SAT, SUN}; + int number; + printf("Enter a number (1-7): "); + scanf("%d", &number); + + switch ((enum WEEKDAYS)number) { + case MON: printf("MONDAY\n\n"); break; + case TUE: printf("TUESDAY\n\n"); break; + case WED: printf("WEDNESDAY\n\n"); break; + case THR: printf("THURSDAY\n\n"); break; + case FRI: printf("FRIDAY\n\n"); break; + case SAT: printf("SATURDAY\n\n"); break; + case SUN: printf("SUNDAY\n\n"); break; + default: printf("NOT A VALID DAY\n\n"); + } +} + +/////////////////////////////////////////////////// Task#09 /////////////////////////////////////////////////////// +void structures() { + printf("Task 09.1: Distance between two points\n"); + struct point { + int x; + int y; + } p1, p2; + + printf("Enter values of x1 y1 x2 y2: "); + scanf("%d %d %d %d", &p1.x, &p1.y, &p2.x, &p2.y); + + double distance = sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2)); + printf("Distance = %.2lf\n\n", distance); +} + +void power_of_two() { + printf("Task 09.2: Power of 2 using structure\n"); + struct num { + int number; + } n; + + printf("Enter a number: "); + scanf("%d", &n.number); + + if ((n.number > 0) && ((n.number & (n.number - 1)) == 0)) { + printf("Yes, %d is power of 2\n\n", n.number); + } else { + printf("No, %d is not the power of 2\n\n", n.number); + } +} + +/////////////////////////////////////////////////// Main ////////////////////////////////////////////////////////// +int main() { + task01_datatypes(); + switch_operation(); + arthmatic_operations(); + fibonaci_sequence(); + guessing_game(); + Is_prime(); + prime_numbers(); + + int n = 5; + printf("Factorial of %d is: %d\n\n", n, fictorial(n)); + + reverse_string(); + second_highest(); + file_system(); + bitwise_operations(); + ispower_of2(); + enumeration(); + structures(); + power_of_two(); + + return 0; +} From 89980ad83c132394807b2a475f970d8a710b5809 Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Tue, 26 Aug 2025 06:43:10 -0700 Subject: [PATCH 2/6] LAB#02 ALL TASKS ADDED --- Lab02/lab2_solution.txt | 441 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 441 insertions(+) create mode 100644 Lab02/lab2_solution.txt diff --git a/Lab02/lab2_solution.txt b/Lab02/lab2_solution.txt new file mode 100644 index 0000000..2bdd1e7 --- /dev/null +++ b/Lab02/lab2_solution.txt @@ -0,0 +1,441 @@ +////////////////////////////////////////// Lab#02 /////////////////////////////////////////////////////// +// Author: [Your Name] +// Description: This program demonstrates different concepts of C programming such as pointers, +// strings, macros, file I/O, linked list operations, dynamic memory allocation, and Booth multiplier. +////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +///////////////////////////////////////////// Task#01 /////////////////////////////////////////////////// +// Pointer basics, swapping, and array manipulation + +//////////////////// Task#1.1: Pointer demonstration //////////////////// +void task1_1() { + int x = 10; // Variable x declared and initialized + int *ptr = &x; // Pointer initialized with address of x + + // Printing x using direct and indirect access + printf("Direct : %d\nIndirect : %d\n", x, *ptr); + + // Modifying x through pointer + *ptr = 40; + + // Printing modified value of x + printf("Modified : %d\n", *ptr); +} + +//////////////////// Task#1.2: Swap using pointers //////////////////// +void swap(int *x, int *y) { + int temp; + temp = *x; + *x = *y; + *y = temp; +} + +//////////////////// Task#1.3: Array operations using pointers //////////////////// +void task1_3() { + int size; + printf("Enter size of an array you want to print: "); + scanf("%d", &size); + + int arr[size]; + printf("Please enter %d elements in array:\n", size); + + // Input elements + for (int i = 0; i < size; i++) { + scanf("%d", &arr[i]); + } + + int *ptr = arr; + + // Printing array using pointer arithmetic + printf("You entered these integers in array:\n"); + for (int i = 0; i < size; i++) { + printf("%d ", *(ptr + i)); + } + printf("\n"); + + // Computing sum + int sum = 0; + for (int i = 0; i < size; i++) { + sum += *(ptr + i); + } + printf("Sum of all elements of array: %d\n", sum); + + // Printing reverse order + printf("Array in reverse order:\n"); + for (int i = size - 1; i >= 0; i--) { + printf("%d ", *(ptr + i)); + } + printf("\n"); +} + +///////////////////////////////////////////// Task#02 /////////////////////////////////////////////////// +// String operations using pointers + +//////////////////// Task#2.1a: String length //////////////////// +void strlength() { + char arr[] = "Hello"; + char *ptr = arr; + int len = 0; + + while (*(ptr + len) != '\0') { + len++; + } + printf("Length of string is: %d\n", len); +} + +//////////////////// Task#2.1b: String copy //////////////////// +void strcopy() { + char a1[] = "Hello"; + char a2[50]; + char *ptr = a1; + int i = 0; + + while (*(ptr + i) != '\0') { + a2[i] = *(ptr + i); + i++; + } + a2[i] = '\0'; // Null-terminate + printf("Copied string: %s\n", a2); +} + +//////////////////// Task#2.1c: String compare (matched/unmatched) //////////////////// +void strcompare() { + char a1[] = "Hello"; + char a2[] = "Helww"; + char matched[50] = {0}; + char unmatched[50] = {0}; + int i = 0, m = 0, u = 0; + + while (a1[i] != '\0' && a2[i] != '\0') { + if (a1[i] == a2[i]) { + matched[m++] = a1[i]; + } else { + unmatched[u++] = a1[i]; + } + i++; + } + printf("Matched part: %s\n", matched); + printf("Unmatched part: %s\n", unmatched); +} + +//////////////////// Task#2.2: Check palindrome //////////////////// +void is_palindrome() { + char str[] = "MONDAY"; + char forward[50], reverse[50]; + int len = strlen(str); + + // Copy forward + strcpy(forward, str); + + // Reverse + for (int i = 0; i < len; i++) { + reverse[i] = str[len - 1 - i]; + } + reverse[len] = '\0'; + + printf("Forward: %s\n", forward); + printf("Reverse: %s\n", reverse); + + if (strcmp(forward, reverse) == 0) + printf("Yes, string is palindrome\n"); + else + printf("Not palindrome\n"); +} + +///////////////////////////////////////////// Task#03 /////////////////////////////////////////////////// +// Macros and File I/O + +//////////////////// Task#3.1: Macros //////////////////// +#define SQUARE(X) ((X) * (X)) +#define MAX2(a, b) (((a) > (b)) ? (a) : (b)) +#define MAX3(a, b, c) (MAX2(MAX2(a, b), c)) +#define MAX4(a, b, c, d) (MAX2(MAX3(a, b, c), d)) +#define TO_UPPER(c) ((c) - 'a' + 'A') + +void macros() { + printf("%d\n", SQUARE(4)); + printf("%d\n", MAX2(3, 4)); + printf("%d\n", MAX3(100, 34, 8)); + printf("%d\n", MAX4(2, 499, 899, 0)); + printf("%c\n", TO_UPPER('f')); +} + +//////////////////// Task#3.2: File I/O with structures //////////////////// +void fileio() { + struct student { + char name[1000]; + int roll; + float gpa; + }; + + int n; + printf("Enter number of students: "); + scanf("%d", &n); + + struct student students_data[n]; + + // Input student data + for (int i = 0; i < n; i++) { + printf("\nEnter data for student %d\n", i + 1); + printf("Roll no: "); + scanf("%d", &students_data[i].roll); + printf("Name: "); + scanf("%s", students_data[i].name); + printf("GPA: "); + scanf("%f", &students_data[i].gpa); + } + + // Find highest GPA + float h_gpa = 0.0; + char h_gpa_stu[1000]; + for (int i = 0; i < n; i++) { + if (students_data[i].gpa > h_gpa) { + h_gpa = students_data[i].gpa; + strcpy(h_gpa_stu, students_data[i].name); + } + } + printf("\nHighest GPA: %.2f by %s\n", h_gpa, h_gpa_stu); + + // Write to file + FILE *ptr = fopen("students.txt", "w"); + for (int i = 0; i < n; i++) { + fprintf(ptr, "%s\n", students_data[i].name); + } + fclose(ptr); + + // Read from file + FILE *ptr1 = fopen("students.txt", "r"); + char ch[60]; + printf("\nNames read from file:\n"); + while (fscanf(ptr1, "%s", ch) != EOF) { + printf("%s\n", ch); + } + fclose(ptr1); +} + +///////////////////////////////////////////// Task#04 /////////////////////////////////////////////////// +// Linked List Operations + +void linklist_operations() { + struct Node { + int data; + struct Node *next; + }; + + // Create nodes dynamically + struct Node *n1 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *n2 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *n3 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *newnode = (struct Node *)malloc(sizeof(struct Node)); + + // Assign data + newnode->data = 15; + n1->data = 23; + n2->data = 12; + n3->data = 14; + + // Linking nodes + newnode->next = n1; + n1->next = n2; + n2->next = n3; + n3->next = NULL; + + struct Node *head = newnode; + + // Delete node with key = 12 + int key = 12; + struct Node *temp = head; + struct Node *prev = NULL; + + if (temp != NULL && temp->data == key) { + head = temp->next; + free(temp); + } else { + while (temp != NULL && temp->data != key) { + prev = temp; + temp = temp->next; + } + if (temp != NULL) { + prev->next = temp->next; + free(temp); + } + } + + // Print remaining list + temp = head; + printf("Linked list after deletion: "); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); + + // Free memory + free(newnode); + free(n1); + free(n3); +} + +///////////////////////////////////////////// Task#05 /////////////////////////////////////////////////// +// Dynamic Memory Allocation + +void dynamic_mem() { + int n; + printf("Enter number of slots: "); + scanf("%d", &n); + + int *arr = (int *)malloc(n * sizeof(int)); + printf("Enter integers:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + // Print array + printf("Array: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + // Calculate sum and average + int sum = 0; + for (int i = 0; i < n; i++) { + sum += arr[i]; + } + printf("Sum: %d\n", sum); + printf("Average: %.2f\n", (float)sum / n); + + free(arr); +} + +void realloc_use() { + int n; + printf("Enter old size: "); + scanf("%d", &n); + + int *arr = (int *)malloc(n * sizeof(int)); + printf("Enter elements:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + int m; + printf("Enter new size: "); + scanf("%d", &m); + arr = (int *)realloc(arr, m * sizeof(int)); + + printf("Enter new elements:\n"); + for (int i = 0; i < m; i++) { + scanf("%d", &arr[i]); + } + + printf("Extended array: "); + for (int i = 0; i < m; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + free(arr); +} + +void mem_free_detector() { + int *ptr = (int *)malloc(5 * sizeof(int)); + if (ptr == NULL) { + printf("Memory not allocated\n"); + } else { + printf("Memory allocated\n"); + } + + for (int i = 0; i < 5; i++) { + printf("%p\n", (ptr + i)); + } + + free(ptr); + ptr = NULL; + + if (ptr == NULL) + printf("Memory freed successfully\n"); + else + printf("Memory not freed\n"); +} + +///////////////////////////////////////////// Project: Booth Multiplier ////////////////////////////////// +void booth_multiplier() { + int n; + printf("Enter bit length: "); + scanf("%d", &n); + + int A[n], Q[n], M[n], M_2scomp[n]; + printf("Enter Multiplier (Q): "); + for (int i = 0; i < n; i++) scanf("%d", &Q[i]); + + printf("Enter Multiplicand (M): "); + for (int i = 0; i < n; i++) scanf("%d", &M[i]); + + printf("Enter 2's complement of M (~M): "); + for (int i = 0; i < n; i++) scanf("%d", &M_2scomp[i]); + + for (int i = 0; i < n; i++) A[i] = 0; + + int Q_ff = 0; + + for (int step = 0; step < n; step++) { + if ((Q[n - 1] == 0 && Q_ff == 0) || (Q[n - 1] == 1 && Q_ff == 1)) { + // Do nothing, just shift + } else if (Q[n - 1] == 1 && Q_ff == 0) { + // A = A - M + int carry = 0; + for (int i = n - 1; i >= 0; i--) { + int temp = A[i] + M_2scomp[i] + carry; + A[i] = temp % 2; + carry = temp / 2; + } + } else if (Q[n - 1] == 0 && Q_ff == 1) { + // A = A + M + int carry = 0; + for (int i = n - 1; i >= 0; i--) { + int temp = A[i] + M[i] + carry; + A[i] = temp % 2; + carry = temp / 2; + } + } + + // Arithmetic right shift + Q_ff = Q[n - 1]; + for (int j = n - 1; j > 0; j--) Q[j] = Q[j - 1]; + Q[0] = A[n - 1]; + for (int j = n - 1; j > 0; j--) A[j] = A[j - 1]; + } + + printf("Result: "); + for (int i = 0; i < n; i++) printf("%d", A[i]); + for (int i = 0; i < n; i++) printf("%d", Q[i]); + printf("\n"); +} + +///////////////////////////////////////////// Main Function ///////////////////////////////////////////// +int main() { + task1_1(); + int x = 10, y = 20; + swap(&x, &y); + printf("Swapped: x=%d, y=%d\n", x, y); + task1_3(); + strlength(); + strcopy(); + strcompare(); + is_palindrome(); + macros(); + fileio(); + linklist_operations(); + dynamic_mem(); + realloc_use(); + mem_free_detector(); + booth_multiplier(); + return 0; +} + +///////////////////////////////////////////// END OF LAB#02 ///////////////////////////////////////////// From 5d9310037cf690ed2a2c781de96ddb544fdf764c Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Tue, 26 Aug 2025 06:43:26 -0700 Subject: [PATCH 3/6] LAB#03 ALL TASKS ADDED --- Lab03/functions.c | 9 + Lab03/functions.h | 6 + Lab03/hello.sh | 235 ++++++++++++++++++ .../scripts/add.sh | 5 + .../scripts/hello.sh | 2 + .../tests/test_add.sh | 4 + .../tests/test_hello.sh | 4 + Lab03/main.c | 8 + Lab03/utils.c | 8 + 9 files changed, 281 insertions(+) create mode 100644 Lab03/functions.c create mode 100644 Lab03/functions.h create mode 100644 Lab03/hello.sh create mode 100644 Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/add.sh create mode 100644 Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/hello.sh create mode 100644 Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_add.sh create mode 100644 Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_hello.sh create mode 100644 Lab03/main.c create mode 100644 Lab03/utils.c diff --git a/Lab03/functions.c b/Lab03/functions.c new file mode 100644 index 0000000..e64636c --- /dev/null +++ b/Lab03/functions.c @@ -0,0 +1,9 @@ +#include +#include "functions.h" + +void add(){ +int x=3; +int y=2; +int sum=x+y; +printf("%d",sum); +} diff --git a/Lab03/functions.h b/Lab03/functions.h new file mode 100644 index 0000000..6e529e6 --- /dev/null +++ b/Lab03/functions.h @@ -0,0 +1,6 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +void add() ; +int multipl() ; +#endif diff --git a/Lab03/hello.sh b/Lab03/hello.sh new file mode 100644 index 0000000..11c8247 --- /dev/null +++ b/Lab03/hello.sh @@ -0,0 +1,235 @@ +#!/bin/bash + +################################# Lab#01 ############################# + +########################### Task#01 ################# +########## Task1.1 ######## +echo "Hello , Word!" +########### Task#1.2 ###### +echo "Enter username" +read name +echo "Assala o alikum $name how are you?" + +############ Task#1.3 ###### +echo "Enter 2 numbers :" +num1=$1 +num2=$2 +echo "Sum of 2 numbers is : $((num1+num2))" +################################### TASK#02 ######################### +######### task#2.1 ######### +echo "Enter a number to whome we want to check either even or not" +read num +if (( $num%2==0 ));then + echo "Number is even" +else + echo "Number is odd" +fi + + + +########## task#2.2 ######### +num=$1 +for i in 1 2 3 4 5 6 7 8 9 10 +do + echo "Multiples of $num are : $((num * i))" +done + +############# task#2.3 ######### +random_number=$((RANDOM %10+1)) + + +guess=0 +while [ $guess -ne $random_number ] +do echo "Enter a guess " + read guess + if [ $guess -lt $random_number ]; then + echo "Guess is less then random number" + elif [ $guess -gt $random_number ]; then + echo "Your guess is greater then the random number" + else + echo "Comngratulations! U guessed corect number" + fi +done + +########################################### TASK#3 ######################## + +###############3 Task#3.1 ############## + +function fictorial() { +n=$1 +if [ $n -eq 0 -o $n -eq 1 ];then + echo "Fictorial of $n is : 1" +else + result=1 + for((i=2;i<=n;i++)) +do + result=$((result*i)) +done + echo "Fictorial of $n is :$result" +fi + +} +fictorial $1 +#################### Task#3.2 ######### + + + +fruits=("Apple" "Banana" "Gava") +function fruit() { +echo "${fruits[@]}" +} +fruits+=("Mango") +fruit +################## Task#3.3 ########### + +declare -A capitals + +capitals["Pakistan"]="Islamabad" +capitals["India"]="Deli" +capitals["Japan"]="Tokyo" + +function capital() { + + read country + + if [[ -v capitals[$country] ]]; then + echo "$country - ${capitals[$country]}" + else + echo "$country not exist in list" + fi +} +capital + + + +################################# TASK#4 ################################ + +####################### Task#4.1 ############ +i=0 +while IFS= read -r line; +do + echo " processing :$i $line" + i=$((i+1)) +done < "text.txt" + +###############3 Task#4.2 ################# + +File="script.log" +read username +read action +timestamp=$(date "+%Y-%m-%d %H:%M:%S") +echo "$timestamp - $username $action" >> $File +echo "Data successfully written in script.log file go and check it out" + +count=0 +while IFS= read -r line; +do + username_field=$(echo "$line" | awk '{print $4}') + echo $username_field + count=$((count+1)) +done < $File +echo "Toatal no of lines in Script.log file are :$count" + +echo "Action count per user :" +awk '{count[$4]++} END {for (user in count) print user " : " count[user]}' "$File" + +#################### Task#4.3 ############# +SOURCE_DIRECTRY="/mnt/c/Users/JK Traders Hall Road/Desktop/LAB#01" +BACKUP_NAME="backup_$(date +%Y-%m-%d).tar.gz" +DESTINATION_DIRECTRY="/mnt/c/Users/JK Traders Hall Road/Documents/backup" + +if [ -d "$SOURCE_DIRECTRY" ]; then + tar -czvf "$DESTINATION_DIRECTRY/$BACKUP_NAME" "$SOURCE_DIRECTRY" + echo "Backup created Successfully! $DESTINATION_DIRECTRY/$BACKUP_NAME" +else + echo "ERROR! Directry '$SOURCE_DIRECTRY\' does not exist." + exit 1 +fi + +#################### TASK#5.1 ################## +all: program + +program: main.o functions.o + gcc main.o functions.o -o program + +main.o: main.c functions.h + gcc -c main.c + +functions.o: functions.c functions.h + gcc -c functions.c + +clean: + rm -f *.o program + + +#################### Task#5.2 ##################33 +################################ Task#5.2 ############################# +CC=gcc +CFLAGS= -Wall -g + +SRCS= main.c functions.c utils.c ##can use SRCS=$(wildcard *.c) it will find all .c files automatically +OBJS= $(SRCS:.c=.o) +TARGET=program + +.PHONY:all clean debug + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) $(OBJS) -o $(TARGET) +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -MMD ##Pattern Rule +-include $(OBJS:.o=.d) +debug: $(CFLAGS) += -O0 ##This tells the compiletr to not do the optimizations on code cuz this optimization make debuging hard. +debug: $(TARGET) + @echo "Compiled $(TARGET) with debuging sysmbols" +clean: + rm -f $(OBJS) $(TARGET) *.d + +########################## TASK#5.3 ################################## +SCRIPTS_DIR=my_shell_scripts/scripts +TESTS_DIR=my_shell_scripts/tests +INSTALL_DIR=/mnt/c/Users/JK\ Traders\ Hall\ Road/Desktop/backup + +check: + @echo "Checking all scripts for syntax checking..." + @for script in $(SCRIPTS_DIR)/*.sh ; do \ + echo "Checking $$script"; \ + bash -n $$script || exit 1; \ + done + @echo "All Scripts passed the syntax check!" + +test: + @echo "Running tests..." + @for test in $(TESTS_DIR)/*.sh ; do \ + echo "Running $$test"; \ + bash $$test || exit 1; \ + done + @echo "All Tests passed!" + +install: + @echo "Installing Scripts to $(INSTALL_DIR)..." + @for script in $(SCRIPTS_DIR)/*.sh ; do \ + cp $$script $(INSTALL_DIR); \ + chmod +x $(INSTALL_DIR)/$$(basename $$script); \ + done + @echo "Installation complete!" + +.PHONY: check test install + + + + + + + + + + + + + + + + + diff --git a/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/add.sh b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/add.sh new file mode 100644 index 0000000..9b4fc5d --- /dev/null +++ b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/add.sh @@ -0,0 +1,5 @@ +#!/bin/bash +a=2 +b=3 +sum=$((a+b)) +echo "Sum of $a and $b is : $sum" diff --git a/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/hello.sh b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/hello.sh new file mode 100644 index 0000000..6932117 --- /dev/null +++ b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/scripts/hello.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Hello from Hello.sh" diff --git a/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_add.sh b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_add.sh new file mode 100644 index 0000000..92c4bfd --- /dev/null +++ b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_add.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -euo pipefail +out=$(bash ./my_shell_scripts/scripts/add.sh) +[ "$out" = "Sum of 2 and 3 is : 5" ] diff --git a/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_hello.sh b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_hello.sh new file mode 100644 index 0000000..fbebc7e --- /dev/null +++ b/Lab03/here are the .sh scripts and their tests used for Makefile iun task 5.3/tests/test_hello.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -euo pipefail +out=$(bash ./my_shell_scripts/scripts/hello.sh) +[ "$out" = "Hello from Hello.sh" ] diff --git a/Lab03/main.c b/Lab03/main.c new file mode 100644 index 0000000..f7f9e01 --- /dev/null +++ b/Lab03/main.c @@ -0,0 +1,8 @@ +#include "functions.h" +#include +int main() { +add (); +int result=multipl(); +printf("%d",result); +return 0; +} diff --git a/Lab03/utils.c b/Lab03/utils.c new file mode 100644 index 0000000..3d163d7 --- /dev/null +++ b/Lab03/utils.c @@ -0,0 +1,8 @@ +#include "functions.h" + +int multipl () { +int a=2; +int b=3; +int multiply=a*b; +return multiply; +} From 82a8a1f8b73510b3dd023d5db73767c3acdb8c0b Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Tue, 26 Aug 2025 06:44:06 -0700 Subject: [PATCH 4/6] LAB#05 ALL TASKS ADDED --- Lab05/design_data.tcl | 66 ++++++++++++++++++++++++++++++++++++++++++ Lab05/digital_calc.tcl | 39 +++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Lab05/design_data.tcl create mode 100644 Lab05/digital_calc.tcl diff --git a/Lab05/design_data.tcl b/Lab05/design_data.tcl new file mode 100644 index 0000000..a309a7c --- /dev/null +++ b/Lab05/design_data.tcl @@ -0,0 +1,66 @@ +# Simulating digital design data operations +# Define a list of module names +set modules {ALU Register_File Decoder Multiplexer} + +# Print all modules +puts "All modules:" +foreach module $modules { puts " $module" } + +# Add a new module +lappend modules "Control_Unit" +puts "\nAfter adding Control_Unit:" +puts $modules + +##################################### Task addinmg new modules ############# +lappend modules "pipeline" "Cache" +puts "\nAfter adding pipeline and Cache" +puts $modules + +# Remove a module +set modules [lsearch -all -inline -not $modules "Decoder"] +puts "\nAfter removing Decoder:" +puts $modules + +# Define a dict of module sizes (simulated gate count) +dict set module_sizes ALU 1000 +dict set module_sizes Register_File 5000 +dict set module_sizes Multiplexer 200 +dict set module_sizes Control_Unit 1500 +dict set module_sizes Cache 10000 +dict set module_sizes pipeline 4000 + +# Calculate total gate count +set total_gates 0 +dict for {module size} $module_sizes { + set total_gates [expr {$total_gates + $size}] +} + +puts "\nTotal gate count: $total_gates" + + +################################################# TASK print moules whose gate count is greater then the threashold ##################################### + +proc find_large_modules {threshold module_dict} { + puts "\nModules larger than $threshold gates:" + dict for {module size} $module_dict { + if {$size > $threshold} { + puts "$module : $size" + } + } +} + +# Call the procedure with threshold = 1000 +find_large_modules 1000 $module_sizes + + +# Find the largest module +set max_size 0 +set largest_module "" +dict for {module size} $module_sizes { + if {$size > $max_size} { + set max_size $size + set largest_module $module + } + } +puts "Largest module: $largest_module with $max_size gates" + diff --git a/Lab05/digital_calc.tcl b/Lab05/digital_calc.tcl new file mode 100644 index 0000000..e9fb076 --- /dev/null +++ b/Lab05/digital_calc.tcl @@ -0,0 +1,39 @@ +# Basic digital design calculations + +# Define clock frequency and calculate period + +################################################### TASK change value of frequency ##################################################################### + +# Here i change the value of clock_frequency from 100mhz to 200mhz so definately now Time period will be decreased as frequency get increased also the power got increased and result proved this expectation. + + + + + +set clock_freq_mhz 200 +set clock_period_ns [expr {1000.0 / $clock_freq_mhz}] +puts "Clock period: $clock_period_ns ns" + +# Calculate power for a simple CMOS circuit +proc calc_power {capacitance voltage frequency} { + return [expr {$capacitance * $voltage * $voltage * $frequency}] +} +set cap_pf 10.0 +set voltage 1.2 +set power_mw [calc_power $cap_pf $voltage $clock_freq_mhz] +puts "Power consumption: $power_mw mW" + +# Simple timing calculation +set prop_delay_ns 2.5 +set setup_time_ns 0.5 +set max_freq_mhz [expr {1000 / ($prop_delay_ns + $setup_time_ns)}] +puts "Maximum frequency: $max_freq_mhz MHz" + + + +###################################################### TASK ADD NEW CALCULATION ############################################### +#NEW CALCULATION of no of cycles + +set time_ns 1000 +set num_cycles [expr {$time_ns / $clock_period_ns}] +puts "Number of cycles in 1 micro sec are : $num_cycles" From 36fe98fcbb9ab1aa620077d70ddec4f0c20ba52c Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Tue, 26 Aug 2025 06:44:16 -0700 Subject: [PATCH 5/6] LAB#06 ALL TASKS ADDED --- Lab06/riscv_test/Makefile | 42 +++++++++++++++++++ Lab06/riscv_test/build/absdiff | Bin 0 -> 5912 bytes Lab06/riscv_test/build/countbits | Bin 0 -> 5872 bytes Lab06/riscv_test/build/countbits.o | Bin 0 -> 3848 bytes Lab06/riscv_test/build/example | Bin 0 -> 5912 bytes Lab06/riscv_test/build/fictorial | Bin 0 -> 5864 bytes Lab06/riscv_test/build/helloword | Bin 0 -> 6008 bytes Lab06/riscv_test/build/reverse_array | Bin 0 -> 5968 bytes Lab06/riscv_test/insertion_sort.S | 56 ++++++++++++++++++++++++++ Lab06/riscv_test/link.ld | 15 +++++++ Lab06/riscv_test/src/absdiff.S | 28 +++++++++++++ Lab06/riscv_test/src/countbits.S | 27 +++++++++++++ Lab06/riscv_test/src/example.S | 30 ++++++++++++++ Lab06/riscv_test/src/fictorial.S | 26 ++++++++++++ Lab06/riscv_test/src/helloword.S | 31 ++++++++++++++ Lab06/riscv_test/src/insertion_sort.S | 45 +++++++++++++++++++++ Lab06/riscv_test/src/reverse_array.S | 33 +++++++++++++++ 17 files changed, 333 insertions(+) create mode 100644 Lab06/riscv_test/Makefile create mode 100644 Lab06/riscv_test/build/absdiff create mode 100644 Lab06/riscv_test/build/countbits create mode 100644 Lab06/riscv_test/build/countbits.o create mode 100644 Lab06/riscv_test/build/example create mode 100644 Lab06/riscv_test/build/fictorial create mode 100644 Lab06/riscv_test/build/helloword create mode 100644 Lab06/riscv_test/build/reverse_array create mode 100644 Lab06/riscv_test/insertion_sort.S create mode 100644 Lab06/riscv_test/link.ld create mode 100644 Lab06/riscv_test/src/absdiff.S create mode 100644 Lab06/riscv_test/src/countbits.S create mode 100644 Lab06/riscv_test/src/example.S create mode 100644 Lab06/riscv_test/src/fictorial.S create mode 100644 Lab06/riscv_test/src/helloword.S create mode 100644 Lab06/riscv_test/src/insertion_sort.S create mode 100644 Lab06/riscv_test/src/reverse_array.S diff --git a/Lab06/riscv_test/Makefile b/Lab06/riscv_test/Makefile new file mode 100644 index 0000000..af59de8 --- /dev/null +++ b/Lab06/riscv_test/Makefile @@ -0,0 +1,42 @@ +# -------- RISC-V Assembly Lab Makefile ------- + PROG ?= helloword # override: make PROG=abs_diff run + +AS := riscv64-unknown-elf-as +LD := riscv64-unknown-elf-ld +OBJDUMP := riscv64-unknown-elf-objdump +SPIKE := spike + +ASFLAGS := -march=rv64imac -mabi=lp64 -g +LDFLAGS := -T link.ld + +SRC_DIR := src +BUILD_DIR := build + +all: $(BUILD_DIR)/$(PROG) + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +# assemble .S -> .o +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.S | $(BUILD_DIR) + $(AS) $(ASFLAGS) -o $@ $< + +# link .o -> ELF +$(BUILD_DIR)/%: $(BUILD_DIR)/%.o link.ld + $(LD) $(LDFLAGS) -o $@ $< + +run: $(BUILD_DIR)/$(PROG) + $(SPIKE) $< + +debug: $(BUILD_DIR)/$(PROG) + # NOTE: use two hyphens here (not an en dash) + $(SPIKE) -d --log-commits $< + +sections: $(BUILD_DIR)/$(PROG) + $(OBJDUMP) -h $< + +clean: + rm -rf $(BUILD_DIR) + +.PHONY: all run debug clean sections + diff --git a/Lab06/riscv_test/build/absdiff b/Lab06/riscv_test/build/absdiff new file mode 100644 index 0000000000000000000000000000000000000000..a856a025c78ce5301c7432a87ee1c0fe85be8141 GIT binary patch literal 5912 zcmeHLOK;Oa5T5lWp=qnAp`aF(Ln|mRp`=Mm1t(lVc}Nu$Rh%Oyb=pXI$#$xi0~iqe zfgZ{YE|vHLNSu+l^BedFh$9D>*@tayQh_*oCC|)$Gqbb%dF{izSl_s3F$Q{A@DtQ5 zB+Mp(WA=>HSSTl90yscFgV@HD^qaD30J;+CQ{;>DLYm?TNo-loI#QW3 zkTQ@mkTQ@mkTQ@mkTQ@mkTQ@mkTQ@mkTUSk49sK!_+mD5g!=XISH7J4WH%n>;K%|v zd}Z+qAHG2TPoBHuhcr# z{!+~$t3lSYPG``r^q1fzMp>|clCfWfag?GJ!F;a);MD!?RbNsJhH(sF#z@@}u=mOBG~*J(QKR#}i)3tT^d`;VT^ zuWij&ODkqeWoT6{2@6dJ$DP@ULuo&!F3yO-0r?p*I2eakF&uIuqVayoPS22!#~ejI zdIK5w@jC*PF(LHu{$=~fg1g4K&Z_ta27X@P;ys7LRN}V6FB$Q76uxTUxGuJ&hS!O1 zZ4bJGPK87jJl7wz1Gu>V*AJrW?!b1h>q5;B94`PU`3IfAsiO`&sdu#%xcdQ=ww=I% zlDH@(CkVV&eSr4`akoC$skK|Uma<#jW>4Erz3#bt(adwYJ8=>gP^|pFn!{9LCvhV} zv6|}l*#*tI8<2TO(Zb-|6p!E0SQz0Db0__%)f=vCPoW9 zSM}eB)nZ}BuA|GuD4G{#PD!drnz2vNV`3CsQTeJJdt!d=g#1mFzZ7LkLmewZyDHy| z3swG&C{h}FuSspbQ;Pho^53bzam1*${#(vc5RN=P`rQjaiOp$gJWhgW>$)1IGNba} MtNa5UsI-~?2O)Hu_5c6? literal 0 HcmV?d00001 diff --git a/Lab06/riscv_test/build/countbits b/Lab06/riscv_test/build/countbits new file mode 100644 index 0000000000000000000000000000000000000000..0fb7141dd56480e0cf75379a4a6c5723bcf99973 GIT binary patch literal 5872 zcmeHLO>5Lp6umELby{_79YLWWl`4u#rX2>YP^3kx3q_FPIwaGy4NN{rGNpB)u`c`p zUAPt8_%HkgfZOaWM`%FkMHG#OOX(uK zL6rpy7#aK7p2o;q4=m3H0E1){7k*ge&W30~OyRwWZombbO|^hwoWN*fLSezM2BWg1 zx46Ec!E4;~2EPpq&{F+~lCJu4JHphLxQRbjvRRlpG0V;|yFJ&w+U`tJT8&!>=r(aF z9x;>zaH+8apS0V2(C$tW1>I)Sp|k`y+RIBuZ z^ZhK?t*$aJ-mZ&qgQhM2jpwkETyC6v$rqD-JpH{F-v(Fka2X@jt2Yq!B_;Z@zWR!Y z#KBKgKpyFT25Zv>#i8my>Ys{UPv@?4v_LHPf6G^^gRWi2l+Ka*hpcz%QjT=(Gh}s+ zgbU&YhV#6t4!YLGl+KZQRn{-or5wrIs@ND2)$6)c*59d=0;s*3vwo(e{3z>7DV#>` qEdJDaUL+BYDg3Fk=Y$N`Wq2`sn~?42Dqc+q4*5DtWxEL^zE_RRLOlgwATdp4^v zxWWO??!haG7mwaNdi7$$PeDC;p`Pw4sD7tA9IVN7)%(=*R@M92{r&Y@U)4O1R6P2Z zUbYk^`txXR!?&K9>$FHG=;s>I!NVH)ztrf5FZ*?$nh%=v&p~seNq_yJ-g8f4I;cNg z@UAC$82X#_BmURu96vqpzs6C1W`%f3+$ZW!X5+LsNRwbAFX#wo%H_209pOIm^o?!t z+_v7D%j7mE{g|J5?0VVAC26j_eUC?dXwf_A`HeG;^Nr?0!B3Z71}i!$T0GVjJJgk( z$tYSG&61sPI~)#H;z82i?WR$Z(v919TURz(onWm)S5`S*<|FSTHW%XE%WH$qbhSI` zOxxWMRv*^4qtR@*I&D{^FTj53$`kqgD-)kyn}a{gka>PSv-3Uj>6@ST!Tbd97%jfd$!C+}#L@uQ_}) zeL6gcD&KV|?#9V+cU82WV|;@YMfn$taMtn(2XunOxI53_`6QQ|)5)j# z`g8sp4wL8$MoyHSqw#D+OM9<8ropXti-wcQl(r`0sGD64)J@VbP7AqCb8CLoM4uK| z^x}kKAAV?jdU9R9?t84u>xQrU73ay$lFlRQ(U|C_xEud0oGhrP1w%w}m#_O37qs1t zpPPPl4Z>P+oEOW#YWQ{+DGt7kbJOtc>MIVujdR=Z?XFN9d>iME;p?0OQXG66=bqso z7aK}(@NJw;I9V{S_Ka-;WC7pC+0M|MzbH18;^5mjL*XF4y%QA&-^PgzzbQ79;^5mj zd%{8dQ({9Y4!(`^KseyPDK?bi;M+J4g#*65yA=oD#yKQ>ypi4)8%oy{xbAu2I#)He zvDa192L6tMtXpCu&bwkmEr|{MjMz}iVguK+3CPxoGpX{bdXUG~qje&W+F7h1`DT=c zJqnUE&h?I3aWo8rG}=ok*b38-f?kqPkfB>qZ+5>s9E_s^Fc@!7n4CGoUN4Sz3+^xu z$M++Skux|CyYWF3glQTNdNckm(gFTY-Qxb3OPxOrw=Tw-0-~{gR|JE(Y_HS}Y zKNB2h6nW~>*15%g1MYq~rNUFbG&8Q+(3kiRj@$n`I77vH00 z<9{L(`lC!3#*Zn{@#y$(l*D9+d?_kG!-!k!RrZzTUzSVySLuuVcrUp5HyCqcqRXm) z_U-`ga<@w`@V*ve8x13R9u*4ND69Xe)W0c2)Q|VCTmOtPq;m7@OaA=3oCzWVzZl0p zR0`{5IYKp*8U2k?Vbtvo(XYJHYVOVOZ<21$1vT!%Mk6z`MHLM5jQ{HHD&$V N^0D%h=?l~C{~Yq_vpoO+ literal 0 HcmV?d00001 diff --git a/Lab06/riscv_test/build/example b/Lab06/riscv_test/build/example new file mode 100644 index 0000000000000000000000000000000000000000..cb1cd246dc2d768382016ad443cb5329e6ef7723 GIT binary patch literal 5912 zcmeHL&2AGh5FW=#N%#o~sZ^w1QniQ{p-mbU)B}g6sBkF=sc_&FZQ5XaOWL(53cYUh$9D>@!HwNNd*#DTiTiNH}l!?cy^`Cn}fp#ImSSb9Q*(^iwWB! zf#X{((d65MUC5!l0+)dUjPr=?RuaD{TLz$aB7K2;DKDlej+n$I2^?By;u43IHUl;T zHUl;THUl;THUl;THUl;THUl;THUl;T|D1u3`&;)vzw5$E8K74F!q)4r;r;#!{;jX^ zb++C-0`_UqV(o22Lq9s60=tQ3A)Mab@mu5VPS6@RJ1$wrWW5T4$zXfj1RtYvu!fSc zpXG}vW!r&8;W2=B<_6<|=iqi20Hqv?n5<>aaKd;_N?yvN%}Pa-+9q#;F!BMdu-Y>~{Z%af$Uri0@z%N|JkId)~RZ?Ygh zuS5RO95M&acsML&W~s@O&P@s^i$ie^BY;C`4yRkm!}njN%JDb1HIyBCGF@i8&NU#N`(nq-C<`v`p|M&Vx-KdI+*0W3zS7kG{X_ l6oiAm7`k>OphQvip%1el+In9N(>9CvPZj^I4piFY{|4(tltusm literal 0 HcmV?d00001 diff --git a/Lab06/riscv_test/build/fictorial b/Lab06/riscv_test/build/fictorial new file mode 100644 index 0000000000000000000000000000000000000000..ee6fc9994423959b2cc2242136bc557bf65ac5cb GIT binary patch literal 5864 zcmeHL&2G~`5T3Q$(iRB)K~U(Ws30UjP8upGw^mi*0ti&#>N=^@8aZ)fZ>kmvVdx|D zP>u*5gje7NxFc~w91((<{c+4D5=dNkC4W2n&CKrl+jXSOtBw129mYU62fl+E<;1NL zVEZyFd-AQq3OI z6|f3e1*`&A0jq#jz$#!BunJfOtOEb6z_9~x^u}=y-#ak6-hB25Zn~S!&za93kMXbl z(cuTS_^t^@k}a*Hws|Mm4n%*;I|Vjt|S(G)#?6vl-@A<||h!v-INFZs0>( z!$|}p+r4ZzXa#Xp6%zV9%sAY8_;g`ybD{1nn?)02W>-p- z8#LBpoIhSh`(3Ki3~4T)E<>6tPI=Jb!q^~*j4LZS13tH}QQ;%yN8sD9f>6e3(GTwb zjN+F|heXsl!HaXug{k0<;wwh{y5cVy`~&2thUwWqv;;bkhFLG>aC!f)*Sr-pxgRG< z7q*j5=+hAVj0Y(P@Unw84;t9>RQ9{t;^96AZ#&=tc;ZfY0q1Gd=;75sK5X=M{5a}_ z+KxJ{MB725k%oK4C=EJ0{UlsKN%LRbJZ6*Hr5lio303{hy#MkW5fK6|AyU4+i5Q=g zqo(qUuZc)(;$)>MkbYw@w_Bowsq2pBCu8Q*y)ZeN;JI4=wV16BX6y#KOpc;<7D+l0(#(2su1NAIfj_bp~x}AOC4J43g`;x z3g`;x3g`;x3g`;x3g`;x3g`;x3g`;_a|KRKC||)p@fCJ#9y11=PYkLjA-vi@h*Ku8cWA&vUy4|&f*(r855f2AZXAH(oH0Q$c?X5v0*lc-?px&~{>X7xu z^G5DwP=`ZgnNUS-7(Yu3sChfWh{6s)pHd$BVb{VRfD|a2s1mX&%qSMXV5~TgIs{$Hz#FWhkFpHZB>gv{JfJs?3oa zsd5RLk|d-EpC!1fT6R;lzVA6TZxr?I1KS5J8!z14=br8ZHNg;`pf zM1iq4W1iWH^fUA{Gh3YE)XB*U1HRRqgTc8RMgU7YJ2@YN`5cz=i#eRmqX6H3tBxfO z@Y71NzB-@e_q#3li^_iQ3;ddb?{Yj}pJxI`f5J_n_HiuFs{izmeYk$~*SElPqR8$! z5QKwa+;V+CfUZAuAolw{-i3qE_qfrDVmpihtmw5D+ie`}xf`Y)UsZL_e%(E=^>pdpvcnkY;lNK& zUP{;k2^_2Eq^8&c+yDpV88{6bpi@9>eMH7hSv3H?66sSE%j!~caik>HN#M{rmnQ>O zS_UiwmI2FvWxz6E8L$jk1}p=X0n318z%p=R23|P;htGn-1pbld&81mA!O!roO*FEPfhbOEIoR|K@Qb>IMLH0|wNyIEzM9 zk88WYt`HXOUZ1a5JF{N1+L`fuvT9^KX*Ro!+0G2S!7K+Tm$6@^VU(gd!Q!ZfGljch z6o;N4MgG3KCL0w)v~Y?KOmH?-1lE%Uv>gl+LN?3UJZI~u%*jhxOY&$wA~++y+*Mqb z)(0r+&PGb!vU(-5sdG}nhox*3Mg~XO7~`eM(&f@P(PWHk=&K2JBqakU>^qQ`ib+dl zyWI>c&2GHyZ~BdTMQnhVgmD5Z_a9C#u1#0nd9yLoXLc?LPR$U-GPmEEA-Fc-83n+H}UlHHLcJ2hEVm8$$H1C1DDmHfH@z%r-Ha;6SbaPRy1GGj|C?CPvwJ<-r9vD;1WR`v6fBqr|4FS8ZHT zOTx_c`e2lIpz3GH$qn_hdPoZ`n$yhtpz3e+GNqx{&ueo}Mdp~yPgUYDx@YmxH(&4T i5Mp}oWrdXKxYUpGAliCe4byA(tN*O(_jRJuX8m6Y5vX7Q literal 0 HcmV?d00001 diff --git a/Lab06/riscv_test/insertion_sort.S b/Lab06/riscv_test/insertion_sort.S new file mode 100644 index 0000000..792a19a --- /dev/null +++ b/Lab06/riscv_test/insertion_sort.S @@ -0,0 +1,56 @@ + .data +array: .word 5,2,4,6,1,3 +size: .word 6 + + .text + .globl _start +_start: + # Load size + la t0, size + lw t1, 0(t0) # t1 = size + li t2, 1 # i = 1 + +outer_loop: + bge t2, t1, done_sort + + # key = array[i] + la t3, array + slli t4, t2, 2 + add t5, t3, t4 + lw t6, 0(t5) # t6 = key + + # j = i - 1 + addi t7, t2, -1 + +inner_loop: + blt t7, x0, insert_key + + # array[j] + slli t8, t7, 2 + add t9, t3, t8 + lw t10, 0(t9) + + blt t10, t6, insert_key + + # array[j+1] = array[j] + addi t11, t9, 4 + sw t10, 0(t11) + + addi t7, t7, -1 + j inner_loop + +insert_key: + # array[j+1] = key + slli t8, t7, 2 + add t9, t3, t8 + addi t9, t9, 4 + sw t6, 0(t9) + + addi t2, t2, 1 + j outer_loop + +done_sort: + li a7, 93 + li a0, 0 + ecall + diff --git a/Lab06/riscv_test/link.ld b/Lab06/riscv_test/link.ld new file mode 100644 index 0000000..ce0dd07 --- /dev/null +++ b/Lab06/riscv_test/link.ld @@ -0,0 +1,15 @@ +OUTPUT_ARCH("riscv") +ENTRY(_start) + +SECTIONS +{ + . = 0x80000000; + + .text : { *(.text) } + .data : { *(.data) } + .bss : { *(.bss) } + .tohost : { *(.tohost) } + + /DISCARD/ : { *(.comment) *(.note*) } +} + diff --git a/Lab06/riscv_test/src/absdiff.S b/Lab06/riscv_test/src/absdiff.S new file mode 100644 index 0000000..43676d2 --- /dev/null +++ b/Lab06/riscv_test/src/absdiff.S @@ -0,0 +1,28 @@ +.section .data +num1: .word 25 +num2: .word 40 +result: .word 0 + +.section .text +.globl _start +_start: + lw a0, num1 + lw a1, num2 + + sub a2, a0, a1 + bltz a2, neg + + la t0, result + sw a2, 0(t0) + j done + +neg: + sub a2, a1, a0 + la t0, result + sw a2, 0(t0) + +done: + li a7, 93 + li a0, 0 + ecall + diff --git a/Lab06/riscv_test/src/countbits.S b/Lab06/riscv_test/src/countbits.S new file mode 100644 index 0000000..a891c6e --- /dev/null +++ b/Lab06/riscv_test/src/countbits.S @@ -0,0 +1,27 @@ +# src/countbits.S + .data +count: .word 0 # store number of set bits +num: .word 0xF1 # example number + + .text + .globl _start +_start: + la t0, num + lw t1, 0(t0) # load number into t1 + li t2, 0 # t2 = counter + +loop: + beq t1, x0, done_count + andi t3, t1, 1 + add t2, t2, t3 + srli t1, t1, 1 + j loop + +done_count: + la t0, count + sw t2, 0(t0) + + li a7, 93 + li a0, 0 + ecall + diff --git a/Lab06/riscv_test/src/example.S b/Lab06/riscv_test/src/example.S new file mode 100644 index 0000000..8bd1348 --- /dev/null +++ b/Lab06/riscv_test/src/example.S @@ -0,0 +1,30 @@ + .section .text + .globl _start + +_start: + # Initialize registers with some values + li t0, 5 # Load 5 into t0 + li t1, 10 # Load 10 into t1 + + add t2, t0, t1 # t2 = t0 + t1 (5 + 10 = 15) + + # Store result at a memory location + la t3, result + sw t2, 0(t3) + + # ---- Spike exit stub ---- + li t0, 1 + la t1, tohost + sd t0, (t1) +1: j 1b + +# ---- Data section ---- + .section .data +result: + .word 0 + + .section .tohost + .align 3 +tohost: .dword 0 +fromhost: .dword 0 + diff --git a/Lab06/riscv_test/src/fictorial.S b/Lab06/riscv_test/src/fictorial.S new file mode 100644 index 0000000..0de597b --- /dev/null +++ b/Lab06/riscv_test/src/fictorial.S @@ -0,0 +1,26 @@ +# src/factorial.S + .data +n: .word 5 +result: .word 1 + + .text + .globl _start +_start: + la t0, n + lw t0, 0(t0) # t0 = n + li t1, 1 # t1 = factorial accumulator + +fact_loop: + blez t0, done_fact + mul t1, t1, t0 + addi t0, t0, -1 + j fact_loop + +done_fact: + la t2, result + sw t1, 0(t2) + + li a7, 93 + li a0, 0 + ecall + diff --git a/Lab06/riscv_test/src/helloword.S b/Lab06/riscv_test/src/helloword.S new file mode 100644 index 0000000..63d1e85 --- /dev/null +++ b/Lab06/riscv_test/src/helloword.S @@ -0,0 +1,31 @@ +.global _start + +.section .text +_start: + li t0, 0x10000000 # HTIF base address + la t1, message # Load address of message + +print_loop: + lb t2, (t1) # Load byte from message + beqz t2, done # If byte is zero, exit loop + sw t2, 0(t0) # Write byte to HTIF + addi t1, t1, 1 # Move to next byte + j print_loop + +done: + # Signal test pass to Spike + li t0, 1 + la t1, tohost + sd t0, (t1) + + # Loop forever +1: j 1b + +.section .data +message: + .string "Hello, World!\n" + +.section .tohost +.align 3 +tohost: .dword 0 +fromhost: .dword 0 diff --git a/Lab06/riscv_test/src/insertion_sort.S b/Lab06/riscv_test/src/insertion_sort.S new file mode 100644 index 0000000..014425e --- /dev/null +++ b/Lab06/riscv_test/src/insertion_sort.S @@ -0,0 +1,45 @@ + .data +array: .word 5,2,4,6,1,3 +size: .word 6 + + .text + .globl _start +_start: + la t0, size + lw t1, 0(t0) # t1 = size + li t2, 1 # i = 1 + +outer_loop: + bge t2, t1, done_sort + + la t3, array + slli t4, t2, 2 + add t5, t3, t4 + lw t6, 0(t5) # key = array[i] + + addi t7, t2, -1 # j = i-1 + +inner_loop: + blt t7, x0, insert_key + slli t8, t7, 2 + add t9, t3, t8 + lw t10, 0(t9) # array[j] + blt t10, t6, insert_key + sw t10, 4(t9) + addi t7, t7, -1 + j inner_loop + +insert_key: + slli t8, t7, 2 + add t9, t3, t8 + addi t9, t9, 4 + sw t6, 0(t9) + + addi t2, t2, 1 + j outer_loop + +done_sort: + li a7, 93 + li a0, 0 + ecall + diff --git a/Lab06/riscv_test/src/reverse_array.S b/Lab06/riscv_test/src/reverse_array.S new file mode 100644 index 0000000..1d56081 --- /dev/null +++ b/Lab06/riscv_test/src/reverse_array.S @@ -0,0 +1,33 @@ +# src/reverse_array.S + .data +array: .word 1, 2, 3, 4, 5 +len: .word 5 + + .text + .globl _start +_start: + la t0, array # start address + lw t1, len # length + add t1, t1, x0 # t1 = len + addi t2, x0, -1 # t2 = end index (will adjust) + + add t3, t0, t1 # pointer to end element (not exact yet) + slli t1, t1, 2 # multiply length by 4 (word size) + add t3, t0, t1 # t3 = address of end element + 4 + addi t3, t3, -4 # t3 = address of last element + +rev_loop: + bge t0, t3, done_rev + lw t4, 0(t0) + lw t5, 0(t3) + sw t5, 0(t0) + sw t4, 0(t3) + addi t0, t0, 4 + addi t3, t3, -4 + j rev_loop + +done_rev: + li a7, 93 + li a0, 0 + ecall + From 7f11f1b83722d14f119a5727a3899196fb7683f3 Mon Sep 17 00:00:00 2001 From: mushaf23 Date: Fri, 5 Sep 2025 17:14:02 -0700 Subject: [PATCH 6/6] Reorganized Week-1: day-x/task-y structure, renamed READMEs, moved code and tests --- MushafAli/Day01/Readme.txt | 193 ++++++++++++ MushafAli/Day01/lab1_solution.txt | 288 ++++++++++++++++++ MushafAli/Day02/Readme.txt | 172 +++++++++++ MushafAli/Day02/lab2_solution.txt | 441 ++++++++++++++++++++++++++++ MushafAli/Day03/Readme.txt | 160 ++++++++++ MushafAli/Day03/functions.c | 9 + MushafAli/Day03/functions.h | 6 + MushafAli/Day03/hello.sh | 235 +++++++++++++++ MushafAli/Day03/main.c | 8 + MushafAli/Day03/scripts/add.sh | 5 + MushafAli/Day03/scripts/hello.sh | 2 + MushafAli/Day03/tests/test_add.sh | 4 + MushafAli/Day03/tests/test_hello.sh | 4 + MushafAli/Day03/utils.c | 8 + MushafAli/Day04/Readme.txt | 41 +++ MushafAli/Day04/design_data.tcl | 66 +++++ MushafAli/Day04/digital_calc.tcl | 39 +++ MushafAli/Day05/Makefile | 42 +++ MushafAli/Day05/Readme.txt | 34 +++ MushafAli/Day05/build/absdiff | Bin 0 -> 5912 bytes MushafAli/Day05/build/countbits | Bin 0 -> 5872 bytes MushafAli/Day05/build/countbits.o | Bin 0 -> 3848 bytes MushafAli/Day05/build/fictorial | Bin 0 -> 5864 bytes MushafAli/Day05/link.ld | 15 + MushafAli/Day05/src/absdiff.S | 28 ++ MushafAli/Day05/src/countbits.S | 27 ++ MushafAli/Day05/src/fictorial.S | 26 ++ 27 files changed, 1853 insertions(+) create mode 100644 MushafAli/Day01/Readme.txt create mode 100644 MushafAli/Day01/lab1_solution.txt create mode 100644 MushafAli/Day02/Readme.txt create mode 100644 MushafAli/Day02/lab2_solution.txt create mode 100644 MushafAli/Day03/Readme.txt create mode 100644 MushafAli/Day03/functions.c create mode 100644 MushafAli/Day03/functions.h create mode 100644 MushafAli/Day03/hello.sh create mode 100644 MushafAli/Day03/main.c create mode 100644 MushafAli/Day03/scripts/add.sh create mode 100644 MushafAli/Day03/scripts/hello.sh create mode 100644 MushafAli/Day03/tests/test_add.sh create mode 100644 MushafAli/Day03/tests/test_hello.sh create mode 100644 MushafAli/Day03/utils.c create mode 100644 MushafAli/Day04/Readme.txt create mode 100644 MushafAli/Day04/design_data.tcl create mode 100644 MushafAli/Day04/digital_calc.tcl create mode 100644 MushafAli/Day05/Makefile create mode 100644 MushafAli/Day05/Readme.txt create mode 100644 MushafAli/Day05/build/absdiff create mode 100644 MushafAli/Day05/build/countbits create mode 100644 MushafAli/Day05/build/countbits.o create mode 100644 MushafAli/Day05/build/fictorial create mode 100644 MushafAli/Day05/link.ld create mode 100644 MushafAli/Day05/src/absdiff.S create mode 100644 MushafAli/Day05/src/countbits.S create mode 100644 MushafAli/Day05/src/fictorial.S diff --git a/MushafAli/Day01/Readme.txt b/MushafAli/Day01/Readme.txt new file mode 100644 index 0000000..f85e058 --- /dev/null +++ b/MushafAli/Day01/Readme.txt @@ -0,0 +1,193 @@ +Lab 01 – C Programming Fundamentals + +This project contains the solutions to **Lab 01** tasks for practicing the fundamentals of the C programming language. +The lab covers essential concepts such as data types, arithmetic operations, loops, conditionals, recursion, file handling, bitwise operations, enumerations, and structures. + +--- + +## Problem Statement + +The purpose of this lab is to strengthen understanding of **basic programming constructs in C**. +The tasks gradually build from simple operations (data types, arithmetic) to more advanced concepts like recursion, file I/O, and structures. + +By completing these tasks, I practise practices: + +- Declaring and using variables of different data types +- Performing arithmetic operations and handling user input +- Implementing loops and conditionals +- Generating sequences and games using random numbers +- Understanding recursion with factorial +- Manipulating strings and arrays +- Writing to and reading from files +- Applying bitwise operations +- Using enumerations for better code readability +- Working with structures to represent complex data + +--- + +## Task List + +| Task | Description | +|------|-------------| +| **Task 01** | Data types and their memory sizes | +| **Task 02** | Arithmetic operations and calculator using `switch` | +| **Task 03.1** | Fibonacci sequence generation | +| **Task 03.2** | Number guessing game | +| **Task 04.1** | Prime number check | +| **Task 04.2** | Printing all prime numbers up to 100 | +| **Task 04.3** | Factorial calculation using recursion | +| **Task 05.1** | String reversal | +| **Task 05.2** | Find second highest element in an array | +| **Task 06** | File handling: write and read numbers | +| **Task 07.1** | Bitwise operations (AND, OR, XOR, shifts) | +| **Task 07.2** | Check if a number is a power of 2 | +| **Task 08** | Enumeration of weekdays | +| **Task 09.1** | Distance between two points using structures | +| **Task 09.2** | Power of 2 check using structures | + +--- + +## Approach + +- Each task is written as a separate function and calling each task in `main.c`. +- The `main()` function sequentially calls all task functions to demonstrate their outputs. +- Important concepts covered: + - **Recursion** → Factorial calculation + - **Loops** → Fibonacci, prime numbers + - **Switch-case** → Arithmetic calculator, enumeration + - **File I/O** → Writing and reading integers + - **Bitwise operations** → Efficient mathematical checks + - **Structures** → Distance calculation, number check + - **Enumerations** → Mapping integers to days of the week + +--- + +## How to Compile and Run + +1. Make sure you have **GCC** installed. +2. Open a terminal in the project directory. +3. Compile the program: + + ```bash + gcc main.c -o lab1 -lm + + + + + +## Example Outputs + + + +###Task 01: Data Types and Sizes + +size of int is 4 +size of float is 4 +size of double is 8 +size of char is 1 + + +###Task 02 – Arithmetic Operations + +Enter 1st positive number: 10 +Enter 2nd positive number: 3 +sum=13 sub=7 mul=30 div=3.33 mod=1 + +###Task 03.1 – Fibonacci Sequence + +Enter a number: 7 +0 1 1 2 3 5 8 + +###Task 03.2 – Guessing Game + +Guess a number between 1 to 100: 42 +YOU FAIL! Your guess is less than the random number (87) + +###Task 04 – Prime and Factorial +Enter a number: 17 +Yes, it's a prime number + +Prime numbers up to 100: +2 3 5 7 11 ... 97 + +Factorial of 5 is: 120 + + +###Task 05 – Strings and Arrays + +Reverse of "Hello" is: olleH +Second highest is 4 + + +###Task 06 – File Handling + +1 2 3 4 5 6 7 8 9 10 + +###Task 07 – Bitwise Operations + +Enter x: 5 +Enter y: 2 +AND = 0 +OR = 7 +XOR = 7 +Shift Left = 20 +Shift Right = 1 + +Enter a number: 16 +Yes, 16 is the power of 2 + +###Task 08 – Enumeration (Days of Week) + +Enter a number (1-7): 3 +WEDNESDAY + +###Task 09 – Structures + +Enter values of x1 y1 x2 y2: 0 0 3 4 +Distance = 5.00 +Enter a number: 32 +Yes, 32 is power of 2 + + + + + +## Key Learnings +- Learned how to use data types and memory sizes +- Practiced recursion and iterative methods +- Worked with file input/output in C +- Applied bitwise operations for efficient checks +- Improved understanding of structures and enums + + + + + + + + +########### AI USAGE ############### +prefer google for syntax confirmation +use chatgpt for checking which function used to generate the random numbers etc + + + + + + + + + + + + + + + + + + + + + + diff --git a/MushafAli/Day01/lab1_solution.txt b/MushafAli/Day01/lab1_solution.txt new file mode 100644 index 0000000..203a80f --- /dev/null +++ b/MushafAli/Day01/lab1_solution.txt @@ -0,0 +1,288 @@ +#include +#include +#include +#include +#include + +/////////////////////////////////////////////////// Task#01 /////////////////////////////////////////////////////// +void task01_datatypes() { + printf("Task 01: Data Types and Sizes\n"); + printf("size of int is %ld\n", sizeof(int)); + printf("size of float is %ld\n", sizeof(float)); + printf("size of double is %ld\n", sizeof(double)); + printf("size of char is %ld\n\n", sizeof(char)); +} + +/////////////////////////////////////////////////// Task#02 /////////////////////////////////////////////////////// +void arthmatic_operations() { + printf("Task 02: Arithmetic Operations\n"); + int x, y; + printf("Enter 1st positive number: "); + scanf("%d", &x); + printf("Enter 2nd positive number: "); + scanf("%d", &y); + + int add = x + y; + int sub = x - y; + int mul = x * y; + double div = (double)x / y; + int mod = x % y; + + printf("sum=%d sub=%d mul=%d div=%.2f mod=%d\n\n", add, sub, mul, div, mod); +} + +void switch_operation() { + printf("Task 02: Switch Operation\n"); + int a, b, operation; + printf("Enter a: "); + scanf("%d", &a); + printf("Enter b: "); + scanf("%d", &b); + printf("Choose operation (0-add, 1-sub, 2-mul, 3-div, 4-mod): "); + scanf("%d", &operation); + + switch (operation) { + case 0: printf("Sum = %d\n\n", a + b); break; + case 1: printf("Subtraction = %d\n\n", a - b); break; + case 2: printf("Multiplication = %d\n\n", a * b); break; + case 3: printf("Division = %.2f\n\n", (double)a / b); break; + case 4: printf("Modulus = %d\n\n", a % b); break; + default: printf("Invalid operation\n\n"); + } +} + +/////////////////////////////////////////////////// Task#03 /////////////////////////////////////////////////////// +void fibonaci_sequence() { + printf("Task 03.1: Fibonacci Sequence\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + int a = 0, b = 1, result; + printf("%d ", a); + + for (int i = 2; i <= n; i++) { + result = a + b; + a = b; + b = result; + printf("%d ", a); + } + printf("\n\n"); +} + +void guessing_game() { + printf("Task 03.2: Guessing Game\n"); + srand(time(0)); + int random_number = (rand() % 100) + 1; + int guess; + printf("Guess a number between 1 to 100: "); + scanf("%d", &guess); + + if (random_number > guess) + printf("YOU FAIL! Your guess is less than the random number (%d)\n\n", random_number); + else if (random_number < guess) + printf("YOU FAIL! Your guess is greater than the random number (%d)\n\n", random_number); + else + printf("Congratulations! Your guess matches the random number\n\n"); +} + +/////////////////////////////////////////////////// Task#04 /////////////////////////////////////////////////////// +void Is_prime() { + printf("Task 04.1: Check Prime\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + if (n <= 1) { + printf("Not a prime number\n\n"); + return; + } + + int isprime = 1; + for (int i = 2; i <= n / 2; i++) { + if (n % i == 0) { + isprime = 0; + break; + } + } + if (isprime) + printf("Yes, it's a prime number\n\n"); + else + printf("Not a prime number\n\n"); +} + +void prime_numbers() { + printf("Task 04.2: Prime Numbers up to 100\n"); + for (int i = 2; i <= 100; i++) { + int isprime = 1; + for (int j = 2; j * j <= i; j++) { + if (i % j == 0) { + isprime = 0; + break; + } + } + if (isprime) { + printf("%d ", i); + } + } + printf("\n\n"); +} + +int fictorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * fictorial(n - 1); +} + +/////////////////////////////////////////////////// Task#05 /////////////////////////////////////////////////////// +void reverse_string() { + printf("Task 05.1: Reverse String\n"); + char arr[] = "Hello"; + int i = strlen(arr) - 1; + while (i >= 0) { + printf("%c", arr[i]); + i--; + } + printf("\n\n"); +} + +void second_highest() { + printf("Task 05.2: Second Highest Element\n"); + int arr[] = {1, 2, 3, 4, 5}; + int first = arr[0], second = -1; + for (int i = 1; i < 5; i++) { + if (arr[i] > first) { + second = first; + first = arr[i]; + } else if (arr[i] > second && arr[i] < first) { + second = arr[i]; + } + } + printf("Second highest is %d\n\n", second); +} + +/////////////////////////////////////////////////// Task#06 /////////////////////////////////////////////////////// +void file_system() { + printf("Task 06: File System\n"); + int number[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + FILE *ptr = fopen("numbers.txt", "w"); + for (int i = 0; i < 10; i++) { + fprintf(ptr, "%d\n", number[i]); + } + fclose(ptr); + + FILE *ptr1 = fopen("numbers.txt", "r"); + int n[50]; + for (int i = 0; i < 10; i++) { + fscanf(ptr1, "%d", &n[i]); + printf("%d ", n[i]); + } + fclose(ptr1); + printf("\n\n"); +} + +/////////////////////////////////////////////////// Task#07 /////////////////////////////////////////////////////// +void bitwise_operations() { + printf("Task 07.1: Bitwise Operations\n"); + int x, y; + printf("Enter x: "); + scanf("%d", &x); + printf("Enter y: "); + scanf("%d", &y); + + printf("AND = %d\n", x & y); + printf("OR = %d\n", x | y); + printf("XOR = %d\n", x ^ y); + printf("Shift Left = %d\n", x << y); + printf("Shift Right = %d\n\n", x >> y); +} + +void ispower_of2() { + printf("Task 07.2: Power of 2 Check\n"); + int n; + printf("Enter a number: "); + scanf("%d", &n); + + if ((n > 0) && ((n & (n - 1)) == 0)) { + printf("Yes, %d is the power of 2\n\n", n); + } else { + printf("No, %d is not the power of 2\n\n", n); + } +} + +/////////////////////////////////////////////////// Task#08 /////////////////////////////////////////////////////// +void enumeration() { + printf("Task 08: Enumeration\n"); + enum WEEKDAYS {MON = 1, TUE, WED, THR, FRI, SAT, SUN}; + int number; + printf("Enter a number (1-7): "); + scanf("%d", &number); + + switch ((enum WEEKDAYS)number) { + case MON: printf("MONDAY\n\n"); break; + case TUE: printf("TUESDAY\n\n"); break; + case WED: printf("WEDNESDAY\n\n"); break; + case THR: printf("THURSDAY\n\n"); break; + case FRI: printf("FRIDAY\n\n"); break; + case SAT: printf("SATURDAY\n\n"); break; + case SUN: printf("SUNDAY\n\n"); break; + default: printf("NOT A VALID DAY\n\n"); + } +} + +/////////////////////////////////////////////////// Task#09 /////////////////////////////////////////////////////// +void structures() { + printf("Task 09.1: Distance between two points\n"); + struct point { + int x; + int y; + } p1, p2; + + printf("Enter values of x1 y1 x2 y2: "); + scanf("%d %d %d %d", &p1.x, &p1.y, &p2.x, &p2.y); + + double distance = sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2)); + printf("Distance = %.2lf\n\n", distance); +} + +void power_of_two() { + printf("Task 09.2: Power of 2 using structure\n"); + struct num { + int number; + } n; + + printf("Enter a number: "); + scanf("%d", &n.number); + + if ((n.number > 0) && ((n.number & (n.number - 1)) == 0)) { + printf("Yes, %d is power of 2\n\n", n.number); + } else { + printf("No, %d is not the power of 2\n\n", n.number); + } +} + +/////////////////////////////////////////////////// Main ////////////////////////////////////////////////////////// +int main() { + task01_datatypes(); + switch_operation(); + arthmatic_operations(); + fibonaci_sequence(); + guessing_game(); + Is_prime(); + prime_numbers(); + + int n = 5; + printf("Factorial of %d is: %d\n\n", n, fictorial(n)); + + reverse_string(); + second_highest(); + file_system(); + bitwise_operations(); + ispower_of2(); + enumeration(); + structures(); + power_of_two(); + + return 0; +} diff --git a/MushafAli/Day02/Readme.txt b/MushafAli/Day02/Readme.txt new file mode 100644 index 0000000..80452af --- /dev/null +++ b/MushafAli/Day02/Readme.txt @@ -0,0 +1,172 @@ +Lab #02 – Advanced C Programming Concepts + +📌 Overview + +This lab demonstrates various concepts of C programming through multiple tasks and a final project. +The topics include: + +Pointers and array operations + +String manipulation using pointers + +Macros and file I/O with structures + +Linked list operations + +Dynamic memory allocation (malloc, realloc, free) + +Booth multiplier algorithm + +The program is menu-less (all tasks execute sequentially inside main()). + +▶️ How to Compile & Run + +Save the code in a file named lab02.c. + +Open terminal/command prompt and compile: + +gcc lab02.c -o lab02 + + +Run the executable: + +./lab02 + + +You will be prompted for inputs in some tasks. + +📊 Execution Flow & Example Outputs + +The following shows the order in which tasks are executed and the kind of output produced. + +##Task 1 – Pointers + +###Task 1.1: Pointer demonstration +Direct : 10 +Indirect : 10 +Modified : 40 + +###Task 1.2: Swap using pointers +Swapped: x=20, y=10 + +###Task 1.3: Array operations + +Program asks for size and elements of array: + +Enter size of an array you want to print: 3 +Please enter 3 elements in array: +1 2 3 +You entered these integers in array: +1 2 3 +Sum of all elements of array: 6 +Array in reverse order: +3 2 1 + +##Task 2 – String Operations +String length +Length of string is: 5 + +String copy +Copied string: Hello + +String compare +Matched part: Hel +Unmatched part: lo + +Palindrome check +Forward: MONDAY +Reverse: YADNOM +Not palindrome + +###Task 3 – Macros and File I/O +Macros +16 +4 +100 +899 +F + +File I/O with struct student + +Program asks for student details: + +Enter number of students: 2 + +Enter data for student 1 +Roll no: 1 +Name: Ali +GPA: 3.1 + +Enter data for student 2 +Roll no: 2 +Name: Sara +GPA: 3.9 + + +Output: + +Highest GPA: 3.90 by Sara + +Names read from file: +Ali +Sara + +###Task 4 – Linked List + +Deletes node with value 12: + +Linked list after deletion: 15 23 14 + +###Task 5 – Dynamic Memory Allocation +malloc() usage +Enter number of slots: 3 +Enter integers: +10 20 30 +Array: 10 20 30 +Sum: 60 +Average: 20.00 + +realloc() usage +Enter old size: 2 +Enter elements: +1 2 +Enter new size: 3 +Enter new elements: +4 5 6 +Extended array: 4 5 6 + +Memory free detector +Memory allocated +0x55a7a3c0e260 +0x55a7a3c0e264 +0x55a7a3c0e268 +0x55a7a3c0e26c +0x55a7a3c0e270 +Memory freed successfully + + +(The hex addresses will vary.) + + +###Project + +Project – Booth Multiplier + +Performs signed binary multiplication: + +Enter bit length: 4 +Enter Multiplier (Q): 1 0 1 0 +Enter Multiplicand (M): 0 1 1 0 +Enter 2's complement of M (~M): 1 0 0 1 +Result: 01011100 + + + + + + + + +########### AI USAGE ############### +prefer google for syntax confirmation +use chatgpt for understanding the Booths Algorythm also see youtub vedios on Botths algorithm (working shifting etc ) \ No newline at end of file diff --git a/MushafAli/Day02/lab2_solution.txt b/MushafAli/Day02/lab2_solution.txt new file mode 100644 index 0000000..2bdd1e7 --- /dev/null +++ b/MushafAli/Day02/lab2_solution.txt @@ -0,0 +1,441 @@ +////////////////////////////////////////// Lab#02 /////////////////////////////////////////////////////// +// Author: [Your Name] +// Description: This program demonstrates different concepts of C programming such as pointers, +// strings, macros, file I/O, linked list operations, dynamic memory allocation, and Booth multiplier. +////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +///////////////////////////////////////////// Task#01 /////////////////////////////////////////////////// +// Pointer basics, swapping, and array manipulation + +//////////////////// Task#1.1: Pointer demonstration //////////////////// +void task1_1() { + int x = 10; // Variable x declared and initialized + int *ptr = &x; // Pointer initialized with address of x + + // Printing x using direct and indirect access + printf("Direct : %d\nIndirect : %d\n", x, *ptr); + + // Modifying x through pointer + *ptr = 40; + + // Printing modified value of x + printf("Modified : %d\n", *ptr); +} + +//////////////////// Task#1.2: Swap using pointers //////////////////// +void swap(int *x, int *y) { + int temp; + temp = *x; + *x = *y; + *y = temp; +} + +//////////////////// Task#1.3: Array operations using pointers //////////////////// +void task1_3() { + int size; + printf("Enter size of an array you want to print: "); + scanf("%d", &size); + + int arr[size]; + printf("Please enter %d elements in array:\n", size); + + // Input elements + for (int i = 0; i < size; i++) { + scanf("%d", &arr[i]); + } + + int *ptr = arr; + + // Printing array using pointer arithmetic + printf("You entered these integers in array:\n"); + for (int i = 0; i < size; i++) { + printf("%d ", *(ptr + i)); + } + printf("\n"); + + // Computing sum + int sum = 0; + for (int i = 0; i < size; i++) { + sum += *(ptr + i); + } + printf("Sum of all elements of array: %d\n", sum); + + // Printing reverse order + printf("Array in reverse order:\n"); + for (int i = size - 1; i >= 0; i--) { + printf("%d ", *(ptr + i)); + } + printf("\n"); +} + +///////////////////////////////////////////// Task#02 /////////////////////////////////////////////////// +// String operations using pointers + +//////////////////// Task#2.1a: String length //////////////////// +void strlength() { + char arr[] = "Hello"; + char *ptr = arr; + int len = 0; + + while (*(ptr + len) != '\0') { + len++; + } + printf("Length of string is: %d\n", len); +} + +//////////////////// Task#2.1b: String copy //////////////////// +void strcopy() { + char a1[] = "Hello"; + char a2[50]; + char *ptr = a1; + int i = 0; + + while (*(ptr + i) != '\0') { + a2[i] = *(ptr + i); + i++; + } + a2[i] = '\0'; // Null-terminate + printf("Copied string: %s\n", a2); +} + +//////////////////// Task#2.1c: String compare (matched/unmatched) //////////////////// +void strcompare() { + char a1[] = "Hello"; + char a2[] = "Helww"; + char matched[50] = {0}; + char unmatched[50] = {0}; + int i = 0, m = 0, u = 0; + + while (a1[i] != '\0' && a2[i] != '\0') { + if (a1[i] == a2[i]) { + matched[m++] = a1[i]; + } else { + unmatched[u++] = a1[i]; + } + i++; + } + printf("Matched part: %s\n", matched); + printf("Unmatched part: %s\n", unmatched); +} + +//////////////////// Task#2.2: Check palindrome //////////////////// +void is_palindrome() { + char str[] = "MONDAY"; + char forward[50], reverse[50]; + int len = strlen(str); + + // Copy forward + strcpy(forward, str); + + // Reverse + for (int i = 0; i < len; i++) { + reverse[i] = str[len - 1 - i]; + } + reverse[len] = '\0'; + + printf("Forward: %s\n", forward); + printf("Reverse: %s\n", reverse); + + if (strcmp(forward, reverse) == 0) + printf("Yes, string is palindrome\n"); + else + printf("Not palindrome\n"); +} + +///////////////////////////////////////////// Task#03 /////////////////////////////////////////////////// +// Macros and File I/O + +//////////////////// Task#3.1: Macros //////////////////// +#define SQUARE(X) ((X) * (X)) +#define MAX2(a, b) (((a) > (b)) ? (a) : (b)) +#define MAX3(a, b, c) (MAX2(MAX2(a, b), c)) +#define MAX4(a, b, c, d) (MAX2(MAX3(a, b, c), d)) +#define TO_UPPER(c) ((c) - 'a' + 'A') + +void macros() { + printf("%d\n", SQUARE(4)); + printf("%d\n", MAX2(3, 4)); + printf("%d\n", MAX3(100, 34, 8)); + printf("%d\n", MAX4(2, 499, 899, 0)); + printf("%c\n", TO_UPPER('f')); +} + +//////////////////// Task#3.2: File I/O with structures //////////////////// +void fileio() { + struct student { + char name[1000]; + int roll; + float gpa; + }; + + int n; + printf("Enter number of students: "); + scanf("%d", &n); + + struct student students_data[n]; + + // Input student data + for (int i = 0; i < n; i++) { + printf("\nEnter data for student %d\n", i + 1); + printf("Roll no: "); + scanf("%d", &students_data[i].roll); + printf("Name: "); + scanf("%s", students_data[i].name); + printf("GPA: "); + scanf("%f", &students_data[i].gpa); + } + + // Find highest GPA + float h_gpa = 0.0; + char h_gpa_stu[1000]; + for (int i = 0; i < n; i++) { + if (students_data[i].gpa > h_gpa) { + h_gpa = students_data[i].gpa; + strcpy(h_gpa_stu, students_data[i].name); + } + } + printf("\nHighest GPA: %.2f by %s\n", h_gpa, h_gpa_stu); + + // Write to file + FILE *ptr = fopen("students.txt", "w"); + for (int i = 0; i < n; i++) { + fprintf(ptr, "%s\n", students_data[i].name); + } + fclose(ptr); + + // Read from file + FILE *ptr1 = fopen("students.txt", "r"); + char ch[60]; + printf("\nNames read from file:\n"); + while (fscanf(ptr1, "%s", ch) != EOF) { + printf("%s\n", ch); + } + fclose(ptr1); +} + +///////////////////////////////////////////// Task#04 /////////////////////////////////////////////////// +// Linked List Operations + +void linklist_operations() { + struct Node { + int data; + struct Node *next; + }; + + // Create nodes dynamically + struct Node *n1 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *n2 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *n3 = (struct Node *)malloc(sizeof(struct Node)); + struct Node *newnode = (struct Node *)malloc(sizeof(struct Node)); + + // Assign data + newnode->data = 15; + n1->data = 23; + n2->data = 12; + n3->data = 14; + + // Linking nodes + newnode->next = n1; + n1->next = n2; + n2->next = n3; + n3->next = NULL; + + struct Node *head = newnode; + + // Delete node with key = 12 + int key = 12; + struct Node *temp = head; + struct Node *prev = NULL; + + if (temp != NULL && temp->data == key) { + head = temp->next; + free(temp); + } else { + while (temp != NULL && temp->data != key) { + prev = temp; + temp = temp->next; + } + if (temp != NULL) { + prev->next = temp->next; + free(temp); + } + } + + // Print remaining list + temp = head; + printf("Linked list after deletion: "); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); + + // Free memory + free(newnode); + free(n1); + free(n3); +} + +///////////////////////////////////////////// Task#05 /////////////////////////////////////////////////// +// Dynamic Memory Allocation + +void dynamic_mem() { + int n; + printf("Enter number of slots: "); + scanf("%d", &n); + + int *arr = (int *)malloc(n * sizeof(int)); + printf("Enter integers:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + // Print array + printf("Array: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + // Calculate sum and average + int sum = 0; + for (int i = 0; i < n; i++) { + sum += arr[i]; + } + printf("Sum: %d\n", sum); + printf("Average: %.2f\n", (float)sum / n); + + free(arr); +} + +void realloc_use() { + int n; + printf("Enter old size: "); + scanf("%d", &n); + + int *arr = (int *)malloc(n * sizeof(int)); + printf("Enter elements:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + int m; + printf("Enter new size: "); + scanf("%d", &m); + arr = (int *)realloc(arr, m * sizeof(int)); + + printf("Enter new elements:\n"); + for (int i = 0; i < m; i++) { + scanf("%d", &arr[i]); + } + + printf("Extended array: "); + for (int i = 0; i < m; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + free(arr); +} + +void mem_free_detector() { + int *ptr = (int *)malloc(5 * sizeof(int)); + if (ptr == NULL) { + printf("Memory not allocated\n"); + } else { + printf("Memory allocated\n"); + } + + for (int i = 0; i < 5; i++) { + printf("%p\n", (ptr + i)); + } + + free(ptr); + ptr = NULL; + + if (ptr == NULL) + printf("Memory freed successfully\n"); + else + printf("Memory not freed\n"); +} + +///////////////////////////////////////////// Project: Booth Multiplier ////////////////////////////////// +void booth_multiplier() { + int n; + printf("Enter bit length: "); + scanf("%d", &n); + + int A[n], Q[n], M[n], M_2scomp[n]; + printf("Enter Multiplier (Q): "); + for (int i = 0; i < n; i++) scanf("%d", &Q[i]); + + printf("Enter Multiplicand (M): "); + for (int i = 0; i < n; i++) scanf("%d", &M[i]); + + printf("Enter 2's complement of M (~M): "); + for (int i = 0; i < n; i++) scanf("%d", &M_2scomp[i]); + + for (int i = 0; i < n; i++) A[i] = 0; + + int Q_ff = 0; + + for (int step = 0; step < n; step++) { + if ((Q[n - 1] == 0 && Q_ff == 0) || (Q[n - 1] == 1 && Q_ff == 1)) { + // Do nothing, just shift + } else if (Q[n - 1] == 1 && Q_ff == 0) { + // A = A - M + int carry = 0; + for (int i = n - 1; i >= 0; i--) { + int temp = A[i] + M_2scomp[i] + carry; + A[i] = temp % 2; + carry = temp / 2; + } + } else if (Q[n - 1] == 0 && Q_ff == 1) { + // A = A + M + int carry = 0; + for (int i = n - 1; i >= 0; i--) { + int temp = A[i] + M[i] + carry; + A[i] = temp % 2; + carry = temp / 2; + } + } + + // Arithmetic right shift + Q_ff = Q[n - 1]; + for (int j = n - 1; j > 0; j--) Q[j] = Q[j - 1]; + Q[0] = A[n - 1]; + for (int j = n - 1; j > 0; j--) A[j] = A[j - 1]; + } + + printf("Result: "); + for (int i = 0; i < n; i++) printf("%d", A[i]); + for (int i = 0; i < n; i++) printf("%d", Q[i]); + printf("\n"); +} + +///////////////////////////////////////////// Main Function ///////////////////////////////////////////// +int main() { + task1_1(); + int x = 10, y = 20; + swap(&x, &y); + printf("Swapped: x=%d, y=%d\n", x, y); + task1_3(); + strlength(); + strcopy(); + strcompare(); + is_palindrome(); + macros(); + fileio(); + linklist_operations(); + dynamic_mem(); + realloc_use(); + mem_free_detector(); + booth_multiplier(); + return 0; +} + +///////////////////////////////////////////// END OF LAB#02 ///////////////////////////////////////////// diff --git a/MushafAli/Day03/Readme.txt b/MushafAli/Day03/Readme.txt new file mode 100644 index 0000000..12e728a --- /dev/null +++ b/MushafAli/Day03/Readme.txt @@ -0,0 +1,160 @@ +Lab#03 – Bash Scripting & Makefile Automation + +This lab demonstrates Bash scripting and Makefile tasks including printing messages, reading input, arithmetic operations, conditionals, loops, arrays, associative arrays, file handling, logging, backup, and compiling C programs. + +Task #1 – Basic Bash Commands + +Print Message +echo "Hello , Word!" + + +###Prints a simple greeting message. + +Greet User +Prompts for a username and greets the user: +Assala o alikum how are you? + + +###Sum of Two Numbers + +Takes two numbers as command-line arguments ($1 and $2) and prints their sum: + +Sum of 2 numbers is : + + +Task #2 – Conditionals & Loops + +###Even or Odd + +Prompts the user to enter a number and checks if it is even or odd. + + +###Multiplication Table + +Uses the first command-line argument as input and prints its first 10 multiples. + +###Random Number Guessing Game + +Generates a random number between 1–10. + +User keeps guessing until correct. + +Provides hints if the guess is higher or lower. + + +Task #3 – Functions, Arrays & Associative Arrays + + +###Factorial Function + +Calculates factorial of a number (passed as first argument) using a loop. + +###Arrays + +Stores fruits in an array. + +Adds "Mango" and prints all elements. + +###Associative Arrays + +Stores countries and capitals in an associative array. + +User enters a country name and the script prints its capital or a “not exist” message. + + +Task #4 – File Handling & Logging + +Reading File + +Reads each line of text.txt and prints with line number: + +processing : + + +Logging User Actions + +Prompts user for username and action. + +Appends a timestamped entry to script.log. + +Prints total lines and action counts per user. + +Backup Script + +Compresses the LAB#01 directory into a .tar.gz backup file with the current date. + +Stores it in the specified backup directory. + + +Task #5 – Makefiles + +###Simple Makefile + +Compiles main.c and functions.c into program. + +clean target removes object files and the executable. + +####Advanced Makefile + +Uses variables, pattern rules, and dependency files. + +Supports debug compilation and cleaning. + +###Shell Script Management Makefile + +check – syntax check of all scripts. + +test – runs all test scripts. + +install – copies scripts to backup directory and makes them executable. + +###Execution Instructions +Run Bash Script +bash lab01.sh + + + and are used in sum and multiplication tasks. + +Run Makefile +make # compile program +make debug # compile with debug symbols +make clean # remove object files and executable + +Run Script Management +make check +make test +make install + + + +###Expected Output Summary + +1)Hello message & user greeting + +2)Sum of two numbers + +3)Even/odd check + +4)Multiplication table + +5)Random number guessing game + +5)Factorial calculation + +6)Array and associative array outputs + +7)File reading (text.txt) and logging (script.log) + +8)Backup creation (backup_YYYY-MM-DD.tar.gz) + +9)C program compilation and Makefile operations + + + + + + + + +########### AI USAGE ############### +use chatgpt for some commands revision and use \ No newline at end of file diff --git a/MushafAli/Day03/functions.c b/MushafAli/Day03/functions.c new file mode 100644 index 0000000..e64636c --- /dev/null +++ b/MushafAli/Day03/functions.c @@ -0,0 +1,9 @@ +#include +#include "functions.h" + +void add(){ +int x=3; +int y=2; +int sum=x+y; +printf("%d",sum); +} diff --git a/MushafAli/Day03/functions.h b/MushafAli/Day03/functions.h new file mode 100644 index 0000000..6e529e6 --- /dev/null +++ b/MushafAli/Day03/functions.h @@ -0,0 +1,6 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +void add() ; +int multipl() ; +#endif diff --git a/MushafAli/Day03/hello.sh b/MushafAli/Day03/hello.sh new file mode 100644 index 0000000..11c8247 --- /dev/null +++ b/MushafAli/Day03/hello.sh @@ -0,0 +1,235 @@ +#!/bin/bash + +################################# Lab#01 ############################# + +########################### Task#01 ################# +########## Task1.1 ######## +echo "Hello , Word!" +########### Task#1.2 ###### +echo "Enter username" +read name +echo "Assala o alikum $name how are you?" + +############ Task#1.3 ###### +echo "Enter 2 numbers :" +num1=$1 +num2=$2 +echo "Sum of 2 numbers is : $((num1+num2))" +################################### TASK#02 ######################### +######### task#2.1 ######### +echo "Enter a number to whome we want to check either even or not" +read num +if (( $num%2==0 ));then + echo "Number is even" +else + echo "Number is odd" +fi + + + +########## task#2.2 ######### +num=$1 +for i in 1 2 3 4 5 6 7 8 9 10 +do + echo "Multiples of $num are : $((num * i))" +done + +############# task#2.3 ######### +random_number=$((RANDOM %10+1)) + + +guess=0 +while [ $guess -ne $random_number ] +do echo "Enter a guess " + read guess + if [ $guess -lt $random_number ]; then + echo "Guess is less then random number" + elif [ $guess -gt $random_number ]; then + echo "Your guess is greater then the random number" + else + echo "Comngratulations! U guessed corect number" + fi +done + +########################################### TASK#3 ######################## + +###############3 Task#3.1 ############## + +function fictorial() { +n=$1 +if [ $n -eq 0 -o $n -eq 1 ];then + echo "Fictorial of $n is : 1" +else + result=1 + for((i=2;i<=n;i++)) +do + result=$((result*i)) +done + echo "Fictorial of $n is :$result" +fi + +} +fictorial $1 +#################### Task#3.2 ######### + + + +fruits=("Apple" "Banana" "Gava") +function fruit() { +echo "${fruits[@]}" +} +fruits+=("Mango") +fruit +################## Task#3.3 ########### + +declare -A capitals + +capitals["Pakistan"]="Islamabad" +capitals["India"]="Deli" +capitals["Japan"]="Tokyo" + +function capital() { + + read country + + if [[ -v capitals[$country] ]]; then + echo "$country - ${capitals[$country]}" + else + echo "$country not exist in list" + fi +} +capital + + + +################################# TASK#4 ################################ + +####################### Task#4.1 ############ +i=0 +while IFS= read -r line; +do + echo " processing :$i $line" + i=$((i+1)) +done < "text.txt" + +###############3 Task#4.2 ################# + +File="script.log" +read username +read action +timestamp=$(date "+%Y-%m-%d %H:%M:%S") +echo "$timestamp - $username $action" >> $File +echo "Data successfully written in script.log file go and check it out" + +count=0 +while IFS= read -r line; +do + username_field=$(echo "$line" | awk '{print $4}') + echo $username_field + count=$((count+1)) +done < $File +echo "Toatal no of lines in Script.log file are :$count" + +echo "Action count per user :" +awk '{count[$4]++} END {for (user in count) print user " : " count[user]}' "$File" + +#################### Task#4.3 ############# +SOURCE_DIRECTRY="/mnt/c/Users/JK Traders Hall Road/Desktop/LAB#01" +BACKUP_NAME="backup_$(date +%Y-%m-%d).tar.gz" +DESTINATION_DIRECTRY="/mnt/c/Users/JK Traders Hall Road/Documents/backup" + +if [ -d "$SOURCE_DIRECTRY" ]; then + tar -czvf "$DESTINATION_DIRECTRY/$BACKUP_NAME" "$SOURCE_DIRECTRY" + echo "Backup created Successfully! $DESTINATION_DIRECTRY/$BACKUP_NAME" +else + echo "ERROR! Directry '$SOURCE_DIRECTRY\' does not exist." + exit 1 +fi + +#################### TASK#5.1 ################## +all: program + +program: main.o functions.o + gcc main.o functions.o -o program + +main.o: main.c functions.h + gcc -c main.c + +functions.o: functions.c functions.h + gcc -c functions.c + +clean: + rm -f *.o program + + +#################### Task#5.2 ##################33 +################################ Task#5.2 ############################# +CC=gcc +CFLAGS= -Wall -g + +SRCS= main.c functions.c utils.c ##can use SRCS=$(wildcard *.c) it will find all .c files automatically +OBJS= $(SRCS:.c=.o) +TARGET=program + +.PHONY:all clean debug + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) $(OBJS) -o $(TARGET) +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -MMD ##Pattern Rule +-include $(OBJS:.o=.d) +debug: $(CFLAGS) += -O0 ##This tells the compiletr to not do the optimizations on code cuz this optimization make debuging hard. +debug: $(TARGET) + @echo "Compiled $(TARGET) with debuging sysmbols" +clean: + rm -f $(OBJS) $(TARGET) *.d + +########################## TASK#5.3 ################################## +SCRIPTS_DIR=my_shell_scripts/scripts +TESTS_DIR=my_shell_scripts/tests +INSTALL_DIR=/mnt/c/Users/JK\ Traders\ Hall\ Road/Desktop/backup + +check: + @echo "Checking all scripts for syntax checking..." + @for script in $(SCRIPTS_DIR)/*.sh ; do \ + echo "Checking $$script"; \ + bash -n $$script || exit 1; \ + done + @echo "All Scripts passed the syntax check!" + +test: + @echo "Running tests..." + @for test in $(TESTS_DIR)/*.sh ; do \ + echo "Running $$test"; \ + bash $$test || exit 1; \ + done + @echo "All Tests passed!" + +install: + @echo "Installing Scripts to $(INSTALL_DIR)..." + @for script in $(SCRIPTS_DIR)/*.sh ; do \ + cp $$script $(INSTALL_DIR); \ + chmod +x $(INSTALL_DIR)/$$(basename $$script); \ + done + @echo "Installation complete!" + +.PHONY: check test install + + + + + + + + + + + + + + + + + diff --git a/MushafAli/Day03/main.c b/MushafAli/Day03/main.c new file mode 100644 index 0000000..f7f9e01 --- /dev/null +++ b/MushafAli/Day03/main.c @@ -0,0 +1,8 @@ +#include "functions.h" +#include +int main() { +add (); +int result=multipl(); +printf("%d",result); +return 0; +} diff --git a/MushafAli/Day03/scripts/add.sh b/MushafAli/Day03/scripts/add.sh new file mode 100644 index 0000000..9b4fc5d --- /dev/null +++ b/MushafAli/Day03/scripts/add.sh @@ -0,0 +1,5 @@ +#!/bin/bash +a=2 +b=3 +sum=$((a+b)) +echo "Sum of $a and $b is : $sum" diff --git a/MushafAli/Day03/scripts/hello.sh b/MushafAli/Day03/scripts/hello.sh new file mode 100644 index 0000000..6932117 --- /dev/null +++ b/MushafAli/Day03/scripts/hello.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Hello from Hello.sh" diff --git a/MushafAli/Day03/tests/test_add.sh b/MushafAli/Day03/tests/test_add.sh new file mode 100644 index 0000000..92c4bfd --- /dev/null +++ b/MushafAli/Day03/tests/test_add.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -euo pipefail +out=$(bash ./my_shell_scripts/scripts/add.sh) +[ "$out" = "Sum of 2 and 3 is : 5" ] diff --git a/MushafAli/Day03/tests/test_hello.sh b/MushafAli/Day03/tests/test_hello.sh new file mode 100644 index 0000000..fbebc7e --- /dev/null +++ b/MushafAli/Day03/tests/test_hello.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -euo pipefail +out=$(bash ./my_shell_scripts/scripts/hello.sh) +[ "$out" = "Hello from Hello.sh" ] diff --git a/MushafAli/Day03/utils.c b/MushafAli/Day03/utils.c new file mode 100644 index 0000000..3d163d7 --- /dev/null +++ b/MushafAli/Day03/utils.c @@ -0,0 +1,8 @@ +#include "functions.h" + +int multipl () { +int a=2; +int b=3; +int multiply=a*b; +return multiply; +} diff --git a/MushafAli/Day04/Readme.txt b/MushafAli/Day04/Readme.txt new file mode 100644 index 0000000..4ea506a --- /dev/null +++ b/MushafAli/Day04/Readme.txt @@ -0,0 +1,41 @@ +TCL Scripting Lab – Digital Design +Objective + +###Learn basic TCL scripting for digital design calculations and data manipulation. + + +TCL interpreter (tclsh) +Text editor (nano, vim, gedit, etc.) + +Lab 1 – Basic Operations + +File: digital_calc.tcl + +##Tasks: + +Calculate clock period from frequency. +Compute CMOS power using a procedure calc_power. +Determine maximum operating frequency. +Run: tclsh digital_calc.tcl + +Lab 2 – Lists and Digital Design Data + +File: design_data.tcl + +##Tasks: +Create and print list of modules. +Add/remove modules from list. +Store module sizes in a dictionary. +Calculate total gate count and find largest module. +Run: tclsh design_data.tcl + +Conclusion +This lab introduces TCL scripting fundamentals for digital design, including variables, expressions, procedures, lists, and dictionaries, forming a foundation for more advanced EDA automation. + + + + + + +########### AI USAGE ############## +use chatgpt for commands review and use \ No newline at end of file diff --git a/MushafAli/Day04/design_data.tcl b/MushafAli/Day04/design_data.tcl new file mode 100644 index 0000000..a309a7c --- /dev/null +++ b/MushafAli/Day04/design_data.tcl @@ -0,0 +1,66 @@ +# Simulating digital design data operations +# Define a list of module names +set modules {ALU Register_File Decoder Multiplexer} + +# Print all modules +puts "All modules:" +foreach module $modules { puts " $module" } + +# Add a new module +lappend modules "Control_Unit" +puts "\nAfter adding Control_Unit:" +puts $modules + +##################################### Task addinmg new modules ############# +lappend modules "pipeline" "Cache" +puts "\nAfter adding pipeline and Cache" +puts $modules + +# Remove a module +set modules [lsearch -all -inline -not $modules "Decoder"] +puts "\nAfter removing Decoder:" +puts $modules + +# Define a dict of module sizes (simulated gate count) +dict set module_sizes ALU 1000 +dict set module_sizes Register_File 5000 +dict set module_sizes Multiplexer 200 +dict set module_sizes Control_Unit 1500 +dict set module_sizes Cache 10000 +dict set module_sizes pipeline 4000 + +# Calculate total gate count +set total_gates 0 +dict for {module size} $module_sizes { + set total_gates [expr {$total_gates + $size}] +} + +puts "\nTotal gate count: $total_gates" + + +################################################# TASK print moules whose gate count is greater then the threashold ##################################### + +proc find_large_modules {threshold module_dict} { + puts "\nModules larger than $threshold gates:" + dict for {module size} $module_dict { + if {$size > $threshold} { + puts "$module : $size" + } + } +} + +# Call the procedure with threshold = 1000 +find_large_modules 1000 $module_sizes + + +# Find the largest module +set max_size 0 +set largest_module "" +dict for {module size} $module_sizes { + if {$size > $max_size} { + set max_size $size + set largest_module $module + } + } +puts "Largest module: $largest_module with $max_size gates" + diff --git a/MushafAli/Day04/digital_calc.tcl b/MushafAli/Day04/digital_calc.tcl new file mode 100644 index 0000000..e9fb076 --- /dev/null +++ b/MushafAli/Day04/digital_calc.tcl @@ -0,0 +1,39 @@ +# Basic digital design calculations + +# Define clock frequency and calculate period + +################################################### TASK change value of frequency ##################################################################### + +# Here i change the value of clock_frequency from 100mhz to 200mhz so definately now Time period will be decreased as frequency get increased also the power got increased and result proved this expectation. + + + + + +set clock_freq_mhz 200 +set clock_period_ns [expr {1000.0 / $clock_freq_mhz}] +puts "Clock period: $clock_period_ns ns" + +# Calculate power for a simple CMOS circuit +proc calc_power {capacitance voltage frequency} { + return [expr {$capacitance * $voltage * $voltage * $frequency}] +} +set cap_pf 10.0 +set voltage 1.2 +set power_mw [calc_power $cap_pf $voltage $clock_freq_mhz] +puts "Power consumption: $power_mw mW" + +# Simple timing calculation +set prop_delay_ns 2.5 +set setup_time_ns 0.5 +set max_freq_mhz [expr {1000 / ($prop_delay_ns + $setup_time_ns)}] +puts "Maximum frequency: $max_freq_mhz MHz" + + + +###################################################### TASK ADD NEW CALCULATION ############################################### +#NEW CALCULATION of no of cycles + +set time_ns 1000 +set num_cycles [expr {$time_ns / $clock_period_ns}] +puts "Number of cycles in 1 micro sec are : $num_cycles" diff --git a/MushafAli/Day05/Makefile b/MushafAli/Day05/Makefile new file mode 100644 index 0000000..af59de8 --- /dev/null +++ b/MushafAli/Day05/Makefile @@ -0,0 +1,42 @@ +# -------- RISC-V Assembly Lab Makefile ------- + PROG ?= helloword # override: make PROG=abs_diff run + +AS := riscv64-unknown-elf-as +LD := riscv64-unknown-elf-ld +OBJDUMP := riscv64-unknown-elf-objdump +SPIKE := spike + +ASFLAGS := -march=rv64imac -mabi=lp64 -g +LDFLAGS := -T link.ld + +SRC_DIR := src +BUILD_DIR := build + +all: $(BUILD_DIR)/$(PROG) + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +# assemble .S -> .o +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.S | $(BUILD_DIR) + $(AS) $(ASFLAGS) -o $@ $< + +# link .o -> ELF +$(BUILD_DIR)/%: $(BUILD_DIR)/%.o link.ld + $(LD) $(LDFLAGS) -o $@ $< + +run: $(BUILD_DIR)/$(PROG) + $(SPIKE) $< + +debug: $(BUILD_DIR)/$(PROG) + # NOTE: use two hyphens here (not an en dash) + $(SPIKE) -d --log-commits $< + +sections: $(BUILD_DIR)/$(PROG) + $(OBJDUMP) -h $< + +clean: + rm -rf $(BUILD_DIR) + +.PHONY: all run debug clean sections + diff --git a/MushafAli/Day05/Readme.txt b/MushafAli/Day05/Readme.txt new file mode 100644 index 0000000..c3a9537 --- /dev/null +++ b/MushafAli/Day05/Readme.txt @@ -0,0 +1,34 @@ +Lab: Introduction to RISC-V Assembly Programming +Objective + +In this lab, I learned how to write RISC-V assembly programs, run them on the Spike simulator, and use Spike’s debugging features. + +What I Did + +I installed the RISC-V GNU toolchain and Spike simulator on my machine. + + wrote a linker script (link.ld) to define the memory layout for my programs. + + created assembly programs in .S files and learned to use registers, loops, and branches. + + used Spike to run my programs and debug them using commands like r, s, mem, and until pc. + + learned how to output messages using HTIF. + + created a Makefile to automate assembling, linking, running, and debugging my programs. + + +##Exercises I Completed + +Calculated the absolute difference between two numbers. + +Counted the number of set bits in a 32-bit word. + +Calculated the factorial of a number. + + + +########### AI USAGE ############### +Use chatgpt for setting up the tool chain in VS code and run some test (example) assumbly codes +and also run 3 tasks + diff --git a/MushafAli/Day05/build/absdiff b/MushafAli/Day05/build/absdiff new file mode 100644 index 0000000000000000000000000000000000000000..a856a025c78ce5301c7432a87ee1c0fe85be8141 GIT binary patch literal 5912 zcmeHLOK;Oa5T5lWp=qnAp`aF(Ln|mRp`=Mm1t(lVc}Nu$Rh%Oyb=pXI$#$xi0~iqe zfgZ{YE|vHLNSu+l^BedFh$9D>*@tayQh_*oCC|)$Gqbb%dF{izSl_s3F$Q{A@DtQ5 zB+Mp(WA=>HSSTl90yscFgV@HD^qaD30J;+CQ{;>DLYm?TNo-loI#QW3 zkTQ@mkTQ@mkTQ@mkTQ@mkTQ@mkTQ@mkTUSk49sK!_+mD5g!=XISH7J4WH%n>;K%|v zd}Z+qAHG2TPoBHuhcr# z{!+~$t3lSYPG``r^q1fzMp>|clCfWfag?GJ!F;a);MD!?RbNsJhH(sF#z@@}u=mOBG~*J(QKR#}i)3tT^d`;VT^ zuWij&ODkqeWoT6{2@6dJ$DP@ULuo&!F3yO-0r?p*I2eakF&uIuqVayoPS22!#~ejI zdIK5w@jC*PF(LHu{$=~fg1g4K&Z_ta27X@P;ys7LRN}V6FB$Q76uxTUxGuJ&hS!O1 zZ4bJGPK87jJl7wz1Gu>V*AJrW?!b1h>q5;B94`PU`3IfAsiO`&sdu#%xcdQ=ww=I% zlDH@(CkVV&eSr4`akoC$skK|Uma<#jW>4Erz3#bt(adwYJ8=>gP^|pFn!{9LCvhV} zv6|}l*#*tI8<2TO(Zb-|6p!E0SQz0Db0__%)f=vCPoW9 zSM}eB)nZ}BuA|GuD4G{#PD!drnz2vNV`3CsQTeJJdt!d=g#1mFzZ7LkLmewZyDHy| z3swG&C{h}FuSspbQ;Pho^53bzam1*${#(vc5RN=P`rQjaiOp$gJWhgW>$)1IGNba} MtNa5UsI-~?2O)Hu_5c6? literal 0 HcmV?d00001 diff --git a/MushafAli/Day05/build/countbits b/MushafAli/Day05/build/countbits new file mode 100644 index 0000000000000000000000000000000000000000..0fb7141dd56480e0cf75379a4a6c5723bcf99973 GIT binary patch literal 5872 zcmeHLO>5Lp6umELby{_79YLWWl`4u#rX2>YP^3kx3q_FPIwaGy4NN{rGNpB)u`c`p zUAPt8_%HkgfZOaWM`%FkMHG#OOX(uK zL6rpy7#aK7p2o;q4=m3H0E1){7k*ge&W30~OyRwWZombbO|^hwoWN*fLSezM2BWg1 zx46Ec!E4;~2EPpq&{F+~lCJu4JHphLxQRbjvRRlpG0V;|yFJ&w+U`tJT8&!>=r(aF z9x;>zaH+8apS0V2(C$tW1>I)Sp|k`y+RIBuZ z^ZhK?t*$aJ-mZ&qgQhM2jpwkETyC6v$rqD-JpH{F-v(Fka2X@jt2Yq!B_;Z@zWR!Y z#KBKgKpyFT25Zv>#i8my>Ys{UPv@?4v_LHPf6G^^gRWi2l+Ka*hpcz%QjT=(Gh}s+ zgbU&YhV#6t4!YLGl+KZQRn{-or5wrIs@ND2)$6)c*59d=0;s*3vwo(e{3z>7DV#>` qEdJDaUL+BYDg3Fk=Y$N`Wq2`sn~?42Dqc+q4*5DtWxEL^zE_RRLOlgwATdp4^v zxWWO??!haG7mwaNdi7$$PeDC;p`Pw4sD7tA9IVN7)%(=*R@M92{r&Y@U)4O1R6P2Z zUbYk^`txXR!?&K9>$FHG=;s>I!NVH)ztrf5FZ*?$nh%=v&p~seNq_yJ-g8f4I;cNg z@UAC$82X#_BmURu96vqpzs6C1W`%f3+$ZW!X5+LsNRwbAFX#wo%H_209pOIm^o?!t z+_v7D%j7mE{g|J5?0VVAC26j_eUC?dXwf_A`HeG;^Nr?0!B3Z71}i!$T0GVjJJgk( z$tYSG&61sPI~)#H;z82i?WR$Z(v919TURz(onWm)S5`S*<|FSTHW%XE%WH$qbhSI` zOxxWMRv*^4qtR@*I&D{^FTj53$`kqgD-)kyn}a{gka>PSv-3Uj>6@ST!Tbd97%jfd$!C+}#L@uQ_}) zeL6gcD&KV|?#9V+cU82WV|;@YMfn$taMtn(2XunOxI53_`6QQ|)5)j# z`g8sp4wL8$MoyHSqw#D+OM9<8ropXti-wcQl(r`0sGD64)J@VbP7AqCb8CLoM4uK| z^x}kKAAV?jdU9R9?t84u>xQrU73ay$lFlRQ(U|C_xEud0oGhrP1w%w}m#_O37qs1t zpPPPl4Z>P+oEOW#YWQ{+DGt7kbJOtc>MIVujdR=Z?XFN9d>iME;p?0OQXG66=bqso z7aK}(@NJw;I9V{S_Ka-;WC7pC+0M|MzbH18;^5mjL*XF4y%QA&-^PgzzbQ79;^5mj zd%{8dQ({9Y4!(`^KseyPDK?bi;M+J4g#*65yA=oD#yKQ>ypi4)8%oy{xbAu2I#)He zvDa192L6tMtXpCu&bwkmEr|{MjMz}iVguK+3CPxoGpX{bdXUG~qje&W+F7h1`DT=c zJqnUE&h?I3aWo8rG}=ok*b38-f?kqPkfB>qZ+5>s9E_s^Fc@!7n4CGoUN4Sz3+^xu z$M++Skux|CyYWF3glQTNdNckm(gFTY-Qxb3OPxOrw=Tw-0-~{gR|JE(Y_HS}Y zKNB2h6nW~>*15%g1MYq~rNUFbG&8Q+(3kiRj@$n`I77vH00 z<9{L(`lC!3#*Zn{@#y$(l*D9+d?_kG!-!k!RrZzTUzSVySLuuVcrUp5HyCqcqRXm) z_U-`ga<@w`@V*ve8x13R9u*4ND69Xe)W0c2)Q|VCTmOtPq;m7@OaA=3oCzWVzZl0p zR0`{5IYKp*8U2k?Vbtvo(XYJHYVOVOZ<21$1vT!%Mk6z`MHLM5jQ{HHD&$V N^0D%h=?l~C{~Yq_vpoO+ literal 0 HcmV?d00001 diff --git a/MushafAli/Day05/build/fictorial b/MushafAli/Day05/build/fictorial new file mode 100644 index 0000000000000000000000000000000000000000..ee6fc9994423959b2cc2242136bc557bf65ac5cb GIT binary patch literal 5864 zcmeHL&2G~`5T3Q$(iRB)K~U(Ws30UjP8upGw^mi*0ti&#>N=^@8aZ)fZ>kmvVdx|D zP>u*5gje7NxFc~w91((<{c+4D5=dNkC4W2n&CKrl+jXSOtBw129mYU62fl+E<;1NL zVEZyFd-AQq3OI z6|f3e1*`&A0jq#jz$#!BunJfOtOEb6z_9~x^u}=y-#ak6-hB25Zn~S!&za93kMXbl z(cuTS_^t^@k}a*Hws|Mm4n%*;I|Vjt|S(G)#?6vl-@A<||h!v-INFZs0>( z!$|}p+r4ZzXa#Xp6%zV9%sAY8_;g`ybD{1nn?)02W>-p- z8#LBpoIhSh`(3Ki3~4T)E<>6tPI=Jb!q^~*j4LZS13tH}QQ;%yN8sD9f>6e3(GTwb zjN+F|heXsl!HaXug{k0<;wwh{y5cVy`~&2thUwWqv;;bkhFLG>aC!f)*Sr-pxgRG< z7q*j5=+hAVj0Y(P@Unw84;t9>RQ9{t;^96AZ#&=tc;ZfY0q1Gd=;75sK5X=M{5a}_ z+KxJ{MB725k%oK4C=EJ0{UlsKN%LRbJZ6*Hr5lio303{hy#MkW5fK6|AyU4+i5Q=g zqo(qUuZc)(;$)>MkbYw@w_Bowsq2pBCu8Q*y)ZeN;JI4=wV16BX6y#KOpc;<7