diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..ae841a07dc40ea72757579898f061f99a3d23604 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,25 @@ +all: task1 task2_1 task2_2 task2_3 + +task1: task1.asm + nasm -f elf task1.asm -o task1.o + nasm -f elf asm_io.asm -o asm_io.o + gcc -m32 -c driver.c -o driver.o + gcc -m32 driver.o task1.o asm_io.o -o task1 + +task2_1: task2_1.asm + nasm -f elf task2_1.asm -o task2_1.o + nasm -f elf asm_io.asm -o asm_io.o + gcc -m32 -c driver.c -o driver.o + gcc -m32 driver.o task2_1.o asm_io.o -o task2_1 + +task2_2: task2_2.asm + nasm -f elf task2_2.asm -o task2_2.o + nasm -f elf asm_io.asm -o asm_io.o + gcc -m32 -c driver.c -o driver.o + gcc -m32 driver.o task2_2.o asm_io.o -o task2_2 + +task2_3: task2_3.asm + nasm -f elf task2_3.asm -o task2_3.o + nasm -f elf asm_io.asm -o asm_io.o + gcc -m32 -c driver.c -o driver.o + gcc -m32 driver.o task2_3.o asm_io.o -o task2_3 \ No newline at end of file diff --git a/src/task2.asm b/src/task2.asm deleted file mode 100644 index 23a3c9f9eb37a2b8eb35c3142be6f9bc8d0cd770..0000000000000000000000000000000000000000 --- a/src/task2.asm +++ /dev/null @@ -1,51 +0,0 @@ -%include "asm_io.inc" - -section .data - prompt_name db "Enter your name: ", 0 - prompt_times db "Enter the number of times (51-99) to print the welcome message: ", 0 - invalid_input db "Invalid input! Please enter a number between 51 and 99.", 10, 0 - welcome_msg db "Welcome, %s!", 10, 0 - -section .bss - name resb 50 ; Reserve space for the name (50 bytes) - times resd 1 ; Reserve space for the number (4 bytes) - -section .text - extern asm_main ; Declared in driver.c - global asm_main - -asm_main: - ; Prompt for name - mov eax, prompt_name ; Load the address of the prompt string - call print_string ; Print the string - - mov eax, name ; Load the address of the name buffer - mov ebx, 50 ; Set max input length - call read_string ; Read user input into the name buffer - -ask_times: - ; Prompt for the number of times - mov eax, prompt_times ; Load the address of the prompt string - call print_string ; Print the string - - call read_int ; Read an integer from the user - mov [times], eax ; Store the input number in the `times` variable - - ; Validate the input (51 <= times <= 99) - mov eax, [times] ; Load the number into eax - cmp eax, 51 ; Check if less than 51 - jl invalid_input_msg ; Jump if invalid - cmp eax, 99 ; Check if greater than 99 - jg invalid_input_msg ; Jump if invalid - jmp print_messages ; Valid input, proceed - -invalid_input_msg: - mov eax, invalid_input ; Load the invalid input message - call print_string ; Print the string - jmp ask_times ; Re-prompt the user - -print_messages: - mov ecx, [times] ; Load the number of times into ecx (loop counter) - -print_loop: - mov eax, welcome_msg ; Lo diff --git a/src/test b/src/task2_1 similarity index 55% rename from src/test rename to src/task2_1 index 0f67137503f75efd5016ec14f8b0384f4c20e021..5aa87c0b59e588653a21e9a56db8c353debbcfcb 100755 Binary files a/src/test and b/src/task2_1 differ diff --git a/src/task2_1.asm b/src/task2_1.asm new file mode 100644 index 0000000000000000000000000000000000000000..72f2d4aefc90ee82ac5443f21a6022aab918e08e --- /dev/null +++ b/src/task2_1.asm @@ -0,0 +1,98 @@ +segment .data + prompt_num db "Enter number of times to print the message: ", 0 + prompt db "Enter your name: ", 0 + error_prompt db "You have entered a number that does not satisfy the requirement", 0 + format db "%s", 0 + format_digit db "%d", 0 + welcome_msg db "Welcome, %s!", 0 + newline db 10, 0 +segment .bss + name resb 50 ; Reserve space for the name (50 bytes) + num resb 1 + counter resb 1 + +segment .text + extern printf + extern scanf + +global asm_main +asm_main: + ; Print the prompt + push prompt + call printf + add esp, 4 ; Clean up the stack + + ; Read the input + push name ; Address of the buffer to store the input + push format ; Format string for scanf + call scanf + add esp, 8 ; Clean up the stack (2 arguments) + + ;Print the number prompt + push prompt_num + call printf + add esp, 4 + + ; Read the number input + push num + push format_digit + call scanf + add esp, 8 + + ; Loop + mov al, [num] ; Load the value of 'num' into register AL + mov [counter], al ; Store it in the counter variable + + + ; Check if num > 50 + cmp al, 50 ; Compare num with 50 + jle error_exit ; If num <= 50, jump to error_exit + + ; Check if num < 100 + cmp al, 100 ; Compare num with 100 + jge error_exit ; If num >= 100, jump to error_exit + + + + loop_start: + ; Do something here (inside the loop) + ; Print the welcome message + push name ; Push the name to print + push welcome_msg + call printf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Print newline + push newline + call printf + add esp, 4 + + ; Decrement the counter + dec byte [counter] ; Decrement the value of 'counter' + + ; Check if the counter is zero + cmp byte [counter], 0 ; Compare counter with 0 + jne loop_start ; If not zero, jump back to loop_start + + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 + +error_exit: + ; Exit with error code + push error_prompt + call printf + add esp, 4 + + ;Print newline before exiting + push newline + call printf + add esp, 4 + + ;Exit with error code + mov eax, 60 ; System call for exit + mov edi, 1 ; Exit code 1 (error) + syscall \ No newline at end of file diff --git a/src/task2_1.o b/src/task2_1.o new file mode 100644 index 0000000000000000000000000000000000000000..4350d87e98baddc4e272ddabdf7a786ea9b3ebfc Binary files /dev/null and b/src/task2_1.o differ diff --git a/src/test1 b/src/task2_2 similarity index 55% rename from src/test1 rename to src/task2_2 index 82dc7e1279aa15c6de4c0a5f546b1657e2d4d406..a6b9634d0dd374cced68c38ce91c159c595938cf 100755 Binary files a/src/test1 and b/src/task2_2 differ diff --git a/src/task2_2.asm b/src/task2_2.asm new file mode 100644 index 0000000000000000000000000000000000000000..fbcc6a2226aaab9fcfd1280ce4af2b6ed4ec8ebf --- /dev/null +++ b/src/task2_2.asm @@ -0,0 +1,42 @@ +section .data + sum_prompt db "The sum of the numbers is: %d", 10, 0 ; Format string for sum + total db 0 ; Reserve space for the sum result + +section .bss + sum resd 1 ; Reserve space for the sum (4 bytes) + +section .text + extern printf + +global asm_main +asm_main: + + ; Initialize registers + xor eax, eax ; Clear eax, we'll use it to store the sum + + ; Calculate sum of integers from 1 to 100 + mov ecx, 1 ; Start loop counter at 1 + mov ebx, 100 ; Upper limit of the loop + +calculate_sum: + add eax, ecx ; Add the current counter value to the sum (in eax) + inc ecx ; Increment the counter + cmp ecx, ebx ; Compare counter with 100 + jle calculate_sum ; Loop if counter is <= 100 + + ; Now eax contains the sum of numbers from 1 to 100 + + ; Store the sum in the variable sum + mov [sum], eax + + ; Print the sum using printf + push eax ; Push the sum onto the stack (argument for printf) + push sum_prompt ; Push the format string + call printf ; Call printf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 diff --git a/src/task2_2.o b/src/task2_2.o new file mode 100644 index 0000000000000000000000000000000000000000..f819ab729a310e642ec0482d0379f6ddef12e0f8 Binary files /dev/null and b/src/task2_2.o differ diff --git a/src/task2_3 b/src/task2_3 new file mode 100755 index 0000000000000000000000000000000000000000..b46291db5d70dea9b1e48700aaabaa74f164cd97 Binary files /dev/null and b/src/task2_3 differ diff --git a/src/task2_3.asm b/src/task2_3.asm new file mode 100644 index 0000000000000000000000000000000000000000..dfeacf0b5bf1b04c3b0b13824ac391ce3d84ed73 --- /dev/null +++ b/src/task2_3.asm @@ -0,0 +1,73 @@ +section .data + sum_prompt db "The sum of the numbers is: %d", 10, 0 ; Format string for sum + prompt_start db "Enter the start of the range: ", 0 + prompt_end_range db "Enter the end of the range: ", 0 + format db "%d", 0 + newline db 10, 0 + total db 0 ; Reserve space for the sum result + +section .bss + sum resd 1 ; Reserve space for the sum (4 bytes) + start_range resd 1 + end_range resd 1 + +section .text + extern printf + extern scanf + +global asm_main +asm_main: + ; Print start prompt + push prompt_start + call printf + add esp, 4 + + ; Scan start range + push start_range + push format + call scanf + add esp, 8 + + ; Print end range prompt + push prompt_end_range + call printf + add esp, 4 + + ; Scan end range + push end_range + push format + call scanf + add esp, 8 + + ; Initialize sum register (eax = 0) + xor eax, eax ; Clear eax register to store the sum + + ; Load start and end range values into registers + mov ecx, [start_range] ; Start range (ecx) + mov ebx, [end_range] ; End range (ebx) + +calculate_sum: + cmp ecx, ebx ; Compare start value (ecx) with end value (ebx) + jg done_calculation ; If start > end, exit the loop + + add eax, ecx ; Add current value of ecx to eax (sum) + inc ecx ; Increment ecx to move to the next number + jmp calculate_sum ; Repeat the loop + +done_calculation: + ; Now eax contains the sum of numbers from start_range to end_range + + ; Store the sum in the sum variable (optional) + mov [sum], eax + + ; Print the sum using printf + push eax ; Push the sum onto the stack (argument for printf) + push sum_prompt ; Push the format string + call printf ; Call printf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 diff --git a/src/task2_3.o b/src/task2_3.o new file mode 100644 index 0000000000000000000000000000000000000000..cd8ac57b26fa344688fe484d606d2b0c44950ca5 Binary files /dev/null and b/src/task2_3.o differ diff --git a/src/test.asm b/src/test.asm deleted file mode 100644 index 60e4c1146884df0cb6cf4615e3f34ef359b18044..0000000000000000000000000000000000000000 --- a/src/test.asm +++ /dev/null @@ -1,41 +0,0 @@ -segment .data - prompt db "Enter your name: ", 0 - format db "%s", 0 - welcome_msg db "Welcome, %s!", 0 - newline db 10, 0 -segment .bss - name resb 50 ; Reserve space for the name (50 bytes) - -segment .text - extern printf - extern scanf - -global asm_main -asm_main: - ; Print the prompt - push prompt - call printf - add esp, 4 ; Clean up the stack - - ; Read the input - push name ; Address of the buffer to store the input - push format ; Format string for scanf - call scanf - add esp, 8 ; Clean up the stack (2 arguments) - - ; Print the welcome message - push name ; Push the name to print - push welcome_msg - call printf - add esp, 8 ; Clean up the stack (2 arguments) - - ; Print newline - push newline - call printf - add esp, 4 - - ; Exit the program - mov eax, 1 ; syscall: exit - xor ebx, ebx ; exit code 0 - ret - int 0x80 \ No newline at end of file diff --git a/src/test.o b/src/test.o deleted file mode 100644 index 80cb795da7a45f9eea54d6014ee8e95195c3c1ee..0000000000000000000000000000000000000000 Binary files a/src/test.o and /dev/null differ diff --git a/src/test1.asm b/src/test1.asm deleted file mode 100644 index 35f59598881c05d1760c8126093d8d385fa7f0cc..0000000000000000000000000000000000000000 --- a/src/test1.asm +++ /dev/null @@ -1,43 +0,0 @@ -segment .data - prompt db "Enter your name: ", 0 - format db "%s", 0 - welcome_msg db "Welcome, %s!", 0 - newline db 10, 0 - number dd 0 - count db 0 -segment .bss - name resb 50 ; Reserve space for the name (50 bytes) - -segment .text - extern printf - extern scanf - -global asm_main -asm_main: - ; Print the prompt - push prompt - call printf - add esp, 4 ; Clean up the stack - - ; Read the input - push name ; Address of the buffer to store the input - push format ; Format string for scanf - call scanf - add esp, 8 ; Clean up the stack (2 arguments) - - ; Print the welcome message - push name ; Push the name to print - push welcome_msg - call printf - add esp, 8 ; Clean up the stack (2 arguments) - - ; Print newline - push newline - call printf - add esp, 4 - - ; Exit the program - mov eax, 1 ; syscall: exit - xor ebx, ebx ; exit code 0 - ret - int 0x80 \ No newline at end of file diff --git a/src/test1.o b/src/test1.o deleted file mode 100644 index 798af099283855e8416c322fece231be2ebb8f04..0000000000000000000000000000000000000000 Binary files a/src/test1.o and /dev/null differ