diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8a2d46fd561bbc2f7604228874da0291e76b67df --- /dev/null +++ b/src/README.md @@ -0,0 +1,177 @@ +# Worksheet 1 - Operating Systems + +## Introduction +This repository contains the solutions for Worksheet 1 of the Operating Systems course. The worksheet covers basic assembler programming, calling C from assembler, translating C loops into assembler, and using `make` to build programs. + +## Directory Structure +The repository should have the following structure: +``` +repo-name/ +├── README.md +├── src/ +│ ├── asm_io.inc +│ ├── asm_io.asm +│ ├── driver.c +│ ├── task1.asm +│ ├── task2_1.asm +│ ├── task2_2.asm +│ └── Makefile +``` + +## Task 1: Adding Two Integers +In this task, we implement an assembler program that contains a function `asm_main` to add two integers stored in global memory and output the result. + +### Key Code Snippets +- **driver.c**: + ```c + int __attribute__((cdecl)) asm_main(void); + + int main() { + return asm_main(); + } + ``` +- **task1.asm**: + ```asm + section .data + num1 dd 5 + num2 dd 10 + result dd 0 + + section .text + global asm_main + extern print_int + + asm_main: + mov eax, [num1] + add eax, [num2] + push eax + call print_int + add esp, 4 + ret + ``` + +### Compiling and Running the Program +To compile and run the task1 program, create a Makefile as follows: + +```makefile +# Default target +all: task1 task2_1 task2_2 + +task1: driver.o task1.o asm_io.o + gcc -m32 driver.o task1.o asm_io.o -o task1 + +task2_1: driver.o task2_1.o asm_io.o + gcc -m32 driver.o task2_1.o asm_io.o -o task2_1 + +task2_2: driver.o task2_2.o asm_io.o + gcc -m32 driver.o task2_2.o asm_io.o -o task2_2 + +driver.o: src/driver.c + gcc -m32 -c src/driver.c -o driver.o + +task1.o: src/task1.asm + nasm -f elf src/task1.asm -o task1.o + +task2_1.o: src/task2_1.asm + nasm -f elf src/task2_1.asm -o task2_1.o + +task2_2.o: src/task2_2.asm + nasm -f elf src/task2_2.asm -o task2_2.o + +asm_io.o: src/asm_io.asm + nasm -f elf src/asm_io.asm -o asm_io.o + +clean: + rm -f *.o task1 task2_1 task2_2 +``` + +### Steps to Execute +1. Compile the programs: + ```bash + make + ``` + +2. Run a specific program, for example: + ```bash + ./task1 + ./task2_1 + ./task2_2 + ``` + +3. Clean up compiled files: + ```bash + make clean + ``` + +## Task 2.1: Loops and Conditionals in Assembly + +This task demonstrates loops and conditionals in x86 assembly language. The program prompts the user to input their name and the number of times to print a welcome message, validating the count before proceeding. Invalid inputs are handled gracefully. + +### Key Code Snippets + +#### Data Segment +```asm +segment .data +prompt db "Enter your name: ", 0 +welcome_msg db "Welcome, %s!", 0 +count_prompt db "Enter the number of times to print the welcome message: ", 0 +invalid_count_msg db "The number of times to print the welcome message must be greater than 50 and less than 100!", 0 +``` + +#### Main Logic +```asm +asm_main: + ; Prompts user for name and count, validates input, loops to print message + ; (Full implementation in src/task2_1.asm) +``` + +### Task 2.2: Summing Array Elements + +This task calculates the sum of integers in an array initialized from 1 to 100. + +#### Key Code Snippets + +#### Data Segment +```asm +segment .data +prompt_sum db "The sum of the integers 1 to 100 is: %d", 0 +newline db 10, 0 +``` + +#### Main Logic +```asm +asm_main: + ; Initialize array with values from 1 to 100 + ; Calculate sum of array elements + ; Print the result + ; (Full implementation in src/task2_2.asm) +``` + +### Task 2.3: Summing Array Elements in a Range + +This task extends Task 2.2 to allow summing of array elements within a user-specified range. + +#### Key Code Snippets + +#### Data Segment +```asm +section .data +sum_msg db "The sum is: %d", 0 +start_prompt db "Enter the start index (0-99): ", 0 +end_prompt db "Enter the end index (0-99): ", 0 +error_msg db "Error: Invalid range!", 0 +newline db 10, 0 +``` + +#### Main Logic +```asm +asm_main: + ; Prompt user for start and end indices + ; Validate the range + ; Sum array elements in the specified range + ; Print the result or an error message + ; (Full implementation in src/task2_3.asm) +``` + +## Conclusion +This worksheet covers foundational concepts in assembly programming, demonstrating practical use of loops, conditionals, and data handling. Each task builds upon previous concepts to enhance understanding and proficiency in low-level programming.