diff --git a/Worksheet_1/Dockerfile b/Worksheet_1/Dockerfile index 85516ee65b1e69dee682518af6512f756f0345bb..8bc63ac9b974d4e2868e9e5ff12a37a92867255a 100644 --- a/Worksheet_1/Dockerfile +++ b/Worksheet_1/Dockerfile @@ -1,16 +1,19 @@ -# Use an official lightweight Linux image +# Use a lightweight Linux base image FROM debian:latest -# Install NASM and GCC for assembly and linking +# Install NASM, GCC, and dependencies for 32-bit binaries RUN apt-get update && apt-get install -y \ nasm \ gcc \ + gcc-multilib \ + make \ + libc6-dev-i386 \ && rm -rf /var/lib/apt/lists/* # Set the working directory WORKDIR /app -# Copy the current directory contents into the container +# Copy project files into the container COPY . /app # Set default command to bash diff --git a/Worksheet_1/Makefile b/Worksheet_1/Makefile index 7d859d4358150520451a2b39b4d910d1982c7aca..f4b4ada48d3ca33768bd4e0dafecb1cad84efbac 100644 --- a/Worksheet_1/Makefile +++ b/Worksheet_1/Makefile @@ -1,22 +1,16 @@ -all: task1 task2_1 task2_2 +all: task1 task2 task1: src/task1.o src/driver.o src/asm_io.o gcc -m32 -o src/task1 src/task1.o src/driver.o src/asm_io.o -task2_1: src/task2_1.o src/driver.o src/asm_io.o src/print_int.o - gcc -m32 -o src/task2_1 src/task2_1.o src/driver.o src/asm_io.o src/print_int.o - -task2_2: src/task2_2.o src/driver.o src/asm_io.o src/print_int.o - gcc -m32 -o src/task2_2 src/task2_2.o src/driver.o src/asm_io.o src/print_int.o +task2: src/task2.o src/driver.o src/asm_io.o src/print_int.o + gcc -m32 -o src/task2 src/task2.o src/driver.o src/asm_io.o src/print_int.o src/task1.o: src/task1.asm nasm -f elf src/task1.asm -o src/task1.o -src/task2_1.o: src/task2_1.asm - nasm -f elf src/task2_1.asm -o src/task2_1.o - -src/task2_2.o: src/task2_2.asm - nasm -f elf src/task2_2.asm -o src/task2_2.o +src/task2.o: src/task2.asm + nasm -f elf src/task2.asm -o src/task2.o src/driver.o: src/driver.c gcc -m32 -c src/driver.c -o src/driver.o @@ -28,4 +22,4 @@ src/print_int.o: src/print_int.c gcc -m32 -c src/print_int.c -o src/print_int.o clean: - rm -f src/*.o src/task1 src/task2_1 src/task2_2 + rm -f src/*.o src/task1 src/task2 diff --git a/Worksheet_1/README.md b/Worksheet_1/README.md index fddd09141e8678cf3b05ea3651de850a3aef1a37..10dd0b887111d7a35cffd9f40cf506a070104190 100644 --- a/Worksheet_1/README.md +++ b/Worksheet_1/README.md @@ -61,7 +61,7 @@ apt install -y libc6-dev ``` - Task 2: ```bash - ./src/task2_1 + ./src/task2 ``` - Task 2_2: ```bash @@ -134,7 +134,7 @@ call _print_int --- ## Task 2_1: Loops and Conditionals -- **File**: `task2_1.asm` +- **File**: `task2.asm` - **Description**: Implements a loop to sum an array of integers and validates user input. ### Code Explanation: @@ -186,98 +186,110 @@ call _print_int #### How to Compile and Run: 1. Compile the assembly file: ```bash - nasm -f elf32 src/task2_1.asm -o src/task2_1.o + nasm -f elf32 src/task2.asm -o src/task2.o ``` -2. Link the compiled task2_1.o, driver.o, and print_int.o files using gcc to create an executable file: +2. Link the compiled task2.o, driver.o, and print_int.o files using gcc to create an executable file: ```bash - gcc -m32 src/task2_1.o src/driver.o src/print_int.o -o src/task2_1 + gcc -m32 src/task2.o src/driver.o src/print_int.o -o src/task2 ``` 3. Run the executable: ```bash - ./src/task2_1 + ./src/task2 ``` -# Task 2_1 Output -## Screenshot of Task 2_1 +## Screenshot of Task 2 The following screenshot demonstrates the successful execution of Task 2. It showcases the program's ability to validate user input and sum an array of integers within a specified range. - + ## Explanation -In Task 2_1, the program performs two main functions: +In Task 2, the program performs two main functions: 1. Validates user input to ensure the entered number is within a specified range (50–100). 2. Sums integers from an array and displays the result. --- -### Task 2_2: Loops and Conditionals -- **File**: `task2_2.asm` -- **Description**: This task sums integers within a specified range using loops in assembly. - It calculates the total sum of all numbers between two user-provided inputs. +### Task 3: Makefile +- **File**: `Makefile` +- **Description**: This project uses a `Makefile` to streamline the process of building and cleaning executables for Task 1 and Task 2. The `Makefile` automates assembling, compiling, and linking multiple files while maintaining dependencies between them. + #### Defining Variables: -1. Compile the assembly file: +1. task1: +The task1 target compiles and links the assembly and C files required for Task 1, creating the task1 executable. ```bash - SECTION .data - start dd 0 ; Start of the range - end dd 0 ; End of the range - total dd 0 ; Total sum of the range + task1: src/task1.o src/driver.o src/asm_io.o + gcc -m32 -o src/task1 src/task1.o src/driver.o src/asm_io.o ``` --Defines memory locations for the start and end of the range, as well as the total sum. +Dependencies: -2. Main Logic: - ```assembly - mov eax, [start] ; Load the start value - mov ecx, [end] ; Load the end value - xor edx, edx ; Clear the total - sum_loop: - add edx, eax ; Add the current number to the total - inc eax ; Increment to the next number - cmp eax, ecx ; Compare current with end - jle sum_loop ; If less or equal, continue - mov [total], edx ; Store the total sum in memory +src/task1.o: Compiled from src/task1.asm. +src/driver.o: Compiled from src/driver.c. +src/asm_io.o: Compiled from src/asm_io.asm. - ``` -Uses a loop to add all integers between start and end inclusively. -3. Printing the Total Sum: +2. task2: The task2 target compiles and links the assembly and C files required for Task 2, creating the task2 executable. ```assembly - push dword [total] - call _print_int + task2: src/task2.o src/driver.o src/asm_io.o src/print_int.o + gcc -m32 -o src/task2 src/task2.o src/driver.o src/asm_io.o src/print_int.o ``` --Defines memory locations for the start and end of the range, as well as the total sum. +Dependencies: -#### How to Compile and Run: -1. Compile the assembly file: - ```bash - nasm -f elf32 src/task2_2.asm -o src/task2_2.o +src/task2.o: Compiled from src/task2.asm. +src/driver.o: Compiled from src/driver.c. +src/asm_io.o: Compiled from src/asm_io.asm. +src/print_int.o: Compiled from src/print_int.c. + +3.Object File Targets +task1 + ```assembly + src/task1.o: src/task1.asm + nasm -f elf src/task1.asm -o src/task1.o ``` -2. Link the compiled task1.o, driver.o, and print_int.o files using gcc to create an executable file: +task2 ```bash - gcc -m32 src/task2_2.o src/driver.o src/print_int.o -o src/task2_2 + src/task2.o: src/task2.asm + nasm -f elf src/task2.asm -o src/task2.o + ``` +driver ```bash + src/driver.o: src/driver.c + gcc -m32 -c src/driver.c -o src/driver.o ``` -3. Run the program: - ```bash - ./src/task2_2 +Assembly I/O ```bash + src/asm_io.o: src/asm_io.asm + nasm -f elf src/asm_io.asm -o src/asm_io.o ``` -# Task 2_2 Output +Print Integer ```bash + src/print_int.o: src/print_int.c + gcc -m32 -c src/print_int.c -o src/print_int.o + + ``` +4.clean ```bash + clean: + rm -f src/*.o src/task1 src/task2 -## Screenshot of Task 2_2 -The following screenshot demonstrates the successful execution of Task 2_2. It shows the program prompting the user to enter the size of an array and values one by one. It calculates the maximum, minimum, and sum of all values. + ``` - +# Task 3 Output + +## Screenshot of Task 3 + +The following Makefile automates the build process for Task 1 and Task 2, ensuring efficient compilation, linking, and cleanup of source files and dependencies within the project. + + ## Explanation -In Task 3, the program performs the following operations: -1. Prompts the user to enter the size of an array. -2. Accepts individual integers to populate the array. -3. Computes the maximum, minimum, and sum of the array values. -4. Displays the results. +The Makefile automates the following operations for Task 1 and Task 2: + +1. Assembles the .asm files (task1.asm and task2.asm) into object files. +2. Compiles the .c files (driver.c and print_int.c) into object files. +3. Links all necessary object files to create the task1 and task2 executables. +4. Includes a clean target to remove all generated files, keeping the project directory tidy. --- ## Project Structure @@ -291,18 +303,22 @@ worksheet_1/ │ ├── photo1.png # Task 1 screenshot │ ├── photo2.png # Task 2 screenshot │ ├── photo3.png # Task 3 screenshot -├── src/ # Directory containing all source files +│ ├── photo4.png # Additional screenshot +├── src/ # Directory containing all source files, object files, and executables │ ├── asm_io.asm # Provided Assembly IO code │ ├── asm_io.inc # Provided IO include file │ ├── driver.c # Main C program calling assembly functions │ ├── print_int.c # Helper C program for printing integers │ ├── task1.asm # Assembly code for Task 1 -│ ├── task2_1.asm # Assembly code for Task 2 -│ ├── task2_2.asm # Assembly code for Task 3 -├── task1 # Compiled Task 1 executable -├── task2_1 # Compiled Task 2 executable -├── task2_2 # Compiled Task 3 executable -└── *.o # Object files (task1.o, task2.o, task2_2.o, asm_io.o, driver.o, print_int.o) +│ ├── task2.asm # Assembly code for Task 2 +│ ├── asm_io.o # Object file for asm_io.asm +│ ├── driver.o # Object file for driver.c +│ ├── print_int.o # Object file for print_int.c +│ ├── task1.o # Object file for task1.asm +│ ├── task2.o # Object file for task2.asm +│ ├── task1 # Compiled Task 1 executable +│ ├── task2 # Compiled Task 2 executable + ``` diff --git a/Worksheet_1/images/photo1.png b/Worksheet_1/images/photo1.png index c193547f61a54fce86a71af4c111774272fc4f12..330f6514f2e858609b1a0bc6d95d808eb33f6a31 100644 Binary files a/Worksheet_1/images/photo1.png and b/Worksheet_1/images/photo1.png differ diff --git a/Worksheet_1/images/photo2.png b/Worksheet_1/images/photo2.png index 49cbc552ea191029f61fa192d9bf766ba68f270f..6855cc6b736d75c3b35f19e230fab7aa1099c605 100644 Binary files a/Worksheet_1/images/photo2.png and b/Worksheet_1/images/photo2.png differ diff --git a/Worksheet_1/images/photo3.png b/Worksheet_1/images/photo3.png index 0dbf6c69da64e20367fffb74584fb4727bff6cb3..fea92bd0cc5df2df2e788ff127280c26576f0359 100644 Binary files a/Worksheet_1/images/photo3.png and b/Worksheet_1/images/photo3.png differ diff --git a/Worksheet_1/images/photo4.png b/Worksheet_1/images/photo4.png new file mode 100644 index 0000000000000000000000000000000000000000..21348a6e2ffcd04265057907d166347239b03234 Binary files /dev/null and b/Worksheet_1/images/photo4.png differ diff --git a/Worksheet_1/src/task1 b/Worksheet_1/src/task1 index 1d221cc992064e61813f168e01cd3336c813737b..6a5a8c88e061e6828454a194b68f0dd0c1ff3152 100644 Binary files a/Worksheet_1/src/task1 and b/Worksheet_1/src/task1 differ diff --git a/Worksheet_1/src/task2_1 b/Worksheet_1/src/task2 similarity index 65% rename from Worksheet_1/src/task2_1 rename to Worksheet_1/src/task2 index 003c988ef5d830390da2ca657d6c623304e2e69a..c49fc7e86669300ab26b842fa868822260757a24 100644 Binary files a/Worksheet_1/src/task2_1 and b/Worksheet_1/src/task2 differ diff --git a/Worksheet_1/src/task2_1.asm b/Worksheet_1/src/task2.asm similarity index 100% rename from Worksheet_1/src/task2_1.asm rename to Worksheet_1/src/task2.asm diff --git a/Worksheet_1/src/task2_1.o b/Worksheet_1/src/task2.o similarity index 57% rename from Worksheet_1/src/task2_1.o rename to Worksheet_1/src/task2.o index 4ef86beeca53f454b69a84b15f50dd5eeef29f40..729f3797fae73c0f128fb7d0fcc4863424f71882 100644 Binary files a/Worksheet_1/src/task2_1.o and b/Worksheet_1/src/task2.o differ diff --git a/Worksheet_1/src/task2_2 b/Worksheet_1/src/task2_2 deleted file mode 100644 index 4a9c4dce4c0b4e8c8f1c9f084342dd6759e5f0c3..0000000000000000000000000000000000000000 Binary files a/Worksheet_1/src/task2_2 and /dev/null differ diff --git a/Worksheet_1/src/task2_2.asm b/Worksheet_1/src/task2_2.asm deleted file mode 100644 index ed42fccdfe19371a4a1d599af165f41be108fa84..0000000000000000000000000000000000000000 --- a/Worksheet_1/src/task2_2.asm +++ /dev/null @@ -1,99 +0,0 @@ -%include "src/asm_io.inc" - -section .data - prompt_array db "Enter numbers (one by one): ", 0 - prompt_size db "Enter size of the array: ", 0 - max_msg db "Maximum value is: ", 0 - min_msg db "Minimum value is: ", 0 - sum_msg db "Sum of all values is: ", 0 - newline db 10, 0 - -section .bss - array resd 100 ; Reserve space for an array of 100 integers - size resd 1 ; Store the size of the array - max_val resd 1 ; Store the maximum value - min_val resd 1 ; Store the minimum value - total resd 1 ; Store the sum of the array elements - -section .text - global _asm_main - -_asm_main: - ; Prompt user for array size - mov eax, prompt_size - call print_string - call read_int - mov [size], eax - - ; Fill the array - mov ecx, [size] - xor edi, edi ; Index for the array -fill_array: - mov eax, prompt_array - call print_string - call read_int ; Read one integer - mov [array + edi * 4], eax - inc edi - loop fill_array - - ; Find maximum and minimum values - call find_max_min - - ; Calculate the sum of the array - call calculate_sum - - ; Print results - mov eax, max_msg - call print_string - mov eax, [max_val] - call print_int - call print_nl - - mov eax, min_msg - call print_string - mov eax, [min_val] - call print_int - call print_nl - - mov eax, sum_msg - call print_string - mov eax, [total] - call print_int - call print_nl - ret - -find_max_min: - mov ecx, [size] - mov edi, 0 - mov eax, [array] ; Load the first element - mov ebx, eax ; Assume it is the maximum - mov edx, eax ; Assume it is the minimum -find_loop: - mov eax, [array + edi * 4] - cmp eax, ebx - jle check_min - mov ebx, eax ; Update maximum -check_min: - cmp eax, edx - jge next - mov edx, eax ; Update minimum -next: - inc edi - loop find_loop - mov [max_val], ebx - mov [min_val], edx - ret - -calculate_sum: - mov ecx, [size] - mov edi, 0 - xor eax, eax ; Initialize sum to 0 -sum_loop: - add eax, [array + edi * 4] - inc edi - loop sum_loop - mov [total], eax - ret -section .note.GNU-stack noalloc noexec nowrite progbits - - diff --git a/Worksheet_1/src/task2_2.o b/Worksheet_1/src/task2_2.o deleted file mode 100644 index a4c8f0679bea8d70b22a26bf3edcc8798cd1068c..0000000000000000000000000000000000000000 Binary files a/Worksheet_1/src/task2_2.o and /dev/null differ