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
│ └── image/
│
## 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.
...
...
@@ -50,128 +36,196 @@ In this task, we implement an assembler program that contains a function `asm_ma
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
### How the code work
The program begins by defining two integers (`integer1` and `integer2`) in the `.data` segment.
The `.bss` segment reserves space for the result of the addition.
driver.o:src/driver.c
gcc -m32-c src/driver.c -o driver.o
In the `.text` segment:
- The `mov` instruction is used to load values into registers.
- The `add` instruction performs the addition of the two integers.
- The result is stored in the `result` variable.
- Finally, the program calls the `print_int` function (from `asm_io.inc`) to display the result.
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
### Compiling and Running the Program
To compile and run the task1 program, create a Makefile as follows:
clean:
rm-f*.o task1 task2_1 task2_2
```
### Steps to Execute
1.Compile the programs:
1.Assemble the assembly files into object files
```bash
make
nasm -f elf src/task1.asm -o task1.o
nasm -f elf src/asm_io.asm -o asm_io.o
```
2.Run a specific program, for example:
2.Compile the C file into an object file:
```bash
./task1
./task2_1
./task2_2
gcc -m32-c src/driver.c -o driver.o
```
3.Clean up compiled files:
3.Link the object files to create the final executable
```bash
make clean
gcc -m32 driver.o task1.o asm_io.o -o task1
```
4. Run the code
```bash
./task1
```

## 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.
### How the Code Works
### Key Code Snippets
The program begins by defining several strings in the `.data` segment, such as the prompts for the user's name and the number of repetitions.
#### 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
```
In the `.bss` segment:
- A buffer is reserved to store the user's name (`name`), and a variable (`count`) is reserved to store the number of times the message should be printed.
#### Main Logic
```asm
asm_main:
; Prompts user for name and count, validates input, loops to print message
; (Full implementation in src/task2_1.asm)
```
In the `.text` segment:
1.**Prompt for User Input:**
- The program first prints a prompt asking for the user's name and stores the input in the `name` buffer using `scanf`.
2.**Prompt for the Number of Repetitions:**
- The program then asks for the number of times the welcome message should be printed and stores the value in the `count` variable using `scanf`.
### Task 2.2: Summing Array Elements
3.**Input Validation:**
- The input is checked to ensure it is between 50 and 100 (inclusive). If the input is invalid, an error message is printed, and the program exits.
This task calculates the sum of integers in an array initialized from 1 to 100.
4.**Print the Welcome Message:**
- If the input is valid, the program enters a loop and prints the welcome message the specified number of times. Each time, it uses `printf` to display the message with the user's name.
#### Key Code Snippets
5.**Exit:**
- After printing the message the specified number of times, the program prints a newline and exits.
### 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
```
prompt db "Enter your name: ", 0
format db "%s", 0
welcome_msg db "Welcome, %s!", 0
newline db 10, 0
count_prompt db "Enter the number of times to print the welcome message: ", 0
count_format db "%d", 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:
; Initialize array with values from 1 to 100
; Calculate sum of array elements
; Print the result
; (Full implementation in src/task2_2.asm)
```
### Steps to Execute
1.**Assemble the assembly files into object files**:
```bash
nasm -f elf src/task2_1.asm -o task2_1.o
```
2.**Link the object files to create the final executable**:
```bash
gcc -m32 driver.o task2_1.o asm_io.o -o task2_1
```
3.**Run the program**:
``` bash
./task2_1
```


### Task 2.2: Summing Array Elements
This task calculates the sum of integers in an array initialized from 1 to 100.
### How the code work:
1.**Data Segment**:
-`prompt_sum`: The format string for printing the sum.
-`nl`: A newline character.
2.**BSS Segment**:
-`array`: Reserves space for 100 integers. This array will store the values from 1 to 100.
3.**Text Segment**:
- The program starts by initializing an array of 100 integers, where each element in the array is set to its index + 1 (from 1 to 100).
-`mov ecx, 100` sets the loop counter to 100.
-`mov edi, array` points to the start of the array.
-`xor eax, eax` clears the `eax` register, which is used to store the current value (i+1).
- The loop `init_loop` runs 100 times, incrementing `eax` each time and storing the value in the array.
- Then, the program sums the integers stored in the array.
-`mov ecx, 100` sets the loop counter to 100 again for summing the array.
-`mov edi, array` points to the start of the array again.
-`xor eax, eax` clears `eax` for summing.
- The loop `sum_loop` adds each element of the array to `eax`, which accumulates the sum.
- Finally, the program prints the sum of the integers from 1 to 100.
-`push eax` and `push prompt_sum` push the sum and format string onto the stack for `printf`.
-`call printf` calls `printf` to print the result.
-`add esp, 8` cleans up the stack by removing the two arguments for `printf`.
-`push nl` and `call printf` print a newline.
- The program then exits with `mov eax, 1` (sys_exit) and `int 0x80`, which is a Linux system call for exiting the program.
### Steps to Run
1.**Assemble the Assembly Files into Object Files**:
```bash
nasm -f elf src/task2_2.asm -o task2_2.o
nasm -f elf src/asm_io.asm -o asm_io.o
```
2. **Link the Object Files to Create the Final Executable**:
```bash
gcc -m32 driver.o task2_2.o asm_io.o -o task2_2
```
3. **Run the Program**:
```bash
./task2_2
```

### 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
### How the Code Works
#### 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
```
This program calculates the sum of elements in an array from user-specified start and end indices. It also validates the indices to ensure they fall within the valid range (0-99) and that the start index is not greater than the end index.
#### 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)
```
1. **Data Segment**:
- `sum_msg`: Format string for printing the sum.
- `newline`: A newline character.
- `start_prompt` and `end_prompt`: Strings to prompt the user for input.
- `error_msg`: Error message for invalid input.
2. **BSS Segment**:
- `array`: Reserves space for 100 integers.
- `sum`: Reserves space for the computed sum.
- `start_index` and `end_index`: Reserves space for user-provided indices.
3. **Text Segment**:
- **Array Initialization**:
- Initializes an array with values from 1 to 100 using a loop (`init_loop`).
- **User Input**:
- Prompts the user for start and end indices using `printf` and reads them using `scanf`.
- **Range Validation**:
- Checks that indices are within the range (0-99) and that the start index is not greater than the end index. If invalid, it prints an error message and exits.
- **Sum Calculation**:
- Calculates the sum of array elements from `start_index` to `end_index` using a loop (`sum_loop`).
- **Output**:
- Prints the computed sum using `printf` and adds a newline for formatting.
4. **Error Handling**:
- If the indices are invalid, the program prints an error message (`error_msg`) and exits.
### Steps to Run
1. **Assemble the Assembly Files into Object Files**:
```bash
nasm -f elf src/task2_3.asm -o task2_3.o
```
2. 2. **Link the Object Files to Create the Final Executable**:
```bash
gcc -m32 driver.o task2_3.o asm_io.o -o task2_
```
3. **Run the Program**:
```bash
./task2_3
```


## 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.